Pypsa和Python - 模拟多个国家电力市场时出错
在运行Python代码时出现控制台错误:
我正在尝试使用Python和PyPSA模拟一个由三个国家组成的电力市场。在这个例子中,这三个国家是德国(DE)、法国(FR)和荷兰(NL)。
我不太明白如何正确修改PyPSA文档中的示例代码,以实现这三个国家的配置。
下面是我的代码,这段代码导致了我在截图中看到的错误信息。PyPSA模型给我返回了所有输出的值:价格、流量和负荷。但是我也收到了附带的错误信息,所以我不知道我没有正确指定什么,以解决多个国家的模型问题。
另外,有些互联线路的流量是不对称的(比如出口是2000MW,但进口是1400MW),我在PyPSA文档中没有找到如何在模型代码中指定这种情况的方法。
# 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)
1 个回答
0
我觉得你遇到这个错误是因为你的循环写得不对。比如说,在第一次循环中,你把一个公交车(代表第一个国家)放进了国家列表里。接着你添加了一些生产,这没问题。但是问题出现在你要建立国家之间的连接时,因为这个连接需要两个公交车作为输入,而“另一个国家”的公交车在网络中还没有定义好。所以就出现了错误信息。
为了避免这个问题,你可以先用一个循环把所有的公交车(国家)都放好,然后再用另一个循环来添加公交车之间的连接。