lumix.core.model.LXModel

class lumix.core.model.LXModel(name)[source]

Main model builder with full type safety and IDE support.

Creates and manages optimization models with:

  • Variables (single or multi-indexed)

  • Constraints (linear, indexed, multi-model)

  • Objective function (linear or quadratic)

Examples:

# Simple model
model = LXModel("production_plan")
model.add_variable(production)
model.add_constraint(capacity_constraint)
model.maximize(
    LXLinearExpression().add_term(production, lambda p: p.selling_price)
)

# Type-safe model
model = LXModel[Product]("production_plan")
    .add_variable(production)
    .add_constraint(capacity_constraint)
    .maximize(
        LXLinearExpression()
        .add_term(production, lambda p: p.selling_price - p.cost)
    )
Parameters:

name (str)

__init__(name)[source]

Initialize model.

Parameters:

name (str) – Model name

Methods

__init__(name)

Initialize model.

add_constraint(constraint)

Add constraint with full type checking.

add_constraints(*constraints)

Add multiple constraints.

add_variable(var)

Add variable with full type checking.

add_variables(*variables)

Add multiple variable families.

get_constraint(name)

Get constraint by name.

get_variable(name)

Get variable family by name.

has_goals()

Check if model has any goal constraints.

maximize(expr)

Set objective to maximize.

minimize(expr)

Set objective to minimize.

populate_goal_deviations(solution)

Populate goal deviation values in the solution.

prepare_goal_programming()

Prepare model for goal programming by relaxing goal constraints.

set_goal_mode(mode)

Set goal programming mode.

summary()

Get model summary.

__init__(name)[source]

Initialize model.

Parameters:

name (str) – Model name

__deepcopy__(memo)[source]

Custom deepcopy that enables what-if analysis with ORM data sources.

This method orchestrates deep copying of the entire model including: 1. All variable families (with ORM data materialization) 2. All constraint families (with ORM data materialization) 3. Objective expression 4. Goal programming metadata

This is the central method that makes what-if analysis possible by creating independent copies of models that can be modified without affecting the original.

Parameters:

memo – Dictionary for tracking circular references during deepcopy

Returns:

Deep copy of this model with all ORM dependencies resolved

Note

After copying, all variables and constraints will have their ORM sessions detached and data materialized. The copy is completely independent and safe for serialization/pickling.

Example

>>> original_model = build_model_with_orm(session)
>>> modified_model = deepcopy(original_model)
>>> # modified_model can now be changed without affecting original_model
__getstate__()[source]

Support for pickle protocol - detach ORM sessions before pickling.

Returns:

Dictionary of instance state safe for pickling

__setstate__(state)[source]

Support for pickle protocol - restore from pickled state.

Parameters:

state – Dictionary of instance state from pickling

add_variable(var)[source]

Add variable with full type checking.

Parameters:

var (LXVariable) – Variable to add

Return type:

Self

Returns:

Self for chaining

add_variables(*variables)[source]

Add multiple variable families.

Parameters:

*variables (LXVariable) – Variables to add

Return type:

Self

Returns:

Self for chaining

add_constraint(constraint)[source]

Add constraint with full type checking.

Parameters:

constraint (LXConstraint) – Constraint to add

Return type:

Self

Returns:

Self for chaining

add_constraints(*constraints)[source]

Add multiple constraints.

Parameters:

*constraints (LXConstraint) – Constraints to add

Return type:

Self

Returns:

Self for chaining

minimize(expr)[source]

Set objective to minimize.

Parameters:

expr (LXLinearExpression | LXQuadraticExpression) – Objective expression (linear or quadratic)

Return type:

Self

Returns:

Self for chaining

maximize(expr)[source]

Set objective to maximize.

Parameters:

expr (LXLinearExpression | LXQuadraticExpression) – Objective expression (linear or quadratic)

Return type:

Self

Returns:

Self for chaining

get_variable(name)[source]

Get variable family by name.

Parameters:

name (str) – Variable name

Return type:

Optional[LXVariable]

Returns:

LXVariable if found, None otherwise

get_constraint(name)[source]

Get constraint by name.

Parameters:

name (str) – Constraint name

Return type:

Optional[LXConstraint]

Returns:

LXConstraint if found, None otherwise

set_goal_mode(mode)[source]

Set goal programming mode.

Parameters:

mode (str) – Goal programming mode (“weighted” or “sequential”)

Return type:

Self

Returns:

Self for chaining

Raises:

ValueError – If mode is not “weighted” or “sequential”

Example

>>> model.set_goal_mode("weighted")
>>> # Solve with weighted objectives (single solve)
>>> model.set_goal_mode("sequential")
>>> # Solve lexicographically (multiple solves)
prepare_goal_programming()[source]

Prepare model for goal programming by relaxing goal constraints.

This method: 1. Identifies constraints marked as goals (via .as_goal()) 2. Relaxes them by adding deviation variables 3. Builds the appropriate objective function based on mode 4. Adds deviation variables to the model 5. Replaces goal constraints with relaxed versions

This is automatically called by the solver, but can be called manually for inspection or debugging.

Return type:

Self

Returns:

Self for chaining

Example

>>> model.set_goal_mode("weighted")
>>> model.prepare_goal_programming()
>>> # Model now has deviation variables and goal objective
has_goals()[source]

Check if model has any goal constraints.

Return type:

bool

Returns:

True if at least one constraint is marked as a goal

populate_goal_deviations(solution)[source]

Populate goal deviation values in the solution.

Extracts deviation variable values from the solution and organizes them by goal name for easy access via solution.get_goal_deviations().

This method is automatically called after solving if the model has goals.

Parameters:

solution (LXSolution) – Solution object to populate

Return type:

LXSolution

Returns:

The solution object with goal_deviations populated

summary()[source]

Get model summary.

Return type:

str

Returns:

String summary of model