【问题标题】:Set not initializing in Pyomo在 Pyomo 中设置不初始化
【发布时间】:2019-02-20 18:57:58
【问题描述】:

我正在尝试通过以下方式在 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'"

创建变量集时有什么遗漏吗?

【问题讨论】:

    标签: python pyomo


    【解决方案1】:

    您不需要初始化函数中的内部 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)
    

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2017-08-03
      • 2015-04-16
      • 2012-11-05
      • 1970-01-01
      • 2011-08-14
      • 2019-08-10
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多