| """Scenario engine — perturb instances for stress testing.""" | |
| from __future__ import annotations | |
| import copy | |
| import random | |
| from optos.constants import SCENARIO_TYPES | |
| from optos.engine import OptimizationEngine | |
| from optos.models import ProblemInstance, ScenarioResult | |
| class ScenarioEngine: | |
| def __init__(self, time_limit_sec: float = 12.0) -> None: | |
| self.engine = OptimizationEngine(time_limit_sec) | |
| self.rng = random.Random(42) | |
| def apply_scenario( | |
| self, | |
| instance: ProblemInstance, | |
| scenario_type: str, | |
| seed: int = 42, | |
| ) -> tuple[ProblemInstance, dict]: | |
| rng = random.Random(seed) | |
| data = copy.deepcopy(instance.data) | |
| perturbation: dict = {"scenario_type": scenario_type} | |
| if scenario_type == "capacity_change": | |
| factor = rng.uniform(0.6, 1.4) | |
| if "vehicle_capacity" in data: | |
| data["vehicle_capacity"] = int(data["vehicle_capacity"] * factor) | |
| elif "bin_capacity" in data: | |
| data["bin_capacity"] = int(data["bin_capacity"] * factor) | |
| perturbation["capacity_factor"] = factor | |
| elif scenario_type == "demand_shift": | |
| factor = rng.uniform(0.7, 1.5) | |
| if "demands" in data: | |
| data["demands"] = [max(1, int(d * factor)) for d in data["demands"]] | |
| elif "demand" in data: | |
| data["demand"] = [ | |
| [max(1, int(d * factor)) for d in row] | |
| for row in data["demand"] | |
| ] | |
| perturbation["demand_factor"] = factor | |
| elif scenario_type == "resource_removal": | |
| pct = rng.uniform(0.05, 0.25) | |
| if "n_vehicles" in data: | |
| remove = max(1, int(data["n_vehicles"] * pct)) | |
| data["n_vehicles"] = max(1, data["n_vehicles"] - remove) | |
| elif "n_facilities" in data: | |
| remove = max(1, int(data["n_facilities"] * pct)) | |
| data["n_facilities"] = max(2, data["n_facilities"] - remove) | |
| perturbation["removed_pct"] = pct | |
| elif scenario_type == "cost_increase": | |
| factor = rng.uniform(1.1, 2.0) | |
| if "fixed_costs" in data: | |
| data["fixed_costs"] = [int(c * factor) for c in data["fixed_costs"]] | |
| elif "cost_matrix" in data: | |
| data["cost_matrix"] = [ | |
| [int(c * factor) for c in row] | |
| for row in data["cost_matrix"] | |
| ] | |
| perturbation["cost_factor"] = factor | |
| elif scenario_type == "network_disruption": | |
| pct = rng.uniform(0.1, 0.3) | |
| if "transport_costs" in data: | |
| nc = data["n_customers"] | |
| disrupted = int(nc * pct) | |
| for c in self.rng.sample(range(nc), disrupted): | |
| for f in range(data["n_facilities"]): | |
| data["transport_costs"][c][f] = int(data["transport_costs"][c][f] * 3) | |
| perturbation["disrupted_pct"] = pct | |
| perturbed = ProblemInstance( | |
| problem_type=instance.problem_type, | |
| instance_id=instance.instance_id + f"_sc_{scenario_type}", | |
| label=f"{instance.label} · {SCENARIO_TYPES[scenario_type]['label']}", | |
| size=instance.size, | |
| seed=seed, | |
| data=data, | |
| features=instance.features, | |
| constraints=instance.constraints, | |
| objectives=instance.objectives, | |
| ) | |
| return perturbed, perturbation | |
| def run_scenario( | |
| self, | |
| instance: ProblemInstance, | |
| scenario_type: str, | |
| seed: int = 42, | |
| ) -> ScenarioResult: | |
| baseline_results = self.engine.solve_instance(instance) | |
| baseline_obj = min( | |
| r.metrics.objective_value for r in baseline_results if r.metrics.feasible | |
| ) if any(r.metrics.feasible for r in baseline_results) else 0.0 | |
| perturbed, perturbation = self.apply_scenario(instance, scenario_type, seed) | |
| perturbed_results = self.engine.solve_instance(perturbed) | |
| perturbed_obj = min( | |
| r.metrics.objective_value for r in perturbed_results if r.metrics.feasible | |
| ) if any(r.metrics.feasible for r in perturbed_results) else 0.0 | |
| delta = 0.0 | |
| if baseline_obj > 0: | |
| delta = (perturbed_obj - baseline_obj) / baseline_obj * 100 | |
| binding = [] | |
| if scenario_type == "capacity_change": | |
| binding.append("capacity_constraint") | |
| elif scenario_type == "demand_shift": | |
| binding.append("demand_coverage") | |
| elif scenario_type == "resource_removal": | |
| binding.append("resource_availability") | |
| return ScenarioResult( | |
| scenario_type=scenario_type, | |
| scenario_label=SCENARIO_TYPES[scenario_type]["label"], | |
| perturbation=perturbation, | |
| baseline_objective=baseline_obj, | |
| perturbed_objective=perturbed_obj, | |
| delta_pct=round(delta, 2), | |
| feasible=perturbed_obj > 0, | |
| binding_constraints=binding, | |
| ) | |
| def run_all_scenarios(self, instance: ProblemInstance) -> list[ScenarioResult]: | |
| return [self.run_scenario(instance, st) for st in SCENARIO_TYPES] | |