Different methods to solve a model
AnsweredHello, I am trying to write a piece of script with different methods to solve a problem. This problem has a set of constraints and variables that don't change but the methods do. Each method adds certain constraints. For example, one method is a bi-level reformulation which adds dual variables and their corresponding constraints. And the other one is an enumeration technique that does not add the dual variables or the dual constraints.
def construct_model(**data):
m = gp.model()
# add vars
# add constraint
return m
def method_1(model):
model.addVars() #variables that are added for this particular method
model.addConstrs() #constraints that are added for this particular method
model.optimize()
def method_2(model):
model.addVars() #variables that are added for this method but do not exist for method 1
model.addConstr() #variables that are added for this method but do not exist in method 1
model.optimize()
My question is, after I pass my model in my different methods, Will the model carry the variables and constraints for each method? for example if I run method 1 and then method 2, will the model carry the vars and constrs from method 1 into method 2? Do I have to remove the variables and constraints as a first step inside my methods?
def method_1(model):
try:
model.remove([model.getVarByName('names from method_2')])
model.remove([model.getConstrByName('names from method_2')])
model.addVars() # variables from method 1
Your insights will be greatly appreciated.
Best,
Jose
-
Hi Jose,
as far as I know, each gp.Model() is a separate object in Python, and hence, it will contain all the constraints and variables which were added to it.
If your models are not too big, you could try generating a copy of the model and then use the two solution methods on the two model instances separately. If this is not the case, you'll have to remove the variables and constraints you don't need "manually".
Hope this helps.
Best regards
Jonasz1 -
Hi Jonasz,
Thanks so much for your answer
It is actually the opposite, I've noticed that when building the linear constraints it takes quite a while (+1000 linear equations) so I wanted to have the model already built and then just run it through the different methods. To save some time when creating the object. It is clear that what I want to do will have to be implemented by adding and removing manually. I'll try it, and it might be the case that the time is cut down (hopefully).
Best regards
Jose
0
Please sign in to leave a comment.
Comments
2 comments