Concepts
Features
Reference
Starting with release R2017b, the MATLAB Optimization Toolbox offers analternative way to formulate optimization problems, coined“Problem-Based Optimization”. In this section we’ll explain how thismodeling technique can be used in combination with the Gurobi solver.
The problem-based modeling approach uses an object-oriented paradigm forthe components of an optimization problem; the optimization problemitself, the decision variables, and the linear constraints arerepresented by objects. Their creation and modification is effectedthrough methods. The complete documentation for problem-basedoptimization is part of the Optimization Toolbox; we will only walkthrough a simple example. For this it is important that your MATLAB pathcontains 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 variableprob now refers to an optimization problem object,which we have specified to be a maximization problem. Next we createthree non-negative optimization variables:x,y andz:
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 orderto set an objective function, and to add two linear constraints toprob:
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 guidesprob‘s solutionmethod to the linear program solver functionlinprog, and call thesolve method.
options=optimoptions('linprog');sol=solve(prob,options);
Since theexamples directory of the Gurobi installation has beenadded to the path in the very first step above, a bit of magic happensat this stage: The directory contains a filelinprog.m, so that theinvocation of thesolve method ends up calling this latter functioninstead of the built-in functionlinprog of MATLAB’s OptimizationToolbox. The following output from Gurobi will be shown on the console:
Gurobi Optimizer version 11.0.1 build v11.0.1rc0 ()Optimize a model with 2 rows, 3 columns and 4 nonzerosModel fingerprint: 0x3a4c68c2Coefficient 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 columnsPresolve time: 0.03sPresolve: All rows and columns removedIteration Objective Primal Inf. Dual Inf. Time 0 -4.0000000e+00 0.000000e+00 0.000000e+00 0sSolved in 0 iterations and 0.05 secondsOptimal objective -4.000000000e+00
The example we just discussed can be found in theexamples directoryin the fileopttoolbox_lp.m. The exampleopttoolbox_mip1.m showsan analogous problem formulation with integer variables, that uses thefunctionintlinprog.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 notcover all the features of Gurobi, e.g., SOS, semi-continuous variablesand general constraints to name a few. Moreover not all Gurobiparameters have equivalent counterparts in the option objects forlinprog andintlinprog. In order to use such features, Gurobi’sown Matlab API should be used.
Help and Feedback