[英]Defining multiple models in Pyomo/AMPL
I am trying to set up (and solve) multiple optimizations problems in Pyomo/AMPL
. 我试图在
Pyomo/AMPL
设置(并解决)多个优化问题。 For this I need to define the models first, for AMPL
: 为此,我需要首先为
AMPL
定义模型:
model model_1.mod
model model_2.mod
model model_3.mod
...
model model_n.mod
for Pyomo
: 对于
Pyomo
:
model_1 = ConcreteModel()
model_2 = ConcreteModel()
...
model_n = ConcreteModel()
I was wondering if there is an automatic way to do this, whether with a for loop, or some indexing so that if n=100 I don't have to write 100 model_k = ConcreteModel()
. 我想知道是否有一种自动的方法来执行此操作,无论是使用for循环还是使用某些索引,以便如果n = 100,我不必编写100
model_k = ConcreteModel()
。
In Python, you can simply create a list of models: 在Python中,您可以简单地创建一个模型列表:
from pyomo.environ import *
models = []
for i in range(100):
models.append( ConcreteModel() )
Then, each model can be accessed by indexing the list: models[19]
is the 19th model. 然后,可以通过索引列表来访问每个模型:
models[19]
是第19个模型。
You can load AMPL models in a loop using commands
instead of model
: 您可以使用
commands
而不是model
循环加载AMPL model
:
for {i in 1..n}
commands('model_' & i & '.mod');
Similar thing can be done in Pyomo using standard Python's mechanisms : 可以使用标准Python的机制在Pyomo中完成类似的操作:
g = globals()
for i in range(n + 1):
g['model_' + str(i)] = ConcreteModel()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.