### Python interface

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 = gp.LinExpr() obj += x obj += y obj += 2 * z m.setObjective(obj, GRB.MAXIMIZE)

### Python matrix interface

The next step is to set the optimization objective:

# Set objective obj = np.array([1.0, 1.0, 2.0]) m.setObjective(obj @ x, GRB.MAXIMIZE)

The objective is built here by computing a dot product between a constant vector and our matrix variable using the overloaded `@`

operator. Note that the constant vector must have the same length as our matrix variable.

The second argument indicates that the sense is maximization.

### C interface

The default sense for the objective function is minimization. Since our example aims to maximize the objective, we need to modify the `ModelSense`

attribute:

/* Change objective sense to maximization */ error = GRBsetintattr(model, GRB_INT_ATTR_MODELSENSE, GRB_MAXIMIZE); if (error) goto QUIT;

### C++ interface

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);

### Java interface

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

// 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);

The objective must be a linear or quadratic function of the variables in the model. In our example, we build our objective by first constructing an empty linear expression and adding three terms to it.

The second argument to `setObjective`

indicates that the optimization sense is maximization.

### .NET interface

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

### MATLAB interface

Added as part of struct. See .... Coming soon.

### R interface

Added as part of struct. See .... Coming soon.

## Comments

0 comments

Please sign in to leave a comment.