You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyomo创建双索引参数Wind_DA时遭遇索引无效错误的问题求助

KeyError with Pyomo Dual-Indexed Param: Mismatched Case in Set Elements and Dictionary Keys

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:

  1. First, look at how you define your wind set:

    wind=['W{0}'.format(w+1) for w in range(W)]
    

    This generates the list ['W1'] (with a capital W).

  2. But your Wind_DA dictionary uses lowercase w in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:07:48