繁体   English   中英

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

[英]Error from invalid index/value in pyomo, any tips?

所以我试图找到一个问题的最佳解决方案。 我试图在这里复制格式: 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 对索引组件无效。 我不知道该怎么做,因为我只是厌倦了复制上面发布的链接上的内容。 有人知道这个错误吗?

我希望你找到了答案。 如果不是,model.mean 由 model.J 索引,model.J 从“1”开始,使用 RangeSet(1, model.k),而不是“0”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM