Skip to main content

Long Time Spend in Model Construction

Answered

Comments

6 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yin Can,

    Would it be possible to rerun the subproblem with a different objective coefficient without reconstructing the model?

    Yes, typically this is the way you would do it.  I guess you're constructing the dual form of the subproblem since you've mentioned the objective coefficient.  How you modify the objective coefficients can depend on which API you are using with Gurobi, for example with gurobipy you can use Model.setAttr, or if you were using the guroipy Matrix API it may just be quicker to replace the objective, e.g. model.setObjective(newcoeffs@myvariables)

    - Riley

    0
  • CAN YIN
    Curious
    Gurobi-versary
    Conversationalist

    Hello Riley

    Thanks for your answer, it's really helpful and I really appreciate it. 

    I am using Python to get access to Gurobi, and I am trying to reset the model objective with the below code while obj is the updated obj expression. 

            mSub.setObjective(obj, GRB.MAXIMIZE)
    However, I got an error message as below
    NameError: cannot access free variable 'delta_kit' where it is not associated with a value in enclosing scope
     
    I guess the reason for this is because my subproblem was created with a self-defined function like def SubProblem(): 
    Then when I tried to reset the model objective function in 2nd iteration without rebuilding it, The variable wasn't saved and thus can not refer to it. 
    If my guess is wrong, might I know what might be the reason for it? 
    If yes, does it mean I need to create my subproblem globally If I want to rerun it later?  If it's doesn't need to become global, what kind of object do I need to return in order to modify the objective only?
     
    Thanks for reading and thank you again for your help
    Yin Can
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yin Can,

    Then when I tried to reset the model objective function in 2nd iteration without rebuilding it, The variable wasn't saved and thus can not refer to it. 

    Yes, you are correct.

    does it mean I need to create my subproblem globally If I want to rerun it later?  If it's doesn't need to become global, what kind of object do I need to return in order to modify the objective only?

    I'm guessing you're returning the model from the SubProblem function?  If you return the variables too then you will be able to assign them to a Python variable and you can then use them.  Or alternatively you can use model.getVars() in the global scope to obtain a reference to the variables.

    Are you using an iterative approach, which alternates between master and subproblem solves, or are you using a callback approach which uses MIPSOL callbacks to run the subproblem?  If it is the latter you can use a callback class and it would make sense to store the subproblem model in the callback class.

    - Riley

     

    0
  • CAN YIN
    Curious
    Gurobi-versary
    Conversationalist

    Hello Riley

     

    Thank you very much for your help.

    I am using an iterative approach and I am trying to return the subproblem variables together with the subproblem function. And now it works. 

    Thanks again for your effort, I really appreciate it

    Yin Can

    0
  • CAN YIN
    Curious
    Gurobi-versary
    Conversationalist

    Hello Riley

     

    Sorry to bother you again, but I just realized there is another issue, could you please help me with it?

    So the issue is, I am using an iterative approach,  I try to build the model "m" in the 1st iteration, and then in the following iteration, reset the objective function, and then rerun the model "m". 

    The code now is running when I return model "m" and variables

    However,  when I compare the objective value obtained from this approach, with rebuilding the model in every iteration. Their objective value is not the same, Which indicates it probably still has some issues, but I am not sure how it happened. Could you please help identify it? 

    Thanks for reading and sorry again for keep bothering you

    Yin Can

     

     

     

     

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yin Can,

    This may only be an issue if the end result is different - is it different?

    If the two approaches do end up with different results then I would try writing the models to LP files then comparing the two. Do they have the same number of variables and constraints? Are the coefficient statistics the same? Try to find some differences that gives you clues.

    - Riley

    0

Please sign in to leave a comment.