Pyomo 构建拉格朗日量或选择约束的左侧

Pyomo Building a Lagrangian or Selecting left hand side of constraints

我有一个 pyomo 模型“m”,它有 4 个变量和几个约束(相等和不等),形式为:

最小 F(G1,G2,D1,D2) 英石 h=0 g<=0

然后我需要构建拉格朗日函数,它是这样的:

简单地说,lambda 和 mu 是约束的对偶。所以我需要 objective 函数 + dual1cons1 + dual2cons2 等等。

我完全不知道该怎么做。我得到的最接近的是:

Lagrange = m.objective #initialize the variable with the f(x)

for cons in m.component_objects(pyomo.core.base.constraint.Constraint): #iterates over the constraints
    print("\n->" + str(cons) + ":")
    if isinstance(cons, pyomo.core.base.constraint.SimpleConstraint): #selects whether it is an individual constraint
        # print(cons)
        print(expression_to_string(m.component(cons).expr)) #prints the expresion
    if isinstance(cons, pyomo.core.base.constraint.ConstraintList): #or a list of constraints
        for i in m.component(cons):
            Lagrange=Lagrange+m.component('LinLim')[i].expr
            # print(expression_to_string(m.component(cons)[i].expr)) #prints the expresion

print(expression_to_string(Lagrange))

然后,打印语句returns this:

(12Pgen[G1] + 20Pgen[G2] - (40Pdem[D1] + 35Pdem[D2] )) + (500*(teta[n1] - teta[n2]) - 100 <= 0.0) - (500*(teta[n1] - teta[n2]) + 100) <= 0.0 + (500*(teta [n1] - teta[n3]) - 100 <= 0.0) + (500*(teta[n1] - teta[n2]) - 100 <= 0.0) - (500*(teta[n1] - teta[n2] ) + 100) <= 0.0 + (500*(teta[n1] - teta[n3]) - 100 <= 0.0) - (500*(teta[n1] - teta[n3]) + 100) <= 0.0 + (500*(teta[n2] - teta[n3]) - 100 <= 0.0) - (500*(teta[n2] - teta[n3]) + 100) <= 0.0

我知道阅读是一场噩梦。关键是,它包括方程式 (==、>=、<=) 的运算符,而我只对方程式的左侧部分感兴趣。然后,此外,我还需要添加代表对偶(lambda 和 mu)的新变量。

Pyomo 约束具有 body 属性,uslacklslack 函数分别为您提供约束左侧、上松弛和下松弛的求和表达式. `body 属性是您要乘以 lambda 和 mu 的值。这是一个带有简单约束的示例

import pyomo.environ as pyo

m = pyo.ConcreteModel()

index = [1,2,3,4]
m.x = pyo.Var(index,domain = pyo.Binary,initialize = 0)

m.c1 = pyo.Constraint(expr=sum([m.x[i] for i in index])<=3)
print('Constraint 1: {}'.format(str(m.c1.body)))
# Constraint 1: x[1] + x[2] + x[3] + x[4]
m.fixed_lambda = pyo.Var(domain = pyo.NonNegativeReals)
m.lagrangian = pyo.Objective(expr = m.fixed_lambda * m.c1.body,sense=pyo.minimize)
print('Lagrangian expression: {}'.format(str(m.lagrangian.expr)))
# Lagrangian expression: fixed_lambda*(x[1] + x[2] + x[3] + x[4])