"""Synthetic instance generators for all registered problem types.""" from __future__ import annotations import hashlib import random from typing import Any from optos.constants import PROBLEM_TYPES, SIZE_PRESETS from optos.models import InstanceFeatures, ProblemInstance def _rng(seed: int) -> random.Random: return random.Random(seed) def _instance_id(problem_type: str, size: str, seed: int) -> str: raw = f"{problem_type}_{size}_{seed}" return hashlib.md5(raw.encode()).hexdigest()[:12] def _scale_n(base: int, size: str) -> int: return max(2, int(base * SIZE_PRESETS[size]["scale"])) def generate_scheduling(rng: random.Random, size: str) -> dict[str, Any]: n_jobs = _scale_n(6, size) n_machines = _scale_n(4, size) processing_times = [ [rng.randint(2, 12) for _ in range(n_machines)] for _ in range(n_jobs) ] machine_order = [ list(range(n_machines)) for _ in range(n_jobs) ] return { "n_jobs": n_jobs, "n_machines": n_machines, "processing_times": processing_times, "machine_order": machine_order, } def generate_routing(rng: random.Random, size: str) -> dict[str, Any]: n_customers = _scale_n(12, size) n_vehicles = max(2, n_customers // 4) depot = (50.0, 50.0) customers = [ (rng.uniform(0, 100), rng.uniform(0, 100)) for _ in range(n_customers) ] demands = [rng.randint(1, 10) for _ in range(n_customers)] vehicle_capacity = max(demands) * 3 return { "n_customers": n_customers, "n_vehicles": n_vehicles, "depot": depot, "customers": customers, "demands": demands, "vehicle_capacity": vehicle_capacity, } def generate_assignment(rng: random.Random, size: str) -> dict[str, Any]: n = _scale_n(8, size) cost_matrix = [ [rng.randint(1, 50) for _ in range(n)] for _ in range(n) ] return {"n_agents": n, "cost_matrix": cost_matrix} def generate_inventory(rng: random.Random, size: str) -> dict[str, Any]: n_items = _scale_n(10, size) horizon = _scale_n(14, size) demand = [ [rng.randint(5, 30) for _ in range(horizon)] for _ in range(n_items) ] holding_cost = [rng.uniform(0.5, 2.0) for _ in range(n_items)] stockout_cost = [rng.uniform(5.0, 20.0) for _ in range(n_items)] order_cost = [rng.uniform(10.0, 50.0) for _ in range(n_items)] initial_stock = [rng.randint(10, 40) for _ in range(n_items)] return { "n_items": n_items, "horizon": horizon, "demand": demand, "holding_cost": holding_cost, "stockout_cost": stockout_cost, "order_cost": order_cost, "initial_stock": initial_stock, "max_order": [max(d) * 2 for d in demand], } def generate_facility_location(rng: random.Random, size: str) -> dict[str, Any]: n_facilities = _scale_n(6, size) n_customers = _scale_n(15, size) fixed_costs = [rng.randint(100, 500) for _ in range(n_facilities)] transport_costs = [ [rng.randint(1, 30) for _ in range(n_facilities)] for _ in range(n_customers) ] return { "n_facilities": n_facilities, "n_customers": n_customers, "fixed_costs": fixed_costs, "transport_costs": transport_costs, } def generate_packing(rng: random.Random, size: str) -> dict[str, Any]: n_items = _scale_n(20, size) bin_capacity = 100 item_sizes = [rng.randint(10, 45) for _ in range(n_items)] return { "n_items": n_items, "bin_capacity": bin_capacity, "item_sizes": item_sizes, } GENERATORS = { "scheduling": generate_scheduling, "routing": generate_routing, "assignment": generate_assignment, "inventory": generate_inventory, "facility_location": generate_facility_location, "packing": generate_packing, } def _estimate_features(problem_type: str, data: dict[str, Any]) -> InstanceFeatures: if problem_type == "scheduling": n_vars = data["n_jobs"] * data["n_machines"] * 2 n_cons = data["n_jobs"] * (data["n_machines"] - 1) + data["n_machines"] elif problem_type == "routing": n_vars = data["n_customers"] * data["n_vehicles"] n_cons = data["n_customers"] + data["n_vehicles"] elif problem_type == "assignment": n = data["n_agents"] n_vars = n * n n_cons = 2 * n elif problem_type == "inventory": n_vars = data["n_items"] * data["horizon"] n_cons = data["n_items"] * data["horizon"] elif problem_type == "facility_location": nf, nc = data["n_facilities"], data["n_customers"] n_vars = nf + nf * nc n_cons = nc + nf * nc elif problem_type == "packing": n_vars = data["n_items"] * data["n_items"] n_cons = data["n_items"] + data["n_items"] else: n_vars, n_cons = 10, 10 return InstanceFeatures( n_variables=n_vars, n_constraints=n_cons, density=round(min(1.0, n_cons / max(n_vars, 1)), 3), pct_integer=0.85, constraint_tightness=round(random.Random(0).uniform(0.4, 0.8), 3), ) def generate_instance( problem_type: str, size: str = "medium", seed: int = 42, constraints: dict[str, Any] | None = None, objectives: dict[str, Any] | None = None, ) -> ProblemInstance: if problem_type not in PROBLEM_TYPES: raise ValueError(f"Unknown problem type: {problem_type}") rng = _rng(seed) data = GENERATORS[problem_type](rng, size) features = _estimate_features(problem_type, data) meta = PROBLEM_TYPES[problem_type] label = f"{meta['label']} · {SIZE_PRESETS.get(size, {}).get('label', size)} · seed={seed}" return ProblemInstance( problem_type=problem_type, instance_id=_instance_id(problem_type, size, seed), label=label, size=size, seed=seed, data=data, features=features, constraints=constraints or {}, objectives=objectives or {"primary": meta.get("objective", "minimize")}, )