python - Pyomo Constraint iteration for Optimal Power Flow
问题描述
I am solving a DC Optimal Power Flow Problem and I am trying to brainstorm the most efficient way to iterate over a constraint in Pyomo.
The following is the data structure, where i and k are the nodes connected through a branch, and X is the reactance, a property of the branch.
The constraint I am having trouble with is the following:
Where the symbol "delta" and "p" is a variable in the constraint, each node has a single value of delta and p. What this constraint basically does, is it insures that all the powers flowing into the node i, from all the connected nodes k, are equal to the existing power value in the same node.
Here is an example for i=1 and i=2, iterations of the constraint.
So I am trying to find the most efficient way to state this constraint in pyomo, where instead of having several constraint iterations written like this:
def P1_rule(modelo):
return modelo.p[0]-L[0]== ((modelo.d[0]-modelo.d[1])/0.1)+((modelo.d[0]-modelo.d[2])/0.1)
model.P1 = Constraint(rule=P1_rule)
def P2_rule(modelo):
return modelo.p[1]-L[1]==((modelo.d[1]-modelo.d[0])/0.1)+((modelo.d[1]-modelo.d[2])/0.1)
model.P2 = Constraint(rule=P2_rule)
def P3_rule(modelo):
return modelo.p[2]-L[2] ==((modelo.d[2]-modelo.d[0])/0.1)+((modelo.d[2]-modelo.d[1])/0.1)
model.P3 = Constraint(rule=P3_rule)
I want a single line like this, so that it easily generalizes over a huge network:
def P3_rule(modelo):
return modelo.p[i] ==((modelo.d[i]-modelo.d[k])/X[k])
model.P3 = Constraint(rule=P3_rule)
I have came up with a way that includes restructuring the data and creating new arrays of indices etc... I would like to see if i can apply the constraint using the data keeping the same structure and more directly.
解决方案
好的,所以我想出了如何去做。我不知道的最好方法是通过在求和中创建一个 if 语句,因此基本上在求和中进行完整的条件迭代。在下面的代码中,G 是节点列表,而“From”和“To”是 FullBranch 数据表中的分支编号列。
def Try_rule(mod,g):
return mod.p[g] - L[g] == sum((mod.d[i-1]-mod.d[k-1])/FullBranch.loc[x,"X"] for x,(i,k) in enumerate(zip(FullBranch["From"], FullBranch["To"])) if i == g+1)
model.Try = Constraint(G,rule=Try_rule)
推荐阅读
- encoding - 该编码文本的类型是什么?
- amazon-web-services - 如何使用 lambda 函数通过使用 nodejs 的无服务器框架将图像/文件上传到 s3 存储桶?
- reactjs - 用 react 实现无限滚动
- python - 在 vscode 中调试时如何覆盖/更正 PYTHONPATH?
- postgresql - 在 Postgres 13 中查询视图的效率
- javascript - 元素对齐 A 位关闭
- node.js - 如何使用nodejs将存储为二进制的privateKey转换为base64?
- html - 我的一半谷歌搜索框周围有一条黑线,我无法更改
- node.js - Next.js - 图像未“定义”
- laravel - 如何将所有功能合并为一个功能?