Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Objective coefficient for new variable

Tags:

gurobi

In order to create new GRBVar, I need to provide Objective coefficient for new variable:

GRBVar var = model.addVar (double lowerBound,
        double upperBound,
        double obj, // objective coefficient
        char type,
        String name);

According to this example, the value can be set to 0. So I wonder what objective coefficient is.

like image 503
Long Thai Avatar asked Feb 06 '23 04:02

Long Thai


1 Answers

Objective coefficient is the coefficient of the variable in your objective function. In the example you have given :

 maximize    x +   y + 2 z
 subject to  x + 2 y + 3 z <= 4
             x +   y       >= 1
 x, y, z binary

your objective function is maximize x + y + 2 z

so Objective coefficients are for x: 1 for y: 1 and for z: 2

While creating variables you can give coefficients arbitrary ( here they are as you said 0.0 )

 // Create variables

  GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
  GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
  GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

But later you should set to the actual objective coefficients:

  // Set objective: maximize x + y + 2 z

  GRBLinExpr expr = new GRBLinExpr();
  expr.addTerm(1.0, x); 
  expr.addTerm(1.0, y); 
  expr.addTerm(2.0, z);
  model.setObjective(expr, GRB.MAXIMIZE);
like image 183
Lecuy Avatar answered Apr 10 '23 14:04

Lecuy