Tuesday, February 18, 2014

How to use Gurobi Solver

public static GRBEnv env; //Set up The Environment

public static void initialize()
        {
            env = new GRBEnv("mip1.log");
            env.Set(GRB.IntParam.LogToConsole, 0);
            init = true;
        }

 GRBModel model = new GRBModel(env); //Set up The Model
 sv.decisionVar = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, sv.decisionVarName); //Create Decision Var
model.Update(); //Update the model after adding variable

GRBLinExpr decisionVarExpr =null;
If(i==0){
decisionVarExpr=decisionvar;
}else{
           decisionVarExpr.AddTerm(1, decisionvar);//Accumulate Decision Var
}

 model.AddConstr(decisionVarExpr == 1, "decision" + j);//Add Constraint


GRBLinExpr objectiveFunction=..
 model.SetObjective(objectiveFunction, GRB.MAXIMIZE); //Set Objective Function
 model.Optimize();//Optimize it

  int status = model.Get(GRB.IntAttr.Status); //Get the overall Status
            if (status == GRB.Status.INFEASIBLE)
            {
                resultString = "";
                return false;
            }

objectiveFunction.Value // See the value of Objective function
decisionVar.Get(GRB.DoubleAttr.X).ToString() == "1" // See the value of a decision variable that has been created before


No comments:

Post a Comment