Skip to main content

Matlab "analytical" models using Gurobi as a solver?

Answered

Comments

13 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    Thanks Eli,

    Do you also know how to send in Gurobi parameters "params.XX" 

    into gurobi from Matlab, using "problem-based" models?

     

    /Lars

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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.

    With intlinprog, you can additionally use:

    If you want to use more parameters, you should use the Gurobi-supported MATLAB API.

    1
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    You mean using the A, x, b, and c formulation approach?

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    Ah. So almost "better" to write an own script with "sprinf" in Matlab to create a gurobi program text file?

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Lars Abrahamsson
    Gurobi-versary
    First Question
    Conversationalist

    I think I understand now - thanks!

    0

Post is closed for comments.