Python 在Pyomo/AMPL中定义多个模型
我试图在Python 在Pyomo/AMPL中定义多个模型,python,ampl,pyomo,Python,Ampl,Pyomo,我试图在Pyomo/AMPL中设置(并解决)多个优化问题。为此,我需要首先为AMPL定义模型: model model_1.mod model model_2.mod model model_3.mod ... model model_n.mod 对于Pyomo: model_1 = ConcreteModel() model_2 = ConcreteModel() ... model_n = ConcreteModel() 我想知道是否有一种自动方法可以做到这一点,无论是使用
Pyomo/AMPL
中设置(并解决)多个优化问题。为此,我需要首先为AMPL
定义模型:
model model_1.mod
model model_2.mod
model model_3.mod
...
model model_n.mod
对于Pyomo:
model_1 = ConcreteModel()
model_2 = ConcreteModel()
...
model_n = ConcreteModel()
我想知道是否有一种自动方法可以做到这一点,无论是使用for循环,还是一些索引,这样如果n=100,我就不必编写100
model\u k=ConcreteModel()
您可以使用命令而不是model
在循环中加载AMPL模型:
for {i in 1..n}
commands('model_' & i & '.mod');
在Pyomo中,也可以使用以下方法执行类似操作:
在Python中,您只需创建一个模型列表:
from pyomo.environ import *
models = []
for i in range(100):
models.append( ConcreteModel() )
然后,可以通过索引列表访问每个模型:模型[19]
是第19个模型。谢谢,这非常有用。
from pyomo.environ import *
models = []
for i in range(100):
models.append( ConcreteModel() )