Matlab "analytical" models using Gurobi as a solver?
AnsweredHi,
The MIP example on: https://www.gurobi.com/documentation/9.0/quickstart_windows/matlab_example.html is for the classical style of optimizing (what they call a Solver-Based approach) in Matlab.
Nowadays there exist a somewhat more analytical/symbolic approach (they call it Problem-Based approach).
Can also models created with this problem-based approach (using e.g. optimvar, optimproblem, optimconstr, etc.) be solved by Gurobi with the same syntax as in the quickstart? If so, what have I missed? Or how should it be done?
/Lars
-
Hi Lars,
Yes, you can build models with the problem-based approach and solve them with Gurobi. See here for more information about this. In the \( \texttt{examples/matlab} \) directory of your Gurobi installation, you can find some examples (\( \texttt{opttoolbox_lp.m} \) and \( \texttt{opttoolbox_mip1.m} \)) as well as the functions necessary to interface with Gurobi (\( \texttt{linprog.m} \) and \( \texttt{intlinprog.m} \)).
Eli
0 -
Hi Eli,
Thanks! I got as far as the
options = optimoptions('linprog'); sol = solve(prob, options);
part. When I try to send my existing problem to Gurobi, I get the following error message:
>> sol = solve(model_00(16).problem, options);
Error using optim.internal.problemdef.ProblemImpl/solveImpl
The value of 'x0' is invalid. Initial point must be a nonempty struct.
Error in optim.problemdef.OptimizationProblem/solve
I can however not find any information in "model_00(16).problem" about starting levels of variables - where do I do that?
It looks like this:
>> model_00(16).problem
ans =
OptimizationProblem with properties:
Description: ''
ObjectiveSense: 'maximize'
Variables: [1×1 struct] containing 8 OptimizationVariables
Objective: [1×1 OptimizationExpression]
Constraints: [1×1 struct] containing 16 OptimizationConstraints
See problem formulation with show.
>> model_00(16).problem.Variables
ans =
struct with fields:
Q: [8760×15 optim.problemdef.OptimizationVariable]
p: [8760×15 optim.problemdef.OptimizationVariable]
q: [8760×15 optim.problemdef.OptimizationVariable]
q1: [8760×15 optim.problemdef.OptimizationVariable]
q2: [8760×15 optim.problemdef.OptimizationVariable]
s: [8760×17 optim.problemdef.OptimizationVariable]
v: [8760×17 optim.problemdef.OptimizationVariable]
v_slack: [2×17 optim.problemdef.OptimizationVariable]/Lars
0 -
Since you're using MATLAB's problem-based \( \texttt{solve()} \) function, the second argument is expected to be a start vector \( \texttt{x0} \). Thus, MATLAB is trying to parse \( \texttt{options} \) as an initial starting point. Looking at the MATLAB documentation, the correct way to pass in options is \( \texttt{solve(prob, 'Options', options)} \).
1 -
Thanks Eli,
Do you also know how to send in Gurobi parameters "params.XX"
- https://www.gurobi.com/documentation/9.0/quickstart_windows/matlab_modifying_grb_param.html
- https://www.gurobi.com/documentation/9.0/matlab_html/matlab_the_params_argument.html
into gurobi from Matlab, using "problem-based" models?
/Lars
0 -
This is unfortunately a downside of using MATLAB's problem-based approach; only a small subset of Gurobi parameters are available through this interface. With linprog, you can use the following option fields. The Gurobi parameters they set are in parentheses.
- Display (OutputFlag)
- MaxTime (TimeLimit)
With intlinprog, you can additionally use:
- MaxFeasiblePoints (SolutionLimit)
- RelativeGapTolerance (MIPGap)
- AbsoluteGapTolerance (MIPGapAbs)
If you want to use more parameters, you should use the Gurobi-supported MATLAB API.
1 -
You mean using the A, x, b, and c formulation approach?
0 -
Yes. The \( \texttt{linprog.m} \) and \( \texttt{intlinprog.m} \) files provided by Gurobi are simply wrappers for the Gurobi-supported MATLAB API. These functions translate the MATLAB problem-based model into the "solver-based" construction that you see in the Gurobi documentation. MATLAB didn't design its problem-based approach with Gurobi in mind, so there is no way to incorporate every Gurobi feature/parameter in the problem-based structure.
1 -
Ah. So almost "better" to write an own script with "sprinf" in Matlab to create a gurobi program text file?
0 -
I'm not sure I follow what you mean, could you elaborate? It's okay to use MATLAB's problem-based approach with Gurobi, but it's not easy to set most Gurobi parameters, solve a feasibility relaxation, read a model file, etc. If you want to do anything more than build and solve a model, I recommend using Gurobi's official MATLAB API.
0 -
What I mean ... That as an alternative to the official GUI, when already having a Matlab "problem based" model, one could write scripts oneself that converts the model to Gurobi code in text files ...
Thank you anyway. Then I know ...
0 -
It would be much easier to modify the \(\texttt{linprog.m}\) and/or \(\texttt{intlinprog.m}\) files provided by Gurobi. These files contain all of the code necessary to convert the problem-based model into a model built using the official Gurobi MATLAB API.
0 -
I think I understand now - thanks!
0
Please sign in to leave a comment.
Comments
12 comments