简体   繁体   English

索引对索引组件无效。 pyomo 中带有双索引的参数

[英]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.

相关问题 PYOMO 错误:KeyError:“索引'0'对索引组件'y'无效” - PYOMO ERROR: KeyError: “Index '0' is not valid for indexed component 'y' ” Pyomo无法使用索引集索引组件 - Pyomo Cannot index a component with an indexed set Pyomo KeyError:“访问索引组件时出错:索引'('student_5','company_3','meetingtime_1')'对数组组件'var_X'无效” - Pyomo KeyError: “Error accessing indexed component: Index '('student_5', 'company_3', 'meetingtime_1')' is not valid for array component 'var_X'” Pyomo:从 json 存档中加载三个维度集数据错误:无法索引具有索引集的组件 - Pyomo: Loading three dimension set data from json archive error: Cannot index a component with an indexed set 源错误消息=“索引'('d1','i1')'对于索引组件'NFix'无效” - source error message=“Index '('d1', 'i1')' is not valid for indexed component 'NFix'” 由带有 Pyomo 的索引集索引的变量 - Variable indexed by an indexed Set with Pyomo 将索引变量的值设置为索引表达式-pyomo - Setting value of an indexed Variable to an indexed Expression - pyomo Pyomo中的多索引约束或目标 - Multi indexed constraints or objectives in Pyomo 如何在Pyomo中索引矩阵 - How to index a matrix in Pyomo Pyomo 参数和变量编码 - Pyomo parameter and variable encoding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM