The Pyomo optimization modeling framework for formulating, analyzing, and solving mathematical optimization problems
—
Essential modeling components for creating optimization problems in Pyomo. These components form the foundation of all Pyomo optimization models, providing the building blocks for variables, constraints, objectives, parameters, and model structure.
Base model classes for creating optimization problems with support for both abstract template models and concrete models with specific data.
class Model:
"""Base model class for optimization problems."""
def __init__(self, name=None): ...
def nvariables(self): ...
def nconstraints(self): ...
def nobjectives(self): ...
def compute_statistics(self): ...
statistics: object
solutions: object
config: object
class ConcreteModel(Model):
"""Model with concrete data values."""
def __init__(self, name=None): ...
class AbstractModel(Model):
"""Abstract model template requiring data instantiation."""
def __init__(self, name=None): ...
def create_instance(self, data=None, name=None, namespace=None, namespaces=None, profile_memory=None, report_timing=False): ...
def load(self, data, namespace=None): ...
class Block:
"""Block component for hierarchical modeling."""
def __init__(self, *args, **kwargs): ...
class ScalarBlock(Block):
"""Scalar block component."""
def __init__(self, *args, **kwargs): ...Decision variable components with domain support, bounds, and initialization options for linear and nonlinear optimization problems.
class Var:
"""Decision variable component."""
def __init__(self, *args, domain=None, bounds=None, initialize=None, within=None, units=None, **kwargs): ...
def set_value(self, value): ...
def fix(self, value=None): ...
def unfix(self): ...
def is_fixed(self): ...
@property
def bounds(self): ...
@property
def domain(self): ...
@domain.setter
def domain(self, value): ...
def has_lb(self): ...
def has_ub(self): ...
def setlb(self, val): ...
def setub(self, val): ...
@property
def lb(self): ...
@property
def ub(self): ...
@property
def lower(self): ...
@property
def upper(self): ...
@property
def stale(self): ...
def get_units(self): ...
class ScalarVar(Var):
"""Scalar variable component."""
def __init__(self, *args, **kwargs): ...
class VarList:
"""Variable list component for dynamic variable creation."""
def __init__(self, **kwargs): ...
def add(self): ...
class BooleanVar(Var):
"""Boolean variable component."""
def __init__(self, *args, **kwargs): ...
def get_associated_binary(self): ...
def associate_binary_var(self, binary_var): ...
@property
def stale(self): ...
@property
def domain(self): ...
class BooleanVarList:
"""Boolean variable list component."""
def __init__(self, **kwargs): ...
def add(self): ...
class ScalarBooleanVar(BooleanVar):
"""Scalar boolean variable component."""
def __init__(self, *args, **kwargs): ...Constraint components for defining equality and inequality constraints with support for indexed constraints and logical constraints.
class Constraint:
"""Constraint component for optimization problems."""
def __init__(self, *args, **kwargs): ...
def activate(self): ...
def deactivate(self): ...
def is_active(self): ...
def to_bounded_expression(self): ...
@property
def body(self): ...
@property
def lower(self): ...
@property
def upper(self): ...
@property
def equality(self): ...
def has_lb(self): ...
def has_ub(self): ...
def set_value(self, expr): ...
class ConstraintList:
"""Constraint list component for dynamic constraint creation."""
def __init__(self, **kwargs): ...
def add(self, expr): ...
class LogicalConstraint:
"""Logical constraint component for Boolean relationships."""
def __init__(self, *args, **kwargs): ...
class LogicalConstraintList:
"""Logical constraint list component."""
def __init__(self, **kwargs): ...
def add(self, expr): ...Objective function components supporting minimization and maximization with single and multiple objective capabilities.
class Objective:
"""Objective function component."""
def __init__(self, *args, **kwargs): ...
def activate(self): ...
def deactivate(self): ...
def is_active(self): ...
def is_minimizing(self): ...
def set_sense(self, sense): ...
@property
def sense(self): ...
@sense.setter
def sense(self, value): ...
class ObjectiveList:
"""Objective list component for multiple objectives."""
def __init__(self, **kwargs): ...
def add(self, expr): ...Set components for defining index sets, domains, and structured data organization with support for finite and infinite sets.
class Set:
"""Set component for model indexing."""
def __init__(self, *args, **kwargs): ...
def add(self, value): ...
def remove(self, value): ...
def __contains__(self, item): ...
class SetOf:
"""Set-of component for hierarchical sets."""
def __init__(self, *args, **kwargs): ...
class RangeSet(Set):
"""Range set component for continuous ranges."""
def __init__(self, *args, **kwargs): ...Parameter components for model data including mutable and immutable parameters with validation and initialization support.
class Param:
"""Parameter component for model data."""
def __init__(self, *args, mutable=False, initialize=None, domain=None, within=None, default=None, validate=None, units=None, **kwargs): ...
def set_value(self, value): ...
def is_mutable(self): ...
def clear(self): ...Expression components for algebraic expressions and intermediate calculations with automatic differentiation support.
class Expression:
"""Expression component for algebraic expressions."""
def __init__(self, *args, **kwargs): ...Specialized components for advanced modeling including special ordered sets, piecewise functions, and external function interfaces.
class SOSConstraint:
"""Special Ordered Set constraint component."""
def __init__(self, *args, **kwargs): ...
class Piecewise:
"""Piecewise linear function component."""
def __init__(self, *args, **kwargs): ...
class Connector:
"""Component connector for modular modeling."""
def __init__(self, *args, **kwargs): ...
class ExternalFunction:
"""External function interface component."""
def __init__(self, *args, **kwargs): ...
def Reference(reference, ctype=None):
"""Reference function for component aliasing."""
class Suffix:
"""Suffix component for solver annotations."""
def __init__(self, *args, **kwargs): ...Utilities for traversing, managing, and accessing model components with support for filtering and iteration patterns.
def active_components(block, ctype, sort_by_names=False, sort_by_keys=False):
"""
Iterate over active components of specified type.
Args:
block: Pyomo model or block
ctype: Component type filter
sort_by_names: Sort by component names (default False)
sort_by_keys: Sort by component keys (default False)
Returns:
Iterator over active components
"""
def components(block, ctype=None, active=None, sort_by_names=False, sort_by_keys=False):
"""
Iterate over all components of specified type.
Args:
block: Pyomo model or block
ctype: Component type filter (optional)
active: Filter by active status (optional)
sort_by_names: Sort by component names (default False)
sort_by_keys: Sort by component keys (default False)
Returns:
Iterator over components
"""
def active_components_data(block, ctype, sort_by_names=False, sort_by_keys=False):
"""
Iterate over active component data objects.
Args:
block: Pyomo model or block
ctype: Component type filter
sort_by_names: Sort by names (default False)
sort_by_keys: Sort by keys (default False)
Returns:
Iterator over component data objects
"""
def components_data(block, ctype=None, active=None, sort_by_names=False, sort_by_keys=False):
"""
Iterate over all component data objects.
Args:
block: Pyomo model or block
ctype: Component type filter (optional)
active: Filter by active status (optional)
sort_by_names: Sort by names (default False)
sort_by_keys: Sort by keys (default False)
Returns:
Iterator over component data objects
"""Helper functions and rule constructors for simplified component creation and validation.
def simple_set_rule():
"""Helper for creating simple set rules."""
def simple_constraint_rule():
"""Helper for creating simple constraint rules."""
def simple_constraintlist_rule():
"""Helper for creating simple constraint list rules."""
def simple_objective_rule():
"""Helper for creating simple objective rules."""
def simple_objectivelist_rule():
"""Helper for creating simple objective list rules."""
class BuildAction:
"""Component construction action helper."""
def __init__(self, *args, **kwargs): ...
class BuildCheck:
"""Component construction validation helper."""
def __init__(self, *args, **kwargs): ...Predefined domain sets and utility functions commonly used in optimization models.
# Global domain sets
Reals: Set
PositiveReals: Set
NonPositiveReals: Set
NegativeReals: Set
NonNegativeReals: Set
Integers: Set
PositiveIntegers: Set
NonPositiveIntegers: Set
NegativeIntegers: Set
NonNegativeIntegers: Set
Boolean: Set
Binary: Set
Any: Set
AnyWithNone: Set
EmptySet: Set
UnitInterval: Set
PercentFraction: Set
# Interval constructors
def RealInterval(lb, ub): ...
def IntegerInterval(lb, ub): ...
# Set types
class RealSet(Set): ...
class IntegerSet(Set): ...
class BooleanSet(Set): ...
# Utility functions
def prod(*args): ...
def quicksum(args): ...
def sum_product(*args): ...
def dot_product(*args): ...
def summation(component, *args, **kwargs): ...
def sequence(*args): ...
# Component utilities
def symbol_map_from_instance(instance): ...
def display(instance, filename=None, ostream=None): ...from enum import Flag, Enum
class SortComponents(Flag):
"""Component sorting options for iteration."""
UNSORTED = 0
ORDERED_INDICES = 2
SORTED_INDICES = 4
ALPHABETICAL = 8
class TraversalStrategy(Enum):
"""Model traversal strategy options."""
BreadthFirstSearch = 1
PrefixDepthFirstSearch = 2 # aliased as DepthFirstSearch
PostfixDepthFirstSearch = 3
DepthFirstSearch = 2 # alias for PrefixDepthFirstSearchfrom pyomo.environ import *
# Create concrete model
model = ConcreteModel()
# Add variables
model.x = Var(domain=NonNegativeReals)
model.y = Var(bounds=(0, 10))
# Add parameters
model.a = Param(initialize=2.5)
model.b = Param(initialize=1.0, mutable=True)
# Add constraints
model.con1 = Constraint(expr=model.x + model.y <= 10)
model.con2 = Constraint(expr=model.a * model.x >= model.b)
# Add objective
model.obj = Objective(expr=model.x + 2*model.y, sense=maximize)from pyomo.environ import *
model = ConcreteModel()
# Define index sets
model.I = Set(initialize=[1, 2, 3])
model.J = Set(initialize=['a', 'b', 'c'])
# Indexed variables
model.x = Var(model.I, model.J, domain=Binary)
# Indexed parameters
model.cost = Param(model.I, model.J, initialize=1.0)
# Indexed constraints
def constraint_rule(model, i, j):
return model.x[i, j] <= model.cost[i, j]
model.constraints = Constraint(model.I, model.J, rule=constraint_rule)Install with Tessl CLI
npx tessl i tessl/pypi-pyomo