I want to create a pyomo model with 1000 basic pyomo variables. I know it is a bad idea to do this like the following script. And it is also not working. I hope you understand the idea and be able to help me.
import pyomo.core as pyomo
def create_model_a():
m = pyomo.ConcreteModel()
for i in range(1000):
m.var_i = pyomo.Var(within=pyomo.NonNegativeReals)
return m
so basically instead of writing m.var_0 = ...
to m.var_999 = ...
, I used a for loop and of course in this way it is not working but the idea is creating 1000 variables without hard-coding m.var_0
, m.var_1
, m.var_2
, and so on till m.var_999
. How can I do it?
I want to create this not to model anything but I wanna use memory profile on this function to understand how much memory is needed for a pyomo model with 1000 variables.
Ps: I tried following and it is not working (cannot see any declarations when I cast m.pprint()
):
def create_model_a():
m = pyomo.ConcreteModel()
m.var = {}
for i in range(1000):
m.var[i] = pyomo.Var(within=pyomo.NonNegativeReals)
return m
PS2: checked also How to increment variable names/Is this a bad idea and How do I create a variable number of variables? ... sadly no help
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))