CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyomo

The Pyomo optimization modeling framework for formulating, analyzing, and solving mathematical optimization problems

Pending
Overview
Eval results
Files

domain-sets.mddocs/

Domain Sets and Intervals

Predefined domain sets and interval constructors for variable bounds and parameter domains. Includes real numbers, integers, booleans, and custom intervals for comprehensive domain specification in optimization models.

Capabilities

Real Number Domains

Predefined sets for real number domains with various sign constraints for continuous optimization variables.

Reals: Set
"""Set of all real numbers (-∞, +∞)."""

PositiveReals: Set  
"""Set of positive real numbers (0, +∞)."""

NonPositiveReals: Set
"""Set of non-positive real numbers (-∞, 0]."""

NegativeReals: Set
"""Set of negative real numbers (-∞, 0)."""

NonNegativeReals: Set
"""Set of non-negative real numbers [0, +∞)."""

Integer Domains

Predefined sets for integer domains with various sign constraints for discrete optimization variables.

Integers: Set
"""Set of all integers."""

PositiveIntegers: Set
"""Set of positive integers (1, 2, 3, ...)."""

NonPositiveIntegers: Set  
"""Set of non-positive integers (..., -2, -1, 0)."""

NegativeIntegers: Set
"""Set of negative integers (..., -3, -2, -1)."""

NonNegativeIntegers: Set
"""Set of non-negative integers (0, 1, 2, 3, ...)."""

Boolean and Binary Domains

Domains for logical and binary variables in mixed-integer programming problems.

Boolean: Set
"""Boolean domain {0, 1} (currently same as Binary - TODO: Convert to {True, False})."""

Binary: Set  
"""Binary domain {0, 1}."""

Special Domains

Special-purpose domains for specific modeling requirements and edge cases.

Any: Set
"""Domain allowing any value (unconstrained)."""

AnyWithNone: Set
"""Domain allowing any value including None."""

EmptySet: Set
"""Empty set with no valid values."""

Numeric Intervals

Predefined intervals for common numeric ranges in optimization problems.

UnitInterval: Set
"""Unit interval [0, 1]."""

PercentFraction: Set
"""Percentage fraction interval [0, 1]."""

Interval Constructors

Functions and classes for creating custom intervals with specified bounds for real and integer domains.

class RealInterval(RealSet):
    """
    DEPRECATED: Create real number interval.
    
    .. deprecated:: 5.7
        Use RangeSet(lower, upper, 0) instead.
    
    Args:
        lb (float, optional): Lower bound (None for -∞)
        ub (float, optional): Upper bound (None for +∞)
        
    Returns:
        Set: Real interval [lb, ub]
    """

class IntegerInterval(IntegerSet):
    """
    DEPRECATED: Create integer interval.
    
    .. deprecated:: 5.7
        Use RangeSet(lower, upper, 1) instead.
    
    Args:
        lb (int, optional): Lower bound (None for -∞)
        ub (int, optional): Upper bound (None for +∞)
        
    Returns:
        Set: Integer interval [lb, ub]
    """

class RangeSet(Set):
    """
    Create custom numeric range sets (modern replacement for interval constructors).
    
    Args:
        ranges: Numeric ranges or bounds
        step: Step size (0 for continuous, 1 for integer)
        
    Returns:
        Set: Custom range set
    """

Optimization Direction Constants

Constants for specifying optimization direction in objective functions.

minimize: int
"""Minimization sense constant."""

maximize: int  
"""Maximization sense constant."""

Usage Examples

Variable Domain Specification

from pyomo.environ import *

model = ConcreteModel()

# Real variables with different domains
model.x1 = Var(domain=Reals)                    # Any real number
model.x2 = Var(domain=NonNegativeReals)         # x >= 0
model.x3 = Var(domain=PositiveReals)            # x > 0
model.x4 = Var(domain=UnitInterval)             # 0 <= x <= 1

# Integer variables
model.y1 = Var(domain=Integers)                 # Any integer
model.y2 = Var(domain=NonNegativeIntegers)      # y >= 0 integer
model.y3 = Var(domain=PositiveIntegers)         # y > 0 integer

# Binary and boolean variables
model.z1 = Var(domain=Binary)                   # {0, 1}
model.z2 = Var(domain=Boolean)                  # {True, False}

Custom Intervals

from pyomo.environ import *

model = ConcreteModel()

# Custom real intervals
model.x1 = Var(domain=RealInterval(-10, 10))    # [-10, 10]
model.x2 = Var(domain=RealInterval(0, None))    # [0, +∞)
model.x3 = Var(domain=RealInterval(None, 100))  # (-∞, 100]

# Custom integer intervals  
model.y1 = Var(domain=IntegerInterval(1, 100))  # {1, 2, ..., 100}
model.y2 = Var(domain=IntegerInterval(-5, 5))   # {-5, -4, ..., 5}

Indexed Variables with Domains

from pyomo.environ import *

model = ConcreteModel()
model.I = Set(initialize=[1, 2, 3, 4, 5])

# Different domains for different indices
def domain_rule(model, i):
    if i <= 2:
        return NonNegativeReals
    elif i <= 4:
        return Binary
    else:
        return Integers

model.x = Var(model.I, domain=domain_rule)

# Alternative: specify domain per variable
model.y = Var(model.I)
for i in model.I:
    if i <= 2:
        model.y[i].domain = NonNegativeReals
    else:
        model.y[i].domain = Binary

Parameter Domains

from pyomo.environ import *

model = ConcreteModel()

# Parameters with domain validation
model.cost = Param(domain=NonNegativeReals, initialize=10.5)
model.capacity = Param(domain=PositiveIntegers, initialize=100)
model.enabled = Param(domain=Boolean, initialize=True)

# Parameters with custom intervals
model.temperature = Param(domain=RealInterval(-273.15, None))  # Above absolute zero
model.priority = Param(domain=IntegerInterval(1, 10))          # Priority scale 1-10

Domain Validation Example

from pyomo.environ import *

model = ConcreteModel()

# Create variable with domain constraint
model.x = Var(domain=UnitInterval)

# This will work
model.x.set_value(0.5)

# This would raise an error due to domain violation
try:
    model.x.set_value(1.5)  # Outside [0,1]
except ValueError as e:
    print(f"Domain violation: {e}")

# Check if value is in domain
print(f"0.5 in UnitInterval: {0.5 in UnitInterval}")
print(f"1.5 in UnitInterval: {1.5 in UnitInterval}")

Optimization Sense Usage

from pyomo.environ import *

model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)

# Minimization objective
model.min_obj = Objective(
    expr=model.x + model.y,
    sense=minimize
)

# Maximization objective (deactivate first objective)
model.min_obj.deactivate()
model.max_obj = Objective(
    expr=model.x + model.y,
    sense=maximize
)

Domain Checking and Validation

from pyomo.environ import *

# Check domain membership
values_to_check = [-1, 0, 0.5, 1, 2]

for val in values_to_check:
    print(f"Value {val}:")
    print(f"  In NonNegativeReals: {val in NonNegativeReals}")
    print(f"  In UnitInterval: {val in UnitInterval}")
    print(f"  In PositiveReals: {val in PositiveReals}")
    print(f"  In Binary: {val in Binary}")

Install with Tessl CLI

npx tessl i tessl/pypi-pyomo

docs

advanced-extensions.md

core-modeling.md

dae.md

data-management.md

domain-sets.md

gdp.md

index.md

mathematical-functions.md

mpec.md

optimization-interface.md

tile.json