Setting the objective

Filter Content By
Version
Languages

Setting the objective

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

    # Set objective
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)


The objective is built here using overloaded operators. The Python 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:

  obj = LinExpr();
obj += x;
obj += y;
obj += 2*z;
model.setObjective(obj, GRB.MAXIMIZE);