Using Gurobi within MATLAB’s Problem-Based Optimization
Starting with release R2017b, the MATLAB Optimization Toolbox offers an alternative way to formulate optimization problems, coined “Problem-Based Optimization”. In this section we'll explain how this modeling technique can be used in combination with the Gurobi solver.
The problem-based modeling approach uses an object-oriented paradigm for the components of an optimization problem; the optimization problem itself, the decision variables, and the linear constraints are represented by objects. Their creation and modification is effected through methods. The complete documentation for problem-based optimization is part of the Optimization Toolbox; we will only walk through a simple example. For this it is important that your MATLAB path contains Gurobi's example directory, which can be set as follows:
addpath(fullfile(<path_to_Gurobi>, <architecture>, 'examples', 'matlab'));
The first step is to create an optimization problem:
prob = optimproblem('ObjectiveSense','maximize');
The variable prob now refers to an optimization problem object,
which we have specified to be a maximization problem. Next we create three
non-negative optimization variables: x, y
x = optimvar('x', 'LowerBound', 0); y = optimvar('y', 'LowerBound', 0); z = optimvar('z', 'LowerBound', 0);
With these variables at hand, we now build linear expressions in order to set
an objective function, and to add two linear constraints to prob:
prob.Objective = x + 2 * y + 3 * z; prob.Constraints.cons1 = x + y <= 1; prob.Constraints.cons2 = y + z <= 1;
Finally we create an options object that guides prob's solution
method to the linear
program solver function linprog, and call the solve
options = optimoptions('linprog'); sol = solve(prob, options);
Since the examples directory of the Gurobi installation has been added to the path in the very first step above, a bit of magic happens at this stage: The directory contains a file linprog.m, so that the invocation of the solve method ends up calling this latter function instead of the built-in function linprog of MATLAB's Optimization Toolbox. The following output from Gurobi will be shown on the console:
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Optimize a model with 2 rows, 3 columns and 4 nonzeros Model fingerprint: 0x3a4c68c2 Coefficient statistics: Matrix range [1e+00, 1e+00] Objective range [1e+00, 3e+00] Bounds range [0e+00, 0e+00] RHS range [1e+00, 1e+00] Presolve removed 2 rows and 3 columns Presolve time: 0.03s Presolve: All rows and columns removed Iteration Objective Primal Inf. Dual Inf. Time 0 -4.0000000e+00 0.000000e+00 0.000000e+00 0s Solved in 0 iterations and 0.05 seconds Optimal objective -4.000000000e+00
The example we just discussed can be found in the examples directory in the file opttoolbox_lp.m. The example opttoolbox_mip1.m shows an analogous problem formulation with integer variables, that uses the function intlinprog.m, also found in the Gurobi examples directory, as a surrogate for MATLAB's built-in counterpart.
The modeling constructs provided by the Optimization Toolbox do not cover all the features of Gurobi, e.g., SOS, semi-continuous variables and general constraints to name a few. Moreover not all Gurobi parameters have equivalent counterparts in the option objects for linprog and intlinprog. In order to use such features, Gurobi's own Matlab API should be used.