[英]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 行,您更有可能获得更好的帮助/结果。)
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()
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.