lymbot
lymbot

Reputation: 31

Distinguishing between linear and non-linear constraints in Pyomo

How can I distinguish between linear and non-linear constraints in Pyomo? Lets say they all got constructed with the constraint constructor and not with linear_constraint constructor.

Upvotes: 1

Views: 331

Answers (1)

jsiirola
jsiirola

Reputation: 2599

You can always ask any Pyomo expression what its polynomial degree is:

>>> 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

Constant expressions have degree 0, linear expressions have degree 1.

Note that polynomial_degree returns the current degree, so fixed variables are interpreted as constants when calculating the degree.

Upvotes: 3

Related Questions