I'm trying to define a set of variables in Pyomo the following way:
def initA ( i , model ):
for i in range(p)
yield -1
def initB ( i , model ):
for i in range(p)
yield random.randrage(-999999, 999999)
# variables
model.A = Set()
model.B = Set()
model.a = Var(model.A, within=IntegerSet, bounds=(-1,1), initialize=initA)
model.b = Var(model.B, domain=Reals, initialize=initB)
I use Pyomo's printing function to verify the sets and i get this:
a : Size=0, Index=A
Key : Lower : Value : Upper : Fixed : Stale : Domain
b : Size=0, Index=B
Key : Lower : Value : Upper : Fixed : Stale : Domain
And when I try to solve the model I receive this error:
ERROR: Rule failed when generating expression for objective OBJ: KeyError:
"Index '1' is not valid for indexed component 'a'"
Is there something I'm missing when creating the variable sets?
You don't need the internal for loops in the initialization functions. Also, the model needs to be the first argument in these functions:
def initA(model, i):
return -1
def initB(model, i):
return random.randrage(-999999, 999999)
# variables
model.A = Set()
model.B = Set()
model.a = Var(model.A, within=IntegerSet, bounds=(-1,1), initialize=initA)
model.b = Var(model.B, domain=Reals, initialize=initB)