Expression¶
Basic expression¶
PyOptInterface currently supports polynomial expressions with degree up to 2, including
quadratic expression
linear expression
constant expression
The expression can be expressed by arithmetic operations of variables and constants. For example, we can create a quadratic expression by adding two quadratic expressions, multiplying a linear expression with a constant expression, etc.
import pyoptinterface as poi
from pyoptinterface import highs
model = highs.Model()
x = model.add_variable()
# create a quadratic expression
expr1 = x * x + 2 * x + 1
# create a linear expression
expr2 = 2 * x + 1
# create a quadratic expression by adding two quadratic expressions
expr3 = x * expr2 + expr1
Running HiGHS 1.13.1 (git hash: 1d267d9): Copyright (c) 2026 under Apache 2.0 license terms
Using BLAS: OpenBLAS
Efficient expression construction¶
PyOptInterface provides a special class ExprBuilder to construct expressions efficiently. It is especially useful when we need to construct a large expression with many terms.
It supports the following in-place assignment operations:
+=: add a term to the expression-=: subtract a term from the expression*=: multiply the expression with a constant or another expression/=: divide the expression with a constant
For example, we can use ExprBuilder to construct the following expression efficiently:
N = 1000
x = [model.add_variable() for _ in range(N)]
def fast_expr():
expr = poi.ExprBuilder()
for i in range(N):
expr += 0.5 * x[i] * x[i]
expr -= x[i]
def slow_expr():
expr = 0
for i in range(N):
expr += 0.5 * x[i] * x[i]
expr -= x[i]
%time fast_expr()
CPU times: user 4.13 ms, sys: 16 μs, total: 4.15 ms
Wall time: 1.17 ms
%time slow_expr()
CPU times: user 104 ms, sys: 31 μs, total: 104 ms
Wall time: 25.9 ms
Pretty print expression¶
If the names of variables are specified, We can use the pprint method to print the expression in a human-readable format:
x = model.add_variable(name="x")
y = model.add_variable(name="y")
expr = x * x + 2 * x * y + y * y
model.pprint(expr)
'1*x*x+2*x*y+1*y*y'
Value of expression¶
We can use the get_value method to get the value of an expression after optimization:
expr = x*y + x*x
expr_value = model.get_value(expr)