lumix.analysis.sensitivity.LXSensitivityAnalyzer¶
- class lumix.analysis.sensitivity.LXSensitivityAnalyzer(model, solution)[source]¶
Sensitivity analysis for optimization models.
Analyzes how changes in parameters affect the optimal solution, including:
Shadow prices (dual values) for constraints
Reduced costs for variables
Binding constraints identification
Sensitivity ranges (when available from solver)
Examples
Create analyzer and analyze sensitivity:
analyzer = LXSensitivityAnalyzer(model, solution) # Analyze variable sensitivity var_sensitivity = analyzer.analyze_variable("production") print(f"Reduced cost: {var_sensitivity.reduced_cost}") # Analyze constraint sensitivity const_sensitivity = analyzer.analyze_constraint("capacity") print(f"Shadow price: {const_sensitivity.shadow_price}") # Get binding constraints binding = analyzer.get_binding_constraints() for name, sensitivity in binding.items(): print(f"{name}: shadow price = {sensitivity.shadow_price}") # Generate full report print(analyzer.generate_report()) # Get most sensitive parameters sensitive_constraints = analyzer.get_most_sensitive_constraints(top_n=5)
- Parameters:
model (LXModel[TModel])
solution (LXSolution[TModel])
- __init__(model, solution)[source]¶
Initialize sensitivity analyzer.
- Parameters:
solution (
LXSolution[TypeVar(TModel)]) – Solution to analyze
Methods
__init__(model, solution)Initialize sensitivity analyzer.
Analyze all constraints in model.
Analyze all variables in solution.
analyze_constraint(constraint_name)Analyze sensitivity of a constraint.
analyze_variable(var_name)Analyze sensitivity of a variable.
generate_report([include_variables, ...])Generate comprehensive sensitivity analysis report.
Generate brief sensitivity summary.
get_binding_constraints([threshold])Get all binding (tight) constraints.
get_most_sensitive_constraints([top_n])Get constraints with highest shadow prices (most valuable to relax).
get_most_sensitive_variables([top_n])Get variables with highest reduced costs.
get_non_basic_variables([threshold])Get all non-basic variables (with non-zero reduced costs).
identify_bottlenecks([shadow_price_threshold])Identify bottleneck constraints (binding with high shadow prices).
Create interactive visualization for sensitivity analysis.
- __init__(model, solution)[source]¶
Initialize sensitivity analyzer.
- Parameters:
solution (
LXSolution[TypeVar(TModel)]) – Solution to analyze
- analyze_variable(var_name)[source]¶
Analyze sensitivity of a variable.
- Parameters:
var_name (
str) – Variable name- Return type:
- Returns:
Variable sensitivity information
- Raises:
ValueError – If variable not found in solution
- analyze_constraint(constraint_name)[source]¶
Analyze sensitivity of a constraint.
- Parameters:
constraint_name (
str) – Constraint name- Return type:
- Returns:
Constraint sensitivity information
- analyze_all_variables()[source]¶
Analyze all variables in solution.
- Return type:
- Returns:
Dictionary mapping variable names to sensitivity information
- analyze_all_constraints()[source]¶
Analyze all constraints in model.
- Return type:
- Returns:
Dictionary mapping constraint names to sensitivity information
- get_binding_constraints(threshold=1e-06)[source]¶
Get all binding (tight) constraints.
A constraint is binding if its shadow price is non-zero.
- Parameters:
threshold (
float) – Threshold for considering shadow price as non-zero- Return type:
- Returns:
Dictionary of binding constraints
Examples
Get all binding constraints:
binding = analyzer.get_binding_constraints() for name, sens in binding.items(): print(f"{name} is binding with shadow price {sens.shadow_price}")
- get_non_basic_variables(threshold=1e-06)[source]¶
Get all non-basic variables (with non-zero reduced costs).
- Parameters:
threshold (
float) – Threshold for considering reduced cost as non-zero- Return type:
- Returns:
Dictionary of non-basic variables
- get_most_sensitive_constraints(top_n=10)[source]¶
Get constraints with highest shadow prices (most valuable to relax).
- Parameters:
top_n (
int) – Number of constraints to return- Return type:
- Returns:
List of (name, sensitivity) tuples sorted by shadow price magnitude
Examples
Get most sensitive constraints:
top_constraints = analyzer.get_most_sensitive_constraints(top_n=5) for name, sens in top_constraints: print(f"{name}: ${sens.shadow_price:.2f} per unit relaxation")
- get_most_sensitive_variables(top_n=10)[source]¶
Get variables with highest reduced costs.
- Parameters:
top_n (
int) – Number of variables to return- Return type:
- Returns:
List of (name, sensitivity) tuples sorted by reduced cost magnitude
- identify_bottlenecks(shadow_price_threshold=0.01)[source]¶
Identify bottleneck constraints (binding with high shadow prices).
- Parameters:
shadow_price_threshold (
float) – Minimum shadow price to consider- Return type:
- Returns:
List of bottleneck constraint names
Examples
Identify bottlenecks:
bottlenecks = analyzer.identify_bottlenecks() print(f"Found {len(bottlenecks)} bottlenecks:") for name in bottlenecks: print(f" - {name}")
- generate_report(include_variables=True, include_constraints=True, include_binding_only=False, top_n=None)[source]¶
Generate comprehensive sensitivity analysis report.
- Parameters:
- Return type:
- Returns:
Formatted sensitivity report
Examples
Full report:
print(analyzer.generate_report())
Only binding constraints:
print(analyzer.generate_report( include_variables=False, include_binding_only=True ))
Top 10 most sensitive:
print(analyzer.generate_report(top_n=10))
- generate_summary()[source]¶
Generate brief sensitivity summary.
- Return type:
- Returns:
Brief summary of key sensitivity metrics
- visualize()[source]¶
Create interactive visualization for sensitivity analysis.
Requires the visualization extra: pip install lumix-opt[viz]
- Return type:
LXSensitivityPlot[TypeVar(TModel)]- Returns:
LXSensitivityPlot instance
Examples
Basic usage:
analyzer = LXSensitivityAnalyzer(model, solution) analyzer.visualize().show()
Tornado chart:
analyzer.visualize().plot_tornado(top_n=15).show()
Export to HTML:
analyzer.visualize().to_html("sensitivity.html")