The Pyomo optimization modeling framework for formulating, analyzing, and solving mathematical optimization problems
—
Built-in mathematical functions and expression utilities for constructing complex objective functions and constraints. Includes trigonometric, logarithmic, logical operations, and symbolic manipulation capabilities for nonlinear optimization problems.
Standard mathematical functions for nonlinear expressions including logarithmic, exponential, and trigonometric functions.
def log(x):
"""
Natural logarithm function.
Args:
x: Expression or numeric value
Returns:
Expression: log(x)
"""
def log10(x):
"""
Base-10 logarithm function.
Args:
x: Expression or numeric value
Returns:
Expression: log10(x)
"""
def exp(x):
"""
Exponential function.
Args:
x: Expression or numeric value
Returns:
Expression: exp(x)
"""
def sqrt(x):
"""
Square root function.
Args:
x: Expression or numeric value
Returns:
Expression: sqrt(x)
"""
def ceil(x):
"""
Ceiling function.
Args:
x: Expression or numeric value
Returns:
Expression: ceil(x)
"""
def floor(x):
"""
Floor function.
Args:
x: Expression or numeric value
Returns:
Expression: floor(x)
"""Trigonometric and hyperbolic functions for trigonometric optimization problems and periodic constraints.
def sin(x):
"""
Sine function.
Args:
x: Expression or numeric value (radians)
Returns:
Expression: sin(x)
"""
def cos(x):
"""
Cosine function.
Args:
x: Expression or numeric value (radians)
Returns:
Expression: cos(x)
"""
def tan(x):
"""
Tangent function.
Args:
x: Expression or numeric value (radians)
Returns:
Expression: tan(x)
"""
def sinh(x):
"""
Hyperbolic sine function.
Args:
x: Expression or numeric value
Returns:
Expression: sinh(x)
"""
def cosh(x):
"""
Hyperbolic cosine function.
Args:
x: Expression or numeric value
Returns:
Expression: cosh(x)
"""
def tanh(x):
"""
Hyperbolic tangent function.
Args:
x: Expression or numeric value
Returns:
Expression: tanh(x)
"""
def asin(x):
"""
Inverse sine function.
Args:
x: Expression or numeric value
Returns:
Expression: asin(x)
"""
def acos(x):
"""
Inverse cosine function.
Args:
x: Expression or numeric value
Returns:
Expression: acos(x)
"""
def atan(x):
"""
Inverse tangent function.
Args:
x: Expression or numeric value
Returns:
Expression: atan(x)
"""
def asinh(x):
"""
Inverse hyperbolic sine function.
Args:
x: Expression or numeric value
Returns:
Expression: asinh(x)
"""
def acosh(x):
"""
Inverse hyperbolic cosine function.
Args:
x: Expression or numeric value
Returns:
Expression: acosh(x)
"""
def atanh(x):
"""
Inverse hyperbolic tangent function.
Args:
x: Expression or numeric value
Returns:
Expression: atanh(x)
"""Logical operators and conditional expressions for constraint programming and logical modeling.
def land(*args):
"""
Logical AND operation.
Args:
*args: Boolean expressions or values
Returns:
Expression: Logical AND of arguments
"""
def lor(*args):
"""
Logical OR operation.
Args:
*args: Boolean expressions or values
Returns:
Expression: Logical OR of arguments
"""
def lnot(arg):
"""
Logical NOT operation.
Args:
arg: Boolean expression or value
Returns:
Expression: Logical NOT of argument
"""
def xor(*args):
"""
Logical XOR operation.
Args:
*args: Boolean expressions or values
Returns:
Expression: Logical XOR of arguments
"""
def implies(a, b):
"""
Logical implication operation.
Args:
a: Boolean expression (antecedent)
b: Boolean expression (consequent)
Returns:
Expression: a implies b
"""
def equivalent(*args):
"""
Logical equivalence operation.
Args:
*args: Boolean expressions
Returns:
Expression: All arguments are equivalent
"""
def Expr_if(condition, then_expr, else_expr):
"""
Conditional expression (ternary operator).
Args:
condition: Boolean condition
then_expr: Expression if condition is true
else_expr: Expression if condition is false
Returns:
Expression: Conditional expression
"""Helper functions for creating complex logical constraints and cardinality constraints.
def exactly(n, *args):
"""
Exactly n of the arguments must be true.
Args:
n (int): Required number of true arguments
*args: Boolean expressions
Returns:
Expression: Exactly n constraint
"""
def atleast(n, *args):
"""
At least n of the arguments must be true.
Args:
n (int): Minimum number of true arguments
*args: Boolean expressions
Returns:
Expression: At least n constraint
"""
def atmost(n, *args):
"""
At most n of the arguments must be true.
Args:
n (int): Maximum number of true arguments
*args: Boolean expressions
Returns:
Expression: At most n constraint
"""
def all_different(*args):
"""
All arguments must have different values.
Args:
*args: Variable expressions
Returns:
Expression: All different constraint
"""
def count_if(condition, *args):
"""
Count how many arguments satisfy the condition.
Args:
condition: Boolean condition function
*args: Expressions to evaluate
Returns:
Expression: Count expression
"""
def inequality(body=None, lower=None, upper=None):
"""
Create inequality expression.
Args:
body: Main expression
lower: Lower bound
upper: Upper bound
Returns:
Expression: Inequality constraint
"""Utilities for analyzing expression properties, extracting values, and symbolic manipulation.
def value(expr, exception=True):
"""
Extract numeric value from expression.
Args:
expr: Pyomo expression or component
exception (bool): Raise exception if cannot evaluate
Returns:
float: Numeric value of expression
"""
def is_constant(expr):
"""
Check if expression is constant.
Args:
expr: Expression to check
Returns:
bool: True if expression is constant
"""
def is_fixed(expr):
"""
Check if expression has fixed value.
Args:
expr: Expression to check
Returns:
bool: True if expression is fixed
"""
def is_variable_type(expr):
"""
Check if expression is a variable type.
Args:
expr: Expression to check
Returns:
bool: True if expression is variable type
"""
def is_potentially_variable(expr):
"""
Check if expression could contain variables.
Args:
expr: Expression to check
Returns:
bool: True if potentially variable
"""
def polynomial_degree(expr):
"""
Determine polynomial degree of expression.
Args:
expr: Expression to analyze
Returns:
int or None: Polynomial degree, None if not polynomial
"""
def differentiate(expr, wrt, nth=1):
"""
Symbolic differentiation of expression.
Args:
expr: Expression to differentiate
wrt: Variable to differentiate with respect to
nth (int): Order of differentiation
Returns:
Expression: Derivative expression
"""
def taylor_series_expansion(expr, point, variables, order=1):
"""
Taylor series expansion of expression.
Args:
expr: Expression to expand
point: Expansion point
variables: Variables for expansion
order (int): Order of expansion
Returns:
Expression: Taylor series approximation
"""Optimized functions for summation, product, and other aggregation operations over indexed expressions.
def quicksum(args, start=0, linear=None):
"""
Efficient summation of expressions.
Args:
args: Iterable of expressions to sum
start: Initial value for the sum (default 0)
linear: DEPRECATED parameter for linear expressions
Returns:
Expression: Sum of expressions
"""
def prod(args):
"""
Product of expressions.
Args:
args: Iterable of expressions to multiply
Returns:
Expression: Product of expressions
"""
def sum_product(*args):
"""
Generalized dot product operation.
Args:
*args: Sequences of expressions to multiply and sum
Returns:
Expression: Sum of products
"""
def dot_product(a, b):
"""
Dot product of two sequences.
Args:
a: First sequence of expressions
b: Second sequence of expressions
Returns:
Expression: Dot product
"""
def summation(a, b):
"""
Alias for sum_product.
Args:
a: First sequence of expressions
b: Second sequence of expressions
Returns:
Expression: Sum of products
"""
def sequence(*args):
"""
Create arithmetic progression.
Args:
*args: Start, stop, step parameters
Returns:
Generator: Arithmetic sequence
"""Utilities for displaying and debugging model structure and expression content.
def display(model, ostream=None):
"""
Display model structure and values.
Args:
model: Pyomo model or component
ostream: Output stream (default: stdout)
"""from pyomo.environ import *
model = ConcreteModel()
model.x = Var(bounds=(0.1, 10))
model.y = Var(bounds=(0.1, 10))
# Nonlinear objective with mathematical functions
model.obj = Objective(
expr=log(model.x) + sin(model.y) + exp(model.x * model.y),
sense=minimize
)
# Nonlinear constraints
model.con1 = Constraint(expr=sqrt(model.x**2 + model.y**2) <= 5)
model.con2 = Constraint(expr=cos(model.x) + sin(model.y) >= 0.5)from pyomo.environ import *
model = ConcreteModel()
model.x = Var([1, 2, 3], domain=Binary)
# At least 2 variables must be selected
model.atleast_con = Constraint(
expr=atleast(2, model.x[1], model.x[2], model.x[3])
)
# Exactly one variable must be selected
model.exactly_con = Constraint(
expr=exactly(1, model.x[1], model.x[2], model.x[3])
)
# Implication constraint
model.y = Var(domain=Binary)
model.implication = Constraint(
expr=implies(model.x[1], model.y)
)from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=Binary)
# Conditional objective based on binary variable
model.obj = Objective(
expr=Expr_if(
model.y == 1,
then_expr=model.x**2, # Quadratic if y=1
else_expr=model.x # Linear if y=0
),
sense=minimize
)from pyomo.environ import *
model = ConcreteModel()
model.x = Var(initialize=2.5)
model.y = Var(initialize=1.0)
expr = model.x**2 + 2*model.x*model.y + 3
# Check expression properties
print(f"Is constant: {is_constant(expr)}")
print(f"Polynomial degree: {polynomial_degree(expr)}")
print(f"Current value: {value(expr)}")
# Fix variables and re-evaluate
model.x.fix()
model.y.fix()
print(f"Value after fixing: {value(expr)}")Install with Tessl CLI
npx tessl i tessl/pypi-pyomo