PyPSA与Python:多国电力市场模拟报错及非对称流配置问题
问题描述
我用Python和PyPSA模拟DE、FR、NL三国电力市场,修改官方示例代码后,模型能输出价格、潮流、负荷等结果,但运行时会报错(见截图),不清楚多国模型配置哪里出了问题;另外,我找不到在代码里配置非对称互联潮流的方法(比如出口2000MW、进口1400MW)。
以下是我的代码:
# transmission capacities in MW (not necessarily realistic) transmission = { "DE": {"FR": 2500, "NL": 1400}, "FR": {"NL": 1400}, } network = pypsa.Network() for country in countries: network.add("Bus", country) for plant in power_plant_p_nom[country]: network.add( "Generator", "{} {}".format(country, plant), bus=country, p_nom=power_plant_p_nom[country][plant], marginal_cost=marginal_costs[country][plant], ) network.add("Load", "{} load".format(country), bus=country, p_set=loads[country]) # add transmission as controllable Link if country not in transmission: continue for other_country in countries: if other_country not in transmission[country]: continue # NB: Link is by default unidirectional, so have to set p_min_pu = -1 # to allow bidirectional (i.e. also negative) flow network.add( "Link", "{} - {} link".format(country, other_country), bus0=country, bus1=other_country, p_nom=transmission[country][other_country], p_min_pu=(-0.8 if (country == "DE" and other_country =="FR") else -1), ) network.optimize() # Print Results # print the load active power (P) consumption print(network.loads_t.p) # print the generator active power (P) dispatch print(network.generators_t.p) # print the clearing price (corresponding to gas) print("SMP :", network.buses_t.marginal_price) # print Power Flows print("Power flows:", network.links_t.p0) # link shadow prices print(network.links_t.mu_lower)
一、多国模型配置报错排查
结合代码常见问题,可能的原因及解决方法:
- 重复线路冗余:当前循环逻辑如果后续扩展
transmission字典(比如给FR添加DE的条目),会重复添加DE↔FR的双向线路,冗余约束可能触发求解器警告或错误。PyPSA单条Link设置p_min_pu=-1即可实现双向潮流,无需重复添加。 - 求解器兼容性问题:若报错属于求解器无解、数值不稳定类,可切换求解器(比如从GLPK换成CBC),或调整求解器参数,示例:
network.optimize(solver_name='cbc', solver_options={'ratioGap': 0.01}) - 数据完整性问题:检查
countries、power_plant_p_nom、marginal_costs、loads变量是否完整覆盖DE、FR、NL三国数据,缺失数据会导致元件添加失败。 - 约束冲突:你给DE-FR线路设置的
p_min_pu=-0.8会限制反向潮流最大为2000MW,若该约束与其他供需约束冲突,会导致求解器报错。可先将所有Link的p_min_pu统一设为-1,验证模型基础功能正常后,再逐步添加非对称约束。
二、非对称互联潮流配置方法
要实现出口、进口容量不同的非对称传输,有两种可行方式:
方式1:拆分两条单向Link(直观易维护)
分别定义两个方向的单向Link,各自设置独立的容量上限:
# DE→FR单向链路,最大出口2000MW network.add( "Link", "DE -> FR link", bus0="DE", bus1="FR", p_nom=2000, p_min_pu=0, # 限制为单向传输,只能从DE到FR marginal_cost=0, # 可根据实际传输成本调整 ) # FR→DE单向链路,最大进口1400MW(即FR到DE最多1400MW) network.add( "Link", "FR -> DE link", bus0="FR", bus1="DE", p_nom=1400, p_min_pu=0, marginal_cost=0, )
方式2:单条Link配置比例约束(简洁)
以双向容量的最大值作为p_nom,通过p_max_pu和p_min_pu设置不同方向的容量比例:
比如DE→FR最大2000MW,FR→DE最大1400MW,设置:
network.add( "Link", "DE - FR link", bus0="DE", bus1="FR", p_nom=2000, # 取双向容量的最大值 p_max_pu=1, # DE→FR方向最大2000MW(1*2000) p_min_pu=-0.7, # FR→DE方向最大1400MW(-0.7*2000) )
内容的提问来源于stack exchange,提问作者user23789054
相关产品推荐
相关产品推荐

