[英]Index is not valid for indexed component. Parameter with double indexing in pyomo
I am trying to create a parameter Wind_DA
with double index in the following way:我正在尝试通过以下方式创建具有双索引的参数
Wind_DA
:
import pandas as pd
import pyomo.environ as pe
import pyomo.opt as po
#DATA
T=3;
W=1;
time = ['t{0}'.format(t+1) for t in range(T)]
wind=['W{0}'.format(w+1) for w in range(W)]
Wind_DA={}
Wind_DA['w1', 't1']=200
Wind_DA['w1', 't2']=200
Wind_DA['w1', 't3']=200
#MODEL
seq=pe.ConcreteModel()
### SETS
seq.W = pe.Set(initialize = wind)
seq.T =pe.Set(initialize = time)
### PARAMETERS
seq.Wind_DA = pe.Param(seq.W, seq.T, initialize = Wind_DA)
I am getting the following error:我收到以下错误:
KeyError: "Index '('w1', 't1')' is not valid for indexed component 'Wind_DA'". KeyError:“索引 '('w1', 't1')' 对索引组件 'Wind_DA' 无效”。
However, when I type on the console Wind_DA[('w1', 't1')]
I am getting 200
, which means that this dictionary has that index.但是,当我在控制台
Wind_DA[('w1', 't1')]
上键入时,我得到200
,这意味着这本词典具有该索引。 What could be the problem?可能是什么问题呢? Thank you in advance!
先感谢您!
It just a typing error.这只是一个打字错误。
When creating the wind array with wind=['W{0}'.format(w+1) for w in range(W)]
you're using a capital W, but when creating the param Wind_DA = {}...
you're using a lower W当使用
wind=['W{0}'.format(w+1) for w in range(W)]
创建风阵列时,您使用的是大写 W,但是在创建参数Wind_DA = {}...
你使用的是较低的 W
Just change wind=['W{0}'.format(w+1) for w in range(W)]
for a lowercase w
and that should work properly.只需将
wind=['W{0}'.format(w+1) for w in range(W)]
更改为小写w
,这样就可以正常工作了。 wind=['w{0}'.format(w+1) for w in range(W)]
import pandas as pd
import pyomo.environ as pe
import pyomo.opt as po
#DATA
T=3
W=1
time = ['t{0}'.format(t+1) for t in range(T)]
wind=['w{0}'.format(w+1) for w in range(W)]
Wind_DA={}
Wind_DA['w1', 't1']=200
Wind_DA['w1', 't2']=200
Wind_DA['w1', 't3']=200
#MODEL
seq=pe.ConcreteModel()
### SETS
seq.W = pe.Set(initialize = wind)
seq.T =pe.Set(initialize = time)
### PARAMETERS
seq.Wind_DA = pe.Param(seq.W, seq.T, initialize = Wind_DA)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.