How to update value of parameter during model iteration?
AnsweredHello good people,
I am aiming to recalculate (&update) parameters value after each (or possible) iterations if x=1. How can I update value of parameter during model iteration? In the given problem, x is a Binary Decision variable & p is the parameter. I am writing the model using GurobiPy. I defined p as a dataframe, x is the binary var in gp. If the model is giving us x=1 in ith iteration, I want updated parameter value in (i+1)th iteration (& avoid using default values).
if x[i] = 1 >> p[i+1 & onward] = p[i] - 100
Methods I tried (but no luck yet): Adding parameter value as a constraint:
model.addConstrs(p[i:,] == 12 if certain condition is true)
but since there is no decision variable in this constraint, solver is eventually ignoring this constraint. The same condition is used to tell x[i] == 1.
I am trying to think a mathematical relationship between this parameter & decision variable, but seems like there is no plausible relation which I can use in objective function or decision var. I am fine to move to multidict or ways other than dataframe to represent parameter if that helps.
Note: By parameter I meant parameter in the optimization model formulation, not the input parameter of the Gurobi solver.
-
Hi Tanmoy,
Do you mean your model formulation is parametrized based on a given \(p\) value?
Gurobi Callbacks provide some flexibility such that you can add new lazy constraints to the model by the method Model.cbLazy() from within a callback function when the search tree is at a MIP node or when a MIP solution is found.
However, it seems to me that you would like to modify your model formulation depending on whether specific feasible solutions found or not. If this is the case, you can still use Gurobi Callbacks, terminate the Gurobi Optimizer as soon as a feasible solution with the specific criteria is found, update the model by removing/adding new constraints/variables, and then call optimize again on the model object. You might want to look at the article How do I change parameters in a callback?. In this article, parameters refer to Gurobi parameters, but this is a good example of how to use Gurobi Callbacks to conditionally terminate the solver.
Best regards,
Maliheh
1 -
Hi Meliheh,
Thanks for your reply.
Yes, I am trying to modify formulation based on feasible solution found. I will try out your suggestion and keep progress posted here.
0 -
Hi Tanmoy,
Did you solve this issue please? Because I want to update a parameter set value in an i+1 stage to start from its beginning value if a decision value of a binary variable become 1 in an ith stage. please let me know if you found a way to do that.
1 -
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.0
Please sign in to leave a comment.
Comments
4 comments