所以我试图找到问题的最佳解决方案。我试着在这里复制格式: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索引,从RangeSet(1,model.k)的“1”开始,而不是“0”。