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 expression 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.AddTerm(1.0, x);
  obj.AddTerm(1.0, y);
  obj.AddTerm(2.0, z);
  model.SetObjective(obj, GRB.MAXIMIZE);

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search