Pyomo创建双索引参数Wind_DA时遭遇索引无效错误的问题求助
Ah, I spot the issue right away — it's a case sensitivity mismatch between your set elements and the keys in your Wind_DA dictionary! Let me break this down clearly:
First, look at how you define your
windset:wind=['W{0}'.format(w+1) for w in range(W)]This generates the list
['W1'](with a capital W).But your
Wind_DAdictionary uses lowercasewin its keys:Wind_DA['w1', 't1']=200 # note the lowercase 'w1'
When Pyomo tries to initialize the seq.Wind_DA parameter, it uses the elements from seq.W (which is ['W1']) to look up values in your dictionary. Since Python strings are case-sensitive, 'W1' doesn't match 'w1' — so Pyomo can't find the corresponding key and throws that KeyError.
The reason Wind_DA[('w1', 't1')] works in the console is because you're directly accessing the dictionary with the exact key you defined. This doesn't involve Pyomo's set elements at all, so the case mismatch doesn't matter there.
Fix Options
You just need to make the case consistent between the set and the dictionary. Pick one of these simple fixes:
Option 1: Use lowercase in the wind set
Adjust the wind list generation to match the dictionary's key case:
wind=['w{0}'.format(w+1) for w in range(W)] # now generates ['w1']
Option 2: Use uppercase in the dictionary keys
Update your Wind_DA keys to match the set's case:
Wind_DA['W1', 't1']=200 Wind_DA['W1', 't2']=200 Wind_DA['W1', 't3']=200
Either change will let Pyomo correctly map the set elements to the dictionary values, and your parameter will initialize without errors.
内容的提问来源于stack exchange,提问作者errenmike1806

