Skip to main content

converge_optimization/packs/facility_location/
mod.rs

1//! Facility Location Pack
2//!
3//! Choose facility locations to minimize total cost (opening + transport).
4
5mod solver;
6mod types;
7
8pub use solver::*;
9pub use types::*;
10
11use crate::packs::{InvariantDef, InvariantResult, Pack, PackSolveResult, default_gate_evaluation};
12use converge_pack::gate::GateResult as Result;
13use converge_pack::gate::{KernelTraceLink, ProblemSpec, PromotionGate, ProposedPlan};
14
15pub struct FacilityLocationPack;
16
17impl Pack for FacilityLocationPack {
18    fn name(&self) -> &'static str {
19        "facility-location"
20    }
21
22    fn version(&self) -> &'static str {
23        "1.0.0"
24    }
25
26    fn validate_inputs(&self, inputs: &serde_json::Value) -> Result<()> {
27        let input: FacilityLocationInput = serde_json::from_value(inputs.clone())
28            .map_err(|e| converge_pack::GateError::invalid_input(format!("Invalid input: {e}")))?;
29        input.validate()
30    }
31
32    fn invariants(&self) -> &[InvariantDef] {
33        &[]
34    }
35
36    fn solve(&self, spec: &ProblemSpec) -> Result<PackSolveResult> {
37        let input: FacilityLocationInput = spec.inputs_as()?;
38        input.validate()?;
39
40        let solver = GreedyFacilityLocationSolver;
41        let (output, report) = solver.solve(&input, spec)?;
42
43        let trace = KernelTraceLink::audit_only(format!("trace-{}", spec.problem_id));
44        let confidence = 0.75;
45
46        let plan = ProposedPlan::from_payload(
47            format!("plan-{}", spec.problem_id),
48            self.name(),
49            output.summary(),
50            &output,
51            confidence,
52            trace,
53        )?;
54
55        Ok(PackSolveResult::new(plan, report))
56    }
57
58    fn check_invariants(&self, _plan: &ProposedPlan) -> Result<Vec<InvariantResult>> {
59        Ok(vec![])
60    }
61
62    fn evaluate_gate(
63        &self,
64        _plan: &ProposedPlan,
65        invariant_results: &[InvariantResult],
66    ) -> PromotionGate {
67        default_gate_evaluation(invariant_results, self.invariants())
68    }
69}