在 Pyomo 中设置不初始化

Set not initializing in Pyomo

我正在尝试通过以下方式在 Pyomo 中定义一组变量:

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)

我使用 Pyomo 的打印功能来验证集合,我得到了这个:

a : Size=0, Index=A
  Key : Lower : Value : Upper : Fixed : Stale : Domain
b : Size=0, Index=B
  Key : Lower : Value : Upper : Fixed : Stale : Domain

当我尝试求解模型时收到此错误:

ERROR: Rule failed when generating expression for objective OBJ: KeyError:
"Index '1' is not valid for indexed component 'a'"

创建变量集时我是否遗漏了什么?

您不需要在初始化函数中使用内部 for 循环。此外,模型需要是这些函数中的第一个参数:

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)