创建 Pyomo 约束的性能 [英] Performance of creating Pyomo constraints

查看:43
本文介绍了创建 Pyomo 约束的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 pyomo 设置一个更大的能量优化问题.正如其他 问题,但是我设法加快了大多数有问题的线路,但能量流约束除外.

I am setting up a biggish energy optimization problem with pyomo. The setup took unreasonably long as mentioned in other questions, however I managed to speed up most of the problematic lines, except the energy flow constraints.

与所有其他约束相反,该流程包括所有元素的总和.所以我决定重写流变量的创建方式,以便它们包含所有元素的索引,希望这能改善这种情况.我的代码现在看起来像这样:

Opposed to all other constraints the flow includes a sum over all elements. So I decided to rewrite the way the flow variables are created so they include an index over all elements, hoping this would improve the situation. My code now looks like this:

def flows(model, et, t):
return pyo.quicksum(model.in_flow[:, et, t], 
                    linear=True,
                    start=pyo.quicksum(model.out_flow[:, et, t], 
                                       linear=True)
                    ) == 0

model.add_component("flows", 
                    pyo.Constraint(model.energy_type, 
                                   model.t, 
                                   rule=flows)
                   )

然而,这仍然需要 65% 的模型设置时间.

However this still takes 65% of my model setup time.

我把它分解成一个嵌套的 for 循环,看看谁花时间:

I broke it down into a nested for loop just to see who takes the time:

for t in model.t:
    for et in model.energy_type:
        e = model.in_flow[:, et, t]
        f = model.out_flow[:, et, t]
        es = pyo.quicksum(e)
        fs = pyo.quicksum(f)

这大约需要相同的运行时间,并且全部"花费在最后两行.链接流和设置线性标志提供了一些小的改进,但没有实质性的改进.来自 PyPSA 的 共享代码 仍然使用旧的 coopr3 表达式生成器,因此它不再起作用.我也无法弄清楚它会如何使用.

This takes about the same runtime and "all" of it is spend in the last two lines. Chaining up the quicksums and setting the linear flag gives some minor improvements but nothing substantially. The shared code from PyPSA still uses the old coopr3 expression generator, so it does not work any more. I also could not figure out how it would be used.

关于如何提高模型生成性能的任何建议?

Any suggestions of how to improve the model generation performance?

推荐答案

好吧,原来问题出在切片上.

Well, it turns out the problem was with the slices.

def flows(model, et, t):
    vars = [model.in_flow[obj, et, t] for obj in model.objects_index]
    vars.extend([model.out_flow[obj, et, t] for obj in model.objects_index])
    return pyo.quicksum(vars) == 0

约束规则的这种重构使我的模型创建速度提高了大约 60%.我找到了另外两个地方,我做了类似的重新表述.我现在从优化前的 120 秒下降到大约 7 秒.

This refomulation of the constraint rule speed up my model creation by about 60%. I found two other places where I did a similar reformulation. I am now down from 120s before the optimisation, to about 7s.

这篇关于创建 Pyomo 约束的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆