### Python interface

The next step in our example is to add variables to the model.

# Create variables x = m.addVar(vtype=GRB.BINARY, name="x") y = m.addVar(vtype=GRB.BINARY, name="y") z = m.addVar(vtype=GRB.BINARY, name="z")

Variables are added through the `addVar()`

method on a model object (or `addVars()`

if you wish to add more than one at a time). A variable is always associated with a particular model.

Python allows you to pass arguments by position or by name. We've passed them by name here. Each variable gets a type (binary), and a name. We use the default values for the other arguments. Please refer to the online help (`help(Model.addVar)`

in the Gurobi Shell) for further details on `addVar()`

.

### Python matrix interface

The next step adds a matrix variable to the model:

# Create variables x = m.addMVar(shape=3, vtype=GRB.BINARY, name="x")

A matrix variable is added through the `addMVar()`

method on a model object. In this case the matrix variable consists of a 1-D array of 3 binary variables. Variables are always associated with a particular model.

### C interface

Once we create a Gurobi model, we can start adding variables and constraints to it. In our example, we'll begin by adding variables:

/* Add variables */ obj[0] = 1; obj[1] = 1; obj[2] = 2; vtype[0] = GRB_BINARY; vtype[1] = GRB_BINARY; vtype[2] = GRB_BINARY; error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, NULL, vtype, NULL); if (error) goto QUIT;

The first argument to `GRBaddvars()` is the model to which the variables are being added. The second is the number of added variables (3 in our example).

Arguments three through six describe the constraint matrix coefficients associated with the new variables. The third argument gives the number of non-zero constraint matrix entries associated with the new variables, and the next three arguments give details on these non-zeros. In our example, we'll be adding these non-zeros when we add the constraints. Thus, the non-zero count here is zero, and the following three arguments are all `NULL`

.

The seventh argument to `GRBaddvars()` is the linear objective coefficient for each new variable. Since our example aims to maximize the objective, and by default Gurobi will minimize the objective, we'll need to change the objective sense. This is done in the next statement. Note we could have multiplied the objective coefficients by -1 instead (since maximizing is equivalent to minimizing .

The next two arguments specify the lower and upper bounds of the variables, respectively. The `NULL`

values indicate that these variables should take their default values (0.0 and 1.0 for binary variables).

The tenth argument specifies the types of the variables. In this example, the variables are all binary (`GRB_BINARY`

).

The final argument gives the names of the variables. In this case, we allow the variable names to take their default values (`x0, x1, and x2`

).

### C++ interface

The next step in our example is to add variables to the model.

// Create variables GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x"); GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y"); GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "z");

Variables are added through the `addVar()`

method on the model object (or `addVars()`

if you wish to add more than one at a time). A variable is always associated with a particular model.

The first and second arguments to the `addVar()`

call are the variable lower and upper bounds, respectively. The third argument is the linear objective coefficient (zero here - we'll set the objective later). The fourth argument is the variable type. Our variables are all binary in this example. The final argument is the name of the variable.

The `addVar()`

method has been overloaded to accept several different argument lists. Please refer to the Gurobi Reference Manual for further details.

### Java interface

The next step in our example is to add variables to the model.

// Create variables GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x"); GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y"); GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

Variables are added through the `addVar()`

method on a model object (or `addVars()`

if you wish to add more than one at a time). A variable is always associated with a particular model.

The first and second arguments to the `addVar()`

call are the variable lower and upper bounds, respectively. The third argument is the linear objective coefficient (zero here - we'll set the objective later). The fourth argument is the variable type. Our variables are all binary in this example. The final argument is the name of the variable.

The `addVar()`

method has been overloaded to accept several different argument lists. Please refer to the Gurobi Reference Manual for further details.

### .NET interface

The next step in our example is to add variables to the model.

// Create variables GRBVar x = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x"); GRBVar y = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "y"); GRBVar z = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

Variables are added through the `AddVar()`

method on a model object (or `AddVars`

if you wish to add more than one at a time). A variable is always associated with a particular model.

The first and second arguments to the `AddVar()`

call are the variable lower and upper bounds, respectively. The third argument is the linear objective coefficient (zero here - we'll set the objective later). The fourth argument is the variable type. Our variables are all binary in this example. The final argument is the name of the variable.

The `AddVar()`

method has been overloaded to accept several different argument lists. Please refer to the Gurobi Reference Manual for further details.

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