Building the model

The example begins by building an optimization model. The data associated with an optimization model must be stored in a MATLAB struct. Fields in this struct contain the different parts of the model. A few fields are mandatory: the constraint matrix (A), the objective vector (obj), the right-hand side vector (rhs), and the constraint sense vector (sense). A model can also include optional fields (e.g., the objective sense modelsense).

The example uses the built-in sparse function to build the constraint matrix A. The Gurobi MATLAB interface only accepts sparse matrices as input. If you have a dense matrix, use sparse to convert it to a sparse matrix before passing it to our interface.

Subsequent statements populate other fields of the model variable, including the objective vector, the right-hand-side vector, and the constraint sense vector.

In addition to the mandatory fields, this example also sets two optional fields: modelsense and vtype. The former is used to indicate the sense of the objective function. The default is minimization, so we've set the fields equal to 'max' to indicate that we would like to maximize the specified objective. The vtype field is used to indicate the types of the variables in the model. In our example, all variables are binary ('B'). Note that our interface allows you to specify a scalar value for the sense and vtype arguments. The Gurobi interface will expand that scalar to a constant array of the appropriate length. In this example, the scalar value 'B' will be expanded to an array of length 3, containing one 'B' value for each column of A.

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