Skip to main content
 首页 » 编程设计

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

2024年11月24日9del

所以我正在尝试找到问题的最佳解决方案。我试图在此处复制格式: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”。