Setting the objective

The next step in the example is to set the optimization objective:


    // Set objective: maximize x + y + 2 z
    model.setObjective(x + y + 2 * z, GRB_MAXIMIZE);

The objective is built here using overloaded operators. The C++ API overloads the arithmetic operators to allow you to build linear and quadratic expressions involving Gurobi variables.

The second argument indicates that the sense is maximization.

Note that while this simple example builds the objective in a single statement using an explicit list of terms, more complex programs will typically build it incrementally. For example:

  GRBLinExpr obj = 0.0;
  obj += x;
  obj += y;
  obj += 2*z;
  model.setObjective(obj, GRB_MAXIMIZE);