lumix.analysis.whatif.LXWhatIfAnalyzer¶
- class lumix.analysis.whatif.LXWhatIfAnalyzer(model, optimizer, baseline_solution=None)[source]¶
Interactive what-if analysis for optimization models.
Allows quick exploration of parameter changes and their impact on the optimal solution. Useful for understanding trade-offs and identifying opportunities for improvement.
Examples
Create analyzer and explore changes:
analyzer = LXWhatIfAnalyzer(model, optimizer) # Get baseline solution baseline = analyzer.get_baseline_solution() # What if we increase capacity? result = analyzer.increase_constraint_rhs("capacity", by=200.0) print(f"Increasing capacity by 200 would improve profit by ${result.delta_objective:,.2f}") # What if we relax minimum production? result = analyzer.relax_constraint("min_production", by_percent=0.5) print(f"Relaxing min production by 50% would change objective by {result.delta_percentage:.1f}%") # Compare multiple changes results = analyzer.compare_changes([ ("capacity", "increase", 100), ("capacity", "increase", 200), ("capacity", "increase", 300), ]) # Find bottlenecks bottlenecks = analyzer.find_bottlenecks(top_n=5) for name, improvement in bottlenecks: print(f"{name}: relaxing by 1 unit would improve objective by ${improvement:.2f}")
- Parameters:
model (LXModel[TModel])
optimizer (LXOptimizer[TModel])
baseline_solution (Optional[LXSolution[TModel]])
- __init__(model, optimizer, baseline_solution=None)[source]¶
Initialize what-if analyzer.
- Parameters:
optimizer (
LXOptimizer[TypeVar(TModel)]) – Optimizer to use for solvingbaseline_solution (
Optional[LXSolution[TypeVar(TModel)]]) – Pre-computed baseline solution (optional)
Methods
__init__(model, optimizer[, baseline_solution])Initialize what-if analyzer.
compare_changes(changes[, constraint_type])Compare multiple what-if changes.
decrease_constraint_rhs(constraint_name[, ...])Analyze impact of decreasing constraint RHS.
find_bottlenecks([test_amount, top_n])Find bottleneck constraints by testing small relaxations.
get_baseline_solution([recompute])Get baseline solution (caches result).
increase_constraint_rhs(constraint_name[, ...])Analyze impact of increasing constraint RHS.
modify_variable_bound(variable_name[, ...])Analyze impact of changing variable bounds.
relax_constraint(constraint_name[, by, ...])Relax a constraint (increase RHS for LE, decrease for GE).
sensitivity_range(constraint_name, ...[, ...])Analyze objective sensitivity across a range of constraint RHS values.
tighten_constraint(constraint_name[, by, ...])Tighten a constraint (decrease RHS for LE, increase for GE).
- __init__(model, optimizer, baseline_solution=None)[source]¶
Initialize what-if analyzer.
- Parameters:
optimizer (
LXOptimizer[TypeVar(TModel)]) – Optimizer to use for solvingbaseline_solution (
Optional[LXSolution[TypeVar(TModel)]]) – Pre-computed baseline solution (optional)
- get_baseline_solution(recompute=False)[source]¶
Get baseline solution (caches result).
- Parameters:
recompute (
bool) – Force recomputation of baseline- Return type:
LXSolution[TypeVar(TModel)]- Returns:
Baseline solution
- increase_constraint_rhs(constraint_name, by=None, by_percent=None, to=None)[source]¶
Analyze impact of increasing constraint RHS.
- Parameters:
- Return type:
LXWhatIfResult[TypeVar(TModel)]- Returns:
What-if analysis result
Examples
# Increase capacity by 100 units result = analyzer.increase_constraint_rhs(“capacity”, by=100)
# Increase capacity by 20% result = analyzer.increase_constraint_rhs(“capacity”, by_percent=0.2)
# Set capacity to 1500 result = analyzer.increase_constraint_rhs(“capacity”, to=1500)
- decrease_constraint_rhs(constraint_name, by=None, by_percent=None, to=None)[source]¶
Analyze impact of decreasing constraint RHS.
- relax_constraint(constraint_name, by=None, by_percent=None)[source]¶
Relax a constraint (increase RHS for LE, decrease for GE).
This is a convenience method that automatically determines the direction based on constraint type.
- Parameters:
- Return type:
LXWhatIfResult[TypeVar(TModel)]- Returns:
What-if analysis result
Examples
# Relax capacity constraint by 100 units result = analyzer.relax_constraint(“capacity”, by=100)
# Relax minimum production by 20% result = analyzer.relax_constraint(“min_production”, by_percent=0.2)
- tighten_constraint(constraint_name, by=None, by_percent=None)[source]¶
Tighten a constraint (decrease RHS for LE, increase for GE).
- modify_variable_bound(variable_name, lower=None, upper=None)[source]¶
Analyze impact of changing variable bounds.
- Parameters:
- Return type:
LXWhatIfResult[TypeVar(TModel)]- Returns:
What-if analysis result
Examples
# Increase minimum production to 100 result = analyzer.modify_variable_bound(“production”, lower=100)
# Set production range to [50, 500] result = analyzer.modify_variable_bound(“production”, lower=50, upper=500)
- compare_changes(changes, constraint_type='rhs')[source]¶
Compare multiple what-if changes.
- Parameters:
- Return type:
List[LXWhatIfResult[TypeVar(TModel)]]- Returns:
List of what-if results
Examples
Compare different capacity increases:
results = analyzer.compare_changes([ ("capacity", "increase", 100), ("capacity", "increase", 200), ("capacity", "increase", 300), ]) for result in results: print(f"{result.description}: Δ = ${result.delta_objective:,.2f}")
- find_bottlenecks(test_amount=1.0, top_n=10)[source]¶
Find bottleneck constraints by testing small relaxations.
Tests relaxing each constraint by a small amount and measures the impact on the objective function.
- Parameters:
- Return type:
- Returns:
List of (constraint_name, improvement) tuples sorted by improvement
Examples
Find top bottlenecks:
bottlenecks = analyzer.find_bottlenecks(test_amount=1.0, top_n=5) print("Top 5 bottlenecks:") for name, improvement in bottlenecks: print(f" {name}: +${improvement:.2f} per unit relaxation")
- sensitivity_range(constraint_name, min_value, max_value, num_points=10)[source]¶
Analyze objective sensitivity across a range of constraint RHS values.
- Parameters:
- Return type:
- Returns:
List of (rhs_value, objective_value) tuples
Examples
# Analyze sensitivity to capacity from 800 to 1200 sensitivity = analyzer.sensitivity_range(“capacity”, 800, 1200, num_points=20)
- for rhs, obj in sensitivity:
print(f”Capacity = {rhs:6.1f} → Objective = ${obj:,.2f}”)