문제

I am trying to set up (and solve) multiple optimizations problems in Pyomo/AMPL. For this I need to define the models first, for AMPL:

model model_1.mod

model model_2.mod

model model_3.mod

...

model model_n.mod

for 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().

도움이 되었습니까?

해결책 2

You can load AMPL models in a loop using commands instead of model:

for {i in 1..n}
  commands('model_' & i & '.mod');

Similar thing can be done in Pyomo using standard Python's mechanisms:

g = globals()
for i in range(n + 1):
  g['model_' + str(i)] = ConcreteModel()

다른 팁

In Python, you can simply create a list of models:

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top