Building the model

The example begins by building an optimization model. The data associated with an optimization model must be stored in a MATLABstruct. Fields in this struct contain the different parts of the model. The most fundamental fields are: The constraint matrix (A), the objective vector (obj), the right-hand side vector (rhs), and the constraint sense vector (sense). Among these, only the constraint matrix is mandatory, and default values are substituted for all other model fields in case they are missing.

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

In addition to the fields discussed above, this example sets two more fields:modelsenseandvtype. The former is used to indicate the sense of the objective function. The default is minimization, so we've set the field equal to'max'to indicate that we would like to maximize the specified objective. Thevtypefield 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 thesenseandvtypearguments. 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 ofA.