Speed up iterative model solving and updating parameters

926 views
Skip to first unread message

Salva

unread,
Jun 12, 2018, 5:15:13 AM6/12/18
to pyomo...@googlegroups.com
Hi,
I've built an abstract model in pyomo which takes 50 seconds to be created (model.create_instance(data)) when loading data through DataPortal() (data.load(file.tab)) and 10 seconds to be solved. I have to run the model for each day of the year for 20 years changing input data at each iteration (loading a new DataPortal() for each day).
Is there a way to update parameters avoiding to repeat the model.create_instance(data) command everytime or speeding up this section?

Thanks in advance,

Salva

David Woodruff

unread,
Jun 12, 2018, 7:26:04 AM6/12/18
to pyomo...@googlegroups.com
Yes. A mutable Param can be changed without rebuilding the model. Mutable Params are retained in expressions symbolically. When you declare a Param that is mutable, use the mutable=True flag, e.g.,
import pyomo.environ as pyo
model.P = pyo.Param(mutable=True)

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Salva

unread,
Jun 12, 2018, 9:12:18 AM6/12/18
to Pyomo Forum
Thanks for the reply, and please excuse me if I'm not understanding correctly the answer.

My parameters are flagged with the mutable=true command. So my code should be something like:

data = DataPortal()
data.load("filename.tab', param=['parameter'])
instance=model.create_instance(data)
opt = SolverFactory("cbc")
results = opt.solve(instance)   # first solve with first data
data.load("filename2.tab', param=['parameter']) # update parameter
results2 = opt.solve(instance)    # solve with updated parameters

I think I'm missing something..
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.

Santiago rodriguez

unread,
Jun 12, 2018, 9:27:55 AM6/12/18
to Pyomo Forum
I believe you can create the model once with model.create_instance(data). That will return a concrete model with the mutable parameters set to the values provided in the data file. After the first iteration, and once you have identified the parameters that change from iteration to iteration, you could update those parameters in the concrete model. Say you have a parameter model.Demand that you have constructed with the mutable keyword equal to true and with a value of 10 in the data, after the creation of the instance e.g. concrete_instance = model.create_instance(data),  you can update the value of the demand parameter by simply concrete_instance.Demand = 5. That would change the value without rebuilding the entire model from scratch again and again.

Hope that helps

Salva

unread,
Jun 12, 2018, 1:22:30 PM6/12/18
to Pyomo Forum
Thank you for the clear answer.

The point is that I have to update more than 50k parameters and it would be helpful to do it through dataportal.

Anyway, thanks again.

Santiago rodriguez

unread,
Jun 12, 2018, 2:19:43 PM6/12/18
to Pyomo Forum
Just to clarify, even though the example I gave uses a simple scalar parameter, indexed parameters can also be declared mutable. Hence, even if you have lots of parameters, as long as those are indexed over a set, you could quickly read the data and loop through with standard python instructions to update the value of the parameters.

Juan Esteban Moreno Agudelo

unread,
Nov 7, 2018, 8:28:02 PM11/7/18
to Pyomo Forum
How to change a parameter indexed in two sets ?

for example I have this:

model.Caudales = z.Param(model.H,model.periodos, initialize=q,  mutable=True )

q is a dictionary with two keys 

solve the model with that data and after I want to change them 

I did this:

instance = model.create_instance()
instance.Caudales[instance.H, instance.periodos].value = qq 
instance.preprocess()

but it doesn't work, qq is a dictionary equal to q 

qq = {('G1', 1): 117.744,
 ('G1', 2): 98.0284,
 ('G1', 3): 98.0688,
 ('G1', 4): 117.9826,
 ('G1', 5): 136.143,
 ('G1', 6): 139.6622,
 ('G1', 7): 102.826,
 ('G1', 8): 72.5888,
 ('G1', 9): 65.7478,

How to change the parameter when it has two sets ?

thanks for your help 
Reply all
Reply to author
Forward
0 new messages