5

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.

Long Thai
  • 807
  • 3
  • 12
  • 34

1 Answers1

8

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);
Lecuy
  • 81
  • 1
  • 3
  • 2
    then what is the use of using `Objective Coefficients` while creating variables. Could you please elaborate your answer?. – Praveenkumar Beedanal Sep 06 '18 at 09:23
  • You can mix and match the two methods to create an objective. If you use the objectve coefficients it will create a linear objective and it will get minimized, unless you specify `model.ModelSense = GRB.MAXIMIZE` which will maximize this linear combination. If you use `setObjective` it will override the former. – Francois Vanderseypen Aug 04 '23 at 14:45