package ch.uzh.ce;

import edu.harvard.econcs.jopt.solver.IMIP;
import edu.harvard.econcs.jopt.solver.IMIPResult;
import edu.harvard.econcs.jopt.solver.IMIPSolver;
import edu.harvard.econcs.jopt.solver.client.SolverClient;
import edu.harvard.econcs.jopt.solver.mip.Constraint;
import edu.harvard.econcs.jopt.solver.mip.MIPWrapper;
import edu.harvard.econcs.jopt.solver.mip.Variable;

public class LinearProgram {
    public IMIP createProgram() {
        //Example taken from 
        // http://de.wikipedia.org/wiki/Lineare_Optimierung#Mathematische_Modellierung
        MIPWrapper mipWrapper = MIPWrapper.makeNewMaxMIP();
        //Create variables both need to be positive
        Variable x = mipWrapper.makeNewDoubleVar("x");
        x.setLowerBound(0);
        Variable y = mipWrapper.makeNewDoubleVar("y");
        y.setLowerBound(0);
        mipWrapper.addObjectiveTerm(300, x);
        mipWrapper.addObjectiveTerm(500, y);
        
        //Create new constraint
        Constraint mashineA = mipWrapper.beginNewLEQConstraint(170);
        mashineA.addTerm(1, x);
        mashineA.addTerm(2, y);
        //You need to end the constraint to add it to the model!
        mipWrapper.endConstraint(mashineA);

        //Create new constraint
        Constraint mashineB = mipWrapper.beginNewLEQConstraint(150);
        mashineB.addTerm(1, x);
        mashineB.addTerm(1, y);
        //You need to end the constraint to add it to the model!
        mipWrapper.endConstraint(mashineB);

        //Create new constraint
        Constraint mashineC = mipWrapper.beginNewLEQConstraint(180);
        mashineC.addTerm(3, y);
        //You need to end the constraint to add it to the model!
        mipWrapper.endConstraint(mashineB);

        
        return mipWrapper;
    }

    public IMIPResult solveProgram(IMIP mip) {
        IMIPSolver solver = new SolverClient();
        return solver.solve(mip);
    }

    public void printProgram(IMIP mip, IMIPResult result) {
        System.out.println("Results:");
        // Get specific var value
        System.out.println("Var X "+ result.getValue("x"));
        System.out.println("---------------------");
        // Print full program
        System.out.println(result.toString(mip));
    }
}
