Skip to main content

solverforge_config/
phase.rs

1use serde::{Deserialize, Serialize};
2
3use crate::acceptor::AcceptorConfig;
4use crate::forager::ForagerConfig;
5use crate::move_selector::{MoveSelectorConfig, VariableTargetConfig};
6use crate::termination::TerminationConfig;
7
8// Phase configuration.
9#[derive(Debug, Clone, Deserialize, Serialize)]
10#[serde(tag = "type", rename_all = "snake_case")]
11pub enum PhaseConfig {
12    // Construction heuristic phase.
13    ConstructionHeuristic(ConstructionHeuristicConfig),
14
15    // Local search phase.
16    LocalSearch(LocalSearchConfig),
17
18    // Variable neighborhood descent phase.
19    Vnd(VndConfig),
20
21    // Exhaustive search phase.
22    ExhaustiveSearch(ExhaustiveSearchConfig),
23
24    // Partitioned search phase.
25    PartitionedSearch(PartitionedSearchConfig),
26
27    // Custom phase.
28    Custom(CustomPhaseConfig),
29}
30
31fn default_k() -> usize {
32    2
33}
34
35// Construction heuristic configuration.
36#[derive(Debug, Clone, Deserialize, Serialize)]
37#[serde(rename_all = "snake_case")]
38pub struct ConstructionHeuristicConfig {
39    // Type of construction heuristic.
40    #[serde(default)]
41    pub construction_heuristic_type: ConstructionHeuristicType,
42
43    // Optional variable target.
44    #[serde(flatten)]
45    pub target: VariableTargetConfig,
46
47    // k for ListKOpt (default 2).
48    #[serde(default = "default_k")]
49    pub k: usize,
50
51    // Phase termination configuration.
52    pub termination: Option<TerminationConfig>,
53}
54
55impl Default for ConstructionHeuristicConfig {
56    fn default() -> Self {
57        Self {
58            construction_heuristic_type: ConstructionHeuristicType::default(),
59            target: VariableTargetConfig::default(),
60            k: default_k(),
61            termination: None,
62        }
63    }
64}
65
66// Construction heuristic types.
67#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
68#[serde(rename_all = "snake_case")]
69pub enum ConstructionHeuristicType {
70    // First fit heuristic (scalar variables).
71    #[default]
72    FirstFit,
73
74    // First fit decreasing. Scalar-only; requires `construction_entity_order_key`.
75    FirstFitDecreasing,
76
77    // Weakest fit heuristic. Scalar-only; requires `construction_value_order_key`.
78    WeakestFit,
79
80    // Weakest fit decreasing. Scalar-only; requires both scalar construction order keys.
81    WeakestFitDecreasing,
82
83    // Strongest fit heuristic. Scalar-only; requires `construction_value_order_key`.
84    StrongestFit,
85
86    // Strongest fit decreasing. Scalar-only; requires both scalar construction order keys.
87    StrongestFitDecreasing,
88
89    // Cheapest insertion (greedy, scalar variables).
90    CheapestInsertion,
91
92    // Allocate entity from queue. Scalar-only; requires `construction_entity_order_key`.
93    AllocateEntityFromQueue,
94
95    // Allocate to value from queue. Scalar-only; requires `construction_value_order_key`.
96    AllocateToValueFromQueue,
97
98    // List round-robin construction: distributes elements evenly across entities and validates the
99    // list construction hook surface before phase build.
100    ListRoundRobin,
101
102    // List cheapest insertion: inserts each element at the score-minimizing position and validates
103    // the list construction hook surface before phase build.
104    ListCheapestInsertion,
105
106    // List regret insertion: inserts elements in order of highest placement regret and validates
107    // the list construction hook surface before phase build.
108    ListRegretInsertion,
109
110    // List Clarke-Wright savings: greedy route merging by savings value; requires the declared
111    // `cw_*` list hooks and validates them before phase build.
112    ListClarkeWright,
113
114    // List k-opt: per-route k-opt polishing (k=2 is exact 2-opt); requires the declared
115    // `k_opt_*` list hooks and validates them before phase build.
116    ListKOpt,
117}
118
119// Local search configuration.
120#[derive(Debug, Clone, Default, Deserialize, Serialize)]
121#[serde(rename_all = "snake_case")]
122pub struct LocalSearchConfig {
123    // Acceptor configuration.
124    pub acceptor: Option<AcceptorConfig>,
125
126    // Forager configuration.
127    pub forager: Option<ForagerConfig>,
128
129    // Move selector configuration.
130    pub move_selector: Option<MoveSelectorConfig>,
131
132    // Phase termination configuration.
133    pub termination: Option<TerminationConfig>,
134}
135
136// VND configuration.
137#[derive(Debug, Clone, Default, Deserialize, Serialize)]
138#[serde(rename_all = "snake_case")]
139pub struct VndConfig {
140    // Ordered neighborhood selectors.
141    #[serde(default)]
142    pub neighborhoods: Vec<MoveSelectorConfig>,
143
144    // Phase termination configuration.
145    pub termination: Option<TerminationConfig>,
146}
147
148// Exhaustive search configuration.
149#[derive(Debug, Clone, Default, Deserialize, Serialize)]
150#[serde(rename_all = "snake_case")]
151pub struct ExhaustiveSearchConfig {
152    // Exhaustive search type.
153    #[serde(default)]
154    pub exhaustive_search_type: ExhaustiveSearchType,
155
156    // Phase termination configuration.
157    pub termination: Option<TerminationConfig>,
158}
159
160// Exhaustive search types.
161#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
162#[serde(rename_all = "snake_case")]
163pub enum ExhaustiveSearchType {
164    // Branch and bound.
165    #[default]
166    BranchAndBound,
167
168    // Brute force.
169    BruteForce,
170}
171
172// Partitioned search configuration.
173#[derive(Debug, Clone, Default, Deserialize, Serialize)]
174#[serde(rename_all = "snake_case")]
175pub struct PartitionedSearchConfig {
176    // Number of partitions.
177    pub partition_count: Option<usize>,
178
179    // Phase termination configuration.
180    pub termination: Option<TerminationConfig>,
181}
182
183// Custom phase configuration.
184#[derive(Debug, Clone, Default, Deserialize, Serialize)]
185#[serde(rename_all = "snake_case")]
186pub struct CustomPhaseConfig {
187    // Custom phase class name.
188    pub custom_phase_class: Option<String>,
189}