区分 Pyomo 中的线性和非线性约束

Distinguishing between linear and non-linear constraints in Pyomo

如何区分 Pyomo 中的线性和非线性约束?可以说它们都是用约束构造函数构造的,而不是用 linear_constraint 构造函数构造的。

你可以随时询问任何 Pyomo 表达式的多项式次数:

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.x = Var()
>>> m.y = Var()
>>> m.z = Var()
>>> m.c = Constraint(expr=m.x**m.y + m.x*m.z + m.x >= 0)
>>> m.c.body.polynomial_degree()
None
>>> m.y.fix(3)
>>> m.c.body.polynomial_degree()
3
>>> m.y.fix(1)
>>> m.c.body.polynomial_degree()
2
>>> m.x.fix(1)
>>> m.c.body.polynomial_degree()
1
>>> m.z.fix(0)
>>> m.c.body.polynomial_degree()
0

常数表达式的阶数为 0,线性表达式的阶数为 1。

注意polynomial_degree returns 当前度数,所以固定变量在计算度数时被解释为常量。