2

In addition to the question here I would like to know how to obtain the coefficients of an arbitrary polynomial constraint of a pyomo model. So, for

m= ConcreteModel()
m.x_1 = Var()
m.x_2 = Var()
m.x_3 = Var(within = Integers)
m.x_4 = Var(within = Integers)
m.c= Constraint(expr=2*m.x_1**2 + 5*m.x_1*m.x_2 + m.x_4 <= 2)

I would like to have

coeff[c] = [2, 5, 1].
Nat1
  • 83
  • 8

1 Answers1

0

To my knowledge, there is no easy way to do this without walking the expression tree for arbitrary polynomials (since you could have (x-3)^2+5x+6).

One approach could be to sympy-ify the pyomo expression and ask sympy for those values: How to extract all coefficients in sympy

The current implementation of differentiate actually makes use of sympy: https://github.com/Pyomo/pyomo/blob/4997726dd1f11bdb86589ff1c2f4badc654a69ad/pyomo/core/base/symbolic.py#L128

Qi Chen
  • 1,648
  • 1
  • 10
  • 19