来自pyomo中无效索引/值的错误,有什么提示吗?

时间:2019-03-22 22:17:09

标签: python pyomo ampl

因此,我正在尝试找到问题的最佳解决方案。我试图在此处复制格式:https://pyomo.readthedocs.io/en/latest/pyomo_overview/simple_examples.html

我制作了一个.dat文件和一个model.py文件,但遇到一个奇怪的索引错误,试图调整我的.dat文件,也遇到了类似的错误。我不确定如何修复.dat文件并解决错误。

.py文件:

import numpy as np
import pandas as pd
import pyomo.environ as pyo

# probability model

# model parameters
model = pyo.AbstractModel()
model.k = pyo.Param(within=pyo.NonNegativeIntegers)
model.L = pyo.Param(within=pyo.NonNegativeReals)
model.J = pyo.RangeSet(1, model.k)
model.mean = pyo.Param(model.J)
model.var = pyo.Param(model.J)


# decision variable
model.n = pyo.Var(model.J, domain=pyo.NonNegativeIntegers)

# objective function
def objective(model):
    return sum(model.n[j]*model.n[j]*model.var[j] for j in model.J)/(sum(model.n[j] for j in model.J)**2)
    + sum(model.n[j] for j in model.J)

model.Obj = pyo.Objective(rule=objective)

def constraint(model):
    return (sum(model.n[j]*model.mean[j] for j in model.J)/sum(model.n[j] for j in model.J) >= model.L)

model.Const = pyo.Constraint(rule=constraint)

.dat初始文件:

param k := 9 ;
param L := 0 ;
param mean := 0.9581711079943904 0.8838415730337069 0.8984853752157478 0.8986654447608105 0.8663875972671153 0.8211460863742999 0.7847600783146949 0.7788767153059641 0.7484350221893459 0.6894005956320362 ;
param var := 0.18608283075010482 0.3505045997323567 0.3302027274449947 0.3346348960541469 0.3985411187856784 0.47583289045335103 0.5711695307985707 0.595920918431739 0.639842447473589 0.7188389471803242 ;

第一个错误:

[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.20] Creating model
ERROR: Constructing component 'mean' from data={0.9581711079943904:
    0.8838415730337069, 0.8984853752157478: 0.8986654447608105,
    0.8663875972671153: 0.8211460863742999, 0.7847600783146949:
    0.7788767153059641, 0.7484350221893459: 0.6894005956320362} failed:
        RuntimeError: Failed to set value for param=mean,
        index=0.9581711079943904, value=0.8838415730337069.
        source error message="Index '0.9581711079943904' is not valid for indexed
        component 'mean'"
[    0.20] Pyomo Finished
ERROR: Unexpected exception while running model:
        Failed to set value for param=mean, index=0.9581711079943904,
        value=0.8838415730337069.
        source error message="Index '0.9581711079943904' is not valid for indexed
        component 'mean'"

我认为可能是需要将索引放在.dat文件中的值之前,因为错误列出了以第一个为索引的连续值,所以我更改了.dat文件。

调整后的.dat文件:

param k := 9 ;
param L := 0 ;
param mean := 0 0.9581711079943904 1 0.8838415730337069 2 0.8984853752157478 3 0.8986654447608105 4 0.8663875972671153 5 0.8211460863742999 6 0.7847600783146949 7 0.7788767153059641 8 0.7484350221893459 9 0.6894005956320362 ;
param var := 0 0.18608283075010482 1 0.3505045997323567 2 0.3302027274449947 3 0.3346348960541469 4 0.3985411187856784 5 0.47583289045335103 6 0.5711695307985707 7 0.595920918431739 8 0.639842447473589 9 0.7188389471803242 ;

第二次错误:

[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.20] Creating model
ERROR: Constructing component 'mean' from data={0: 0.9581711079943904, 1:
    0.8838415730337069, 2: 0.8984853752157478, 3: 0.8986654447608105, 4:
    0.8663875972671153, 5: 0.8211460863742999, 6: 0.7847600783146949, 7:
    0.7788767153059641, 8: 0.7484350221893459, 9: 0.6894005956320362} failed:
        RuntimeError: Failed to set value for param=mean, index=0,
        value=0.9581711079943904.
        source error message="Index '0' is not valid for indexed component
        'mean'"
[    0.20] Pyomo Finished
ERROR: Unexpected exception while running model:
        Failed to set value for param=mean, index=0, value=0.9581711079943904.
        source error message="Index '0' is not valid for indexed component
        'mean'"

在这里说0不适用于索引组件。我不确定该怎么做,因为我只是累了复制上面发布的链接中的内容。有人知道这个错误吗?

1 个答案:

答案 0 :(得分:1)

希望您找到了答案。如果不是,则model.mean将由model.J和model.J索引,该值从RangeSet(1,model.k)的“ 1”开始,而不是“ 0”。