model parameter update on sequential model optimization
AnsweredHello guys on a previous post I have found :
I solved the “updating parameter” issue by using “for loop”,
Steps in the for loop:
1. Run the model.
2. Once optimal solution found, gather them, update parameter I needed.
3. Recreate new model & use updated parameters.
Note: whatever I need from previous model, I’d store them in a dataframe when I am inside that model.
Feel free to ask more specifics or tell more details about your problem.
My question is: I am solving a model in a for loop and in each iteration I have to change some fixed parameter values (Bendrs Algorithm). Even though I update the parameter values I observe in the lp file that parameter values do not change in the model. Is there any official way to update parameter values? I have tried thee model.update() code and it did not work.
Kind regards
Iason
-
Hi Iason,
If I understood you correctly, in each iteration of your Benders algorithm, you need to adapt some coefficients in the model of your Benders subproblem.
How did you change the coefficients? Could you paste the corresponding code snippet?
Model.update() should take over the changes. Also, if you write the LP file after the changes, they should be reflected there.Best regards,
Mario0 -
Hello Mario, what worked for me is to make a function that forms my LP model, and each time I want to update a parameter I call the function so the problem is formed again. My function includes the line SPmodel = gp.Model('SP'). I found out you can add constraints by using Model.update() but you cannot change the parameters this way.
Kind regards
Iason0 -
Your function should not include "SPmodel = gp.Model('SP')" since this creates a new model in every function call instead of modifying an existing one. You should create the model once and then hand over SPmodel to your function.
Then, with SPmodel.chgCoeff(), you can modify coefficients of variables in the constraints. For this, you need variable and constraint object references that you can either get with getVars() and getConstrs(), or by storing the return values of addVars() and addConstrs().
0 -
Mario will the execution of the code be slower with my method? Do you have any handy example of how to use SPmodel.chgCoeff()?
Kind regards
Iason0 -
If you create the model from scratch in each Benders iteration, it could lead to some runtime overhead, but it mainly depends on the size of the subproblem model. You could measure the time from SPmodel = g.Model('SP') to SPmodel.optimize() (without optimize()), and see whether the building time summed up over all iterations is significant.
Unfortunately, I have not found a nice example for chgCoeff() in our example library, but you can find a few related posts in this community, e.g., this one.
0 -
Thanks Mario, this example works.
Kind regards
Iason0
Please sign in to leave a comment.
Comments
6 comments