Skip to main content

Running consecutive optimizations on the same model

Ongoing

Comments

12 comments

  • Simranjit Kaur
    Gurobi Staff Gurobi Staff

    Hi,

    Gurobi will continue optimization from where it left off in the first run with the change in the parameters. For example, the following script reading the "glass4.mps" model, which we distribute with the Gurobi installation,

    import gurobipy as gp
    m = gp.read("glass4.mps")
    m.params.TimeLimit=1
    m.optimize()

    m.params.heuristics=0.8
    m.params.TimeLimit=2
    m.optimize()
    produces the following log that shows the second run continues optimization from the last node of the previous run.
    glass4: 396 rows, 322 columns, 1815 nonzeros
    Set parameter TimeLimit to value 1
    Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)
    [...]

    Explored 8190 nodes (35843 simplex iterations) in 1.01 seconds (0.52 work units)
    Thread count was 16 (of 16 available processors)

    Solution count 10: 1.80001e+09 1.80001e+09 1.80002e+09 ... 1.90002e+09

    Time limit reached
    Best objective 1.800014350000e+09, best bound 8.000040048387e+08, gap 55.5557%

    Set parameter Heuristics to value 0.8
    Set parameter TimeLimit to value 2
    Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)
    [ ...]

    Continuing optimization...

    H 8190  4981                    1.800014e+09 8.0000e+08  55.6%   4.3    1s
    8190  4981 1.0000e+09   34   53 1.8000e+09 8.0000e+08  55.6%   4.3    1s
    [...]
    Best regards,
    Simran
    1
  • D
    Gurobi-versary
    Investigator
    Conversationalist

    What does parameter Heuristics do? 

    Also, can I write any model as .mps too? 

    And thanks for answering my original question. 

    0
  • Simranjit Kaur
    Gurobi Staff Gurobi Staff

    Hi,

    Gurobi runs a range of heuristics to find feasible solutions while solving a model. The Heuristics parameter defines the fraction of total runtime that can be used for running heuristics during a solve. 

    Yes, once a model is constructed, it can be exported to an MPS file, for example, by using model.write('mymodel.mps') in Python.

    Best regards,
    Simran

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hi,

    Would it be possible to run the same model in consecutive optimizations? removing from the previous constraint, then running again, without the last optimization being influenced by the removed constraint or the previous result? both optimization( with and without constrain)t  in a while loop for adding constraints ( Gurobi C++).

    Or is it necessary to run another model? 

    Thank you for any help in advance.

    Best Regards, 

     

    Alessandra 

     

     

    0
  • Simranjit Kaur
    Gurobi Staff Gurobi Staff

    Hi Alessandra,

    If I understand correctly, you would like to do the following: create a model, optimize it, modify the model and optimize it again. To that end, I would suggest going through the examples in the "Modify a model" section of our reference manual.

    Additionally, you might find Gurobi's Multiple-scenario feature useful if you want to solve different scenarios together instead of solving them one by one.

    Best regards,
    Simran

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hi Simran,

    Thank you for your fast answer!

    My optimization procedure in summary consists of the following :

    -Runs suboptimal model without constraint in while loop;

    -Add constraints in part, until the optimization value of the previous optimization holds continue adding and optimizing until all constraints are added.

    -If the previous optimization value does not hold, make user cuts to reproduce in the optimization free of constraints, this goes also until all constraints are implicitly in the model ;

    The problem is that until now, I tried to remove the constraint from the model, to see if the constraint does not influence the value for the next optimization. Even when the constraint isn't there to be removed (the constraint was created in the particular loop and I get from GUROBI the 20001 error) the subsequent optimization continues to reproduce the constrained optimization value. It is impossible to test my algorithm cuts!

    In "modify a model" section, I found out the model.reset () would be an answer ?

     

    Best regards,

     

    Alessandra 

     

     

     

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Alessandra,

    Simran is off today so I'll quickly poke my head in to say yes, model.reset() is the way to go.

    - Riley

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hi people, 

     

    I'm still trying to solve my problem... I tried several scenarios,
    didn't work at all, 
    first I tried just to change in RHS (Gurobi 11/C++) expr[x].RHS(new_value), but it wasn't recognized, it gave an error message (attribute not recognized),
    then I tried to add a constraint as Cut/Lazy in the callback,
    but unfortunately neither worked, it gave an error message
    (AddCut/Lazy is only for linear constraints, error 10003).

    By the way, all Gurobi/C++ variables are multidimensional arrays (the documentation does not support this).
    My expr[x] is:

    GRBQuadExpr* expr = new expr[X};

    expr[x]= C++ variable + array [x][y]*(GRBQuadExpr[j][g]+GRBQuadExpr[p][q])
    do
    //Callback;
    WHERE==MIP_Node:
    for x=k:
    addCut/Lazy(expr[x] >= limit);
    model.optimize;
    k +=1;
    while (k < T)


    Then the restriction for k=k-1 must be ignored at the moment  x=k.

    Any help/suggestions?

    I'm grateful for any help you can give me.

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Alessandra,

    As you have discovered you cannot add quadratic constraints as lazy constraints.  However, let's say you have a quadratic expression Q(x) and you want to add the lazy constraint Q(x) >= b.  As a workaround, add a variable y, and set it's bounds appropriately and add the constraint Q(x) == y.  Then you can add y >= b as a lazy constraint without error.

    - Riley

     

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hi Riley,

     

    Thank you! With the suggested approach (and also remembering that Q(x) has many variables compounding it, as the x goes further in set X, the previous constraint in x-1 must be removed or be ineffective), what would be better: Lazy Constraint or Lazy Cut? For clarity, the solution is likely not in the solution pool.

     

    Thank you in advance one more time .

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Alessandra,

    If it is needed for the validity of the solution then it needs to be a lazy constraint, otherwise a lazy cut is preferred.  This may also help if you have not seen it already: https://support.gurobi.com/hc/en-us/articles/360025804471-What-is-the-difference-between-user-cuts-and-lazy-constraints

    - Riley

     

    0
  • Alessandra Vieira
    Gurobi-versary
    Conversationalist
    First Question

    Hi Riley, Hi Simran,

    long story short: I can't use callback in my case because the expressions and constraints inserted into the model are quadratic!  Multi-scenario is also not an answer, because quadratic constraint RHS is inserted with the Gurobi Expression into the model. I am trying now:

    - create a copy of the original model, since the additional constraints can not be directly inserted into the original model, as I want in my algorithm. GRBModel new_model = GRBModel (orig_model);

    -inserted the additional constraint in this copy. In this way, the new constraints should not influence the original model.

    - I use the results to establish new constraints for the original model. 

    The problem is the constraint dedicated to the new model (NEWmodel.addQConstr()) has no effects!

    But once I inserted these constraints in the old model, they worked!

    The additional challenge in my Gurobi C++ program is the constraint should be created, destroyed, and created again in the new model.

    I appreciate your help.

    Best regards

    Alessandra.

     

     

     

     

    0

Please sign in to leave a comment.