简体   繁体   English

每次迭代的 Pyomo 变量的动态下限/上限

[英]Dynamic lower/upper bound of Pyomo Variable for every iteration

I want to adjust the lower and upper bound of three pyo.Var() depending on the outcome of another variable.我想根据另一个变量的结果调整三个 pyo.Var() 的下限和上限。 the variable model.inverter_power should be in the i-th iteration bigger than model.inverter_power[i].lb + model.fcr_power[i] but also smaller than model.inverter_power[i].ub - model.fcr_power[i]) How can i implement this?变量 model.inverter_power 应该在第 i 次迭代中大于 model.inverter_power[i].lb + model.fcr_power[i] 但也小于 model.inverter_power[i].ub - 88361688.9fcr_power)5我该如何实施? Unfortuntately my idea is not working....不幸的是我的想法是行不通的....

        def fcr_inverter_reduction(model, i):
                return (model.inverter_power[i] >= model.inverter_power[i].lb + model.fcr_power[i],
                        model.inverter_power[i] <= model.inverter_power[i].ub - model.fcr_power[i])

        model.fcr_inverter_rule = pyo.Constraint(model.i, rule = fcr_inverter_reduction)

I tried various versions of this code, not only is this code not linear anymore, so I used ipopt as a solver but no solution can be found, i got this error message:我尝试了这段代码的各种版本,不仅这段代码不再是线性的,所以我使用 ipopt 作为求解器但没有找到解决方案,我收到以下错误消息:

  File "D:\.conda\envs\PythonEnviromentV2\lib\site-packages\pyomo\opt\base\solvers.py", line 596, in solve
    raise ApplicationError(
pyomo.common.errors.ApplicationError: Solver (ipopt) did not exit normally

This is very doable if you reformulate just a bit.如果您稍微重新表述一下,这是非常可行的。 Also, I don't think it is possible to return a tuple of 2 constraints like you are doing with that function, so you should break it up... it is clearer as well.另外,我认为不可能像您对 function 所做的那样返回 2 个约束的元组,因此您应该将其分解……它也更清晰。

You probably could access the upper/lower bound and use that within the constraint because they are fixed/constant with respect to the solver, but I think it is probably clearer to break out your min/max values as parameters.您可能可以访问上限/下限并在约束内使用它,因为它们相对于求解器是固定的/恒定的,但我认为将您的最小值/最大值分解为参数可能更清楚。 Some variation of this works.这个作品的一些变化。

(also, in the future, you are more likely to get better help/results if you post a fully minimal-reproducible example instead of just 1-line.) (此外,在未来,如果您发布一个完全最小的可重现示例而不是仅仅 1 行,您更有可能获得更好的帮助/结果。)

Code:代码:

import pyomo.environ as pyo

model = pyo.ConcreteModel()

model.I = pyo.Set(initialize=[1,2,3])

# power parameters...
model.min_inverter = pyo.Param(model.I, initialize={1:10, 2:15, 3:22})
model.max_inverter = pyo.Param(model.I, initialize={1:55, 2:45, 3:80})

# vars...
model.inverter_power = pyo.Var(model.I)
model.fcr_power = pyo.Var(model.I)

def fcr_inverter_min(model, i):
    return model.inverter_power[i] >= model.min_inverter[i] + model.fcr_power[i]
model.fcr_inverter_rule_min = pyo.Constraint(model.I, rule=fcr_inverter_min)

def fcr_inverter_max(model, i):
    return model.inverter_power[i] <= model.max_inverter[i] - model.fcr_power[i]
model.fcr_inverter_rule_max = pyo.Constraint(model.I, rule=fcr_inverter_max)

model.pprint()

Output: Output:

1 Set Declarations
    I : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

2 Param Declarations
    max_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
        Key : Value
          1 :    55
          2 :    45
          3 :    80
    min_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
        Key : Value
          1 :    10
          2 :    15
          3 :    22

2 Var Declarations
    fcr_power : Size=3, Index=I
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          1 :  None :  None :  None : False :  True :  Reals
          2 :  None :  None :  None : False :  True :  Reals
          3 :  None :  None :  None : False :  True :  Reals
    inverter_power : Size=3, Index=I
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          1 :  None :  None :  None : False :  True :  Reals
          2 :  None :  None :  None : False :  True :  Reals
          3 :  None :  None :  None : False :  True :  Reals

2 Constraint Declarations
    fcr_inverter_rule_max : Size=3, Index=I, Active=True
        Key : Lower : Body                                    : Upper : Active
          1 :  -Inf : inverter_power[1] - (55 - fcr_power[1]) :   0.0 :   True
          2 :  -Inf : inverter_power[2] - (45 - fcr_power[2]) :   0.0 :   True
          3 :  -Inf : inverter_power[3] - (80 - fcr_power[3]) :   0.0 :   True
    fcr_inverter_rule_min : Size=3, Index=I, Active=True
        Key : Lower : Body                                  : Upper : Active
          1 :  -Inf : 10 + fcr_power[1] - inverter_power[1] :   0.0 :   True
          2 :  -Inf : 15 + fcr_power[2] - inverter_power[2] :   0.0 :   True
          3 :  -Inf : 22 + fcr_power[3] - inverter_power[3] :   0.0 :   True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历具有上限/下限的struct向量中的一个变量 - Iterating through one variable in a vector of struct with lower/upper bound 在Pyomo中为约束提供上限和下限的问题 - Problems in providing upper and lower bounds to a constraint in Pyomo 非零 降低成本:下限或上限是否有效? - nonzero Reduced cost: Is lower or upper bound active? 在优化问题中指定下限和上限的技巧 - A trick for specifying a lower and upper bound in an optimization problem 如何在 Pyomo 的每次迭代中使用更新的变量值? - how can i use updated value of Variable at each iteration in Pyomo? 如何使用 pyomo 在目标 function 的总和范围内包含一个变量? - How to include a variable in the summation bound of a objective function with pyomo? 达到指定值时将新迭代附加到变量的 Pyomo 约束 - Pyomo constraint that appends new iteration to a variable when reaching specified values 如何将值数组添加到Google ortools中,而不是上下限? - How can i add an array of values to Google ortools versus a lower and upper bound? 是否会在循环的每次迭代中重新分配变量会影响性能? - Will reassigning a variable in every iteration of a loop affect performance? 运行贝叶斯优化器,当执行最大化函数时,“下限之一大于上限”。 发生错误 - Running the Bayesian optimizer, and when the maximize function is executed, "one of the lower bounds is greater than an upper bound." Error occurs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM