### Python interface

The next step in the example is to add the linear constraints. The first constraint is added here:

# Add constraint: x + 2 y + 3 z <= 4 m.addConstr(x + 2 * y + 3 * z <= 4, "c0")

As with variables, constraints are always associated with a specific model. They are created using the `addConstr()` method on the model object.

We again use overloaded arithmetic operators to build linear expressions. The comparison operators are also overloaded to make it easier to build constraints.

The second argument to `addConstr`

gives the (optional) constraint name.

Again, this simple example builds the linear expression for the constraint in a single statement using an explicit list of terms. More complex programs will typically build the expression incrementally.

The second constraint is created in a similar manner:

# Add constraint: x + y >= 1 m.addConstr(x + y >= 1, "c1")

### Python matrix interface

The next step in the example is to add our two linear constraints. This is done by building a sparse matrix that captures the constraint matrix:

# Build (sparse) constraint matrix val = np.array([1.0, 2.0, 3.0, -1.0, -1.0]) row = np.array([0, 0, 0, 1, 1]) col = np.array([0, 1, 2, 0, 1]) A = sp.csr_matrix((val, (row, col)), shape=(2, 3))

The matrix has two rows, one for each constraint, and three columns, one for each variable in our matrix variable. The `row`

and `col`

arrays gives the row and column indices for the 5 non-zero values in the sparse matrix, respectively. The `val`

array gives the numerical values. Note that we multiply the greater-than constraint by -1 to transform it to a less-than constraint.

We also capture the right-hand side in a NumPy array:

# Build rhs vector rhs = np.array([4.0, -1.0])

We then use the overloaded `@`

operator to build a linear matrix expression, and then use the overloaded less-than-or-equal operator to add two constraints (one for each row in the matrix expression):

# Add constraints m.addConstr(A @ x <= rhs, name="c")

### C interface

Once the new variables are integrated into the model, the next step is to add our two linear constraints. These constraints are added through the `GRBaddconstr()` routine. To add a constraint, you must specify several pieces of information, including the non-zero values associated with the constraint, the constraint sense, the right-hand side value, and the constraint name. These are all specified as arguments to `GRBaddconstr()`:

/* First constraint: x + 2 y + 3 z <= 4 */ ind[0] = 0; ind[1] = 1; ind[2] = 2; val[0] = 1; val[1] = 2; val[2] = 3; error = GRBaddconstr(model, 3, ind, val, GRB_LESS_EQUAL, 4.0, "c0"); if (error) goto QUIT;

The first argument of `GRBaddconstr()` is the model to which the constraint is being added. The second is the total number of non-zero coefficients associated with the new constraint. The next two arguments describe the non-zeros in the new constraint. Constraint coefficients are specified using a list of index-value pairs, one for each non-zero value. In our example, the first constraint to be added is . We have chosen to make the first variable in our constraint matrix, the second, and the third (note that this choice is arbitrary). Given our variable ordering choice, the index-value pairs that are required for our first constraint are (0, 1.0), (1, 2.0), and (2, 3.0). These pairs are placed in the `ind`

and `val`

arrays.

The fifth argument to `GRBaddconstr()` provides the sense of the new constraint. Possible values are `GRB_LESS_EQUAL`

, `GRB_GREATER_EQUAL`

, or `GRB_EQUAL`

. The sixth argument gives the right-hand side value (in the C API, all the variables must appear on the left-hand side of the constraint). The final argument gives the name of the constraint (we allow the constraint to take its default name here by specifying `NULL`

for the argument). The second constraint is added in a similar fashion.

Note that routine `GRBaddconstrs()` would allow you to add both constraints in a single call. The arguments for this routine are much more complex, though, without providing any significant advantages, so we recommend that you add one constraint at a time.

### C++ interface

The next step in the example is to add the linear constraints. The first constraint is added here:

// Add constraint: x + 2 y + 3 z <= 4 model.addConstr(x + 2 * y + 3 * z <= 4, "c0");

As with variables, constraints are always associated with a specific model. They are created using the `addConstr()` or `addConstrs()` methods on the model object.

We again use overloaded arithmetic operators to build the linear expression. The comparison operators are also overloaded to make it easier to build constraints.

The second argument to `addConstr`

gives the (optional) constraint name.

Again, this simple example builds the linear expression for the constraint in a single statement using an explicit list of terms. More complex programs will typically build the expression incrementally.

The second constraint in our model is added with this similar call:

// Add constraint: x + y >= 1 model.addConstr(x + y >= 1, "c1");

### Java interface

The next step in the example is to add the linear constraints. The first constraint is added here:

// Add constraint: x + 2 y + 3 z <= 4 expr = new GRBLinExpr(); expr.addTerm(1.0, x); expr.addTerm(2.0, y); expr.addTerm(3.0, z); model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

As with variables, constraints are always associated with a specific model. They are created using the `addConstr()` or `addConstrs()` methods on the model object.

The first argument to `addConstr()` is the left-hand side of the constraint. We built the left-hand side by first creating an empty linear expression object, and then adding three terms to it. The second argument is the constraint sense (`GRB_LESS_EQUAL`

, `GRB_GREATER_EQUAL`

, or `GRB_EQUAL`

). The third argument is the right-hand side (a constant in our example). The final argument is the constraint name. Several signatures are available for `addConstr()`. Please consult the Gurobi Reference Manual for details.

The second constraint is created in a similar manner:

// Add constraint: x + y >= 1 expr = new GRBLinExpr(); expr.addTerm(1.0, x); expr.addTerm(1.0, y); model.addConstr(expr, GRB.GREATER_EQUAL, 1.0, "c1");

### .NET interface

The next step in the example is to add the linear constraints:

// Add constraint: x + 2 y + 3 z <= 4 model.AddConstr(x + 2 * y + 3 * z <= 4.0, "c0");

As with variables, constraints are always associated with a specific model. They are created using the `AddConstr()` or `AddConstrs()` methods on the model object.

We again use overloaded arithmetic operators to build linear expressions. The comparison operators are also overloaded to make it easier to build constraints.

The second argument to `AddConstr`

gives the constraint name.

The Gurobi .NET interface also allows you to add constraints by building linear expressions in a term-by-term fashion:

GRBLinExpr expr = 0.0; expr.AddTerm(1.0, x); expr.AddTerm(2.0, y); expr.AddTerm(3.0, z); model.AddConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

This particular `AddConstr()` signature takes a linear expression that captures the left-hand side of the constraint as its first argument, the sense of the constraint as its second argument, and a linear expression that captures the right-hand side of the constraint as its third argument. The constraint name is given as the fourth argument.

### 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.