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

标签 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 对索引组件无效。我不知道该怎么做,因为我只是厌倦了复制上面发布的链接上的内容。有人知道这个错误吗?

最佳答案

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

关于python - pyomo 中的无效索引/值错误,有什么提示吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308486/

相关文章:

matlab - 将 MATLAB 与 AMPL 结合使用

linear-programming - GLPK Mathprog 集合组

python - 如何在 map 方法中预处理和标记 TensorFlow CsvDataset?

python - 检查 Pandas 列值是否不在列表中

python - 访问 pyomo 约束中出现的所有变量

pyomo - 如何保存 Pyomo 解决方案并重新加载到单独的运行时环境中?

使用 Django 时出现 Javascript 验证错误

python - Pandas pivot_table 因列和边距而失败

python - 创建 Pyomo 约束的性能

algorithm - 如何在AMPL中写入 "not equal to"或设置变量参数的条件?