Skip to main content

u_nesting_core/
alns.rs

1//! # Adaptive Large Neighborhood Search (ALNS) Framework
2//!
3//! Implementation of the ALNS metaheuristic based on Ropke & Pisinger (2006).
4//!
5//! # Architecture
6//!
7//! This module maintains its own ALNS loop rather than delegating to u-metaheur's
8//! ALNS runner. Key design differences:
9//! - u-metaheur uses functional operators (`destroy(&self, &S) -> S`) as separate
10//!   trait objects; u-nesting integrates operators into `AlnsProblem` with mutable
11//!   access (`destroy(&mut self, &mut Solution)`) for stateful placement caches.
12//! - u-metaheur uses `cost(&self, &S) -> f64`; u-nesting uses `AlnsSolution::fitness()`.
13//! - u-nesting provides `relatedness()` for Shaw-based removal, not in u-metaheur.
14//!
15//! The rand 0.9 API is shared with u-metaheur for ecosystem compatibility.
16//!
17//! # Algorithm
18//!
19//! ALNS combines:
20//! - **Destroy operators**: Remove items from current solution (multiple strategies)
21//! - **Repair operators**: Reinsert items to improve solution (multiple strategies)
22//! - **Adaptive weights**: Operators are selected based on past performance
23//! - **Simulated Annealing acceptance**: Probabilistic acceptance of worse solutions
24//!
25//! ## Key Features
26//!
27//! - Multiple destroy/repair operators with adaptive selection
28//! - Segment-based weight updates
29//! - Configurable acceptance criteria (SA, Hill-Climbing)
30//! - Progress callbacks for monitoring
31//!
32//! ## Usage
33//!
34//! ```rust,ignore
35//! use u_nesting_core::alns::{AlnsConfig, AlnsRunner, AlnsProblem};
36//!
37//! let config = AlnsConfig::default();
38//! let runner = AlnsRunner::new(config)?;
39//! let result = runner.run(&mut problem, progress_callback);
40//! ```
41
42use crate::result::StopReason;
43use crate::timing::Timer;
44use std::fmt::Debug;
45use std::sync::atomic::{AtomicBool, Ordering};
46use std::sync::Arc;
47
48/// Configuration for the ALNS algorithm.
49#[derive(Debug, Clone)]
50pub struct AlnsConfig {
51    /// Maximum number of iterations
52    pub max_iterations: usize,
53    /// Time limit in milliseconds (0 = no limit)
54    pub time_limit_ms: u64,
55    /// Number of iterations per segment (for weight updates)
56    pub segment_size: usize,
57    /// Score for finding new best solution
58    pub score_best: f64,
59    /// Score for finding better solution than current
60    pub score_better: f64,
61    /// Score for accepting worse solution
62    pub score_accepted: f64,
63    /// Reaction factor (how quickly weights adapt, 0-1)
64    pub reaction_factor: f64,
65    /// Minimum operator weight
66    pub min_weight: f64,
67    /// Initial temperature for SA acceptance
68    pub initial_temperature: f64,
69    /// Cooling rate for SA acceptance (0-1)
70    pub cooling_rate: f64,
71    /// Final temperature threshold
72    pub final_temperature: f64,
73    /// Random seed for reproducibility (None = random)
74    pub seed: Option<u64>,
75}
76
77impl Default for AlnsConfig {
78    fn default() -> Self {
79        Self {
80            max_iterations: 10000,
81            time_limit_ms: 60000, // 1 minute
82            segment_size: 100,
83            score_best: 33.0,
84            score_better: 9.0,
85            score_accepted: 3.0,
86            reaction_factor: 0.1,
87            min_weight: 0.1,
88            initial_temperature: 100.0,
89            cooling_rate: 0.9995,
90            final_temperature: 0.01,
91            seed: None,
92        }
93    }
94}
95
96impl AlnsConfig {
97    /// Create a new configuration with default values.
98    pub fn new() -> Self {
99        Self::default()
100    }
101
102    /// Set maximum iterations.
103    pub fn with_max_iterations(mut self, iterations: usize) -> Self {
104        self.max_iterations = iterations;
105        self
106    }
107
108    /// Set time limit in milliseconds.
109    pub fn with_time_limit_ms(mut self, ms: u64) -> Self {
110        self.time_limit_ms = ms;
111        self
112    }
113
114    /// Set segment size for weight updates.
115    pub fn with_segment_size(mut self, size: usize) -> Self {
116        self.segment_size = size;
117        self
118    }
119
120    /// Set scoring parameters.
121    pub fn with_scores(mut self, best: f64, better: f64, accepted: f64) -> Self {
122        self.score_best = best;
123        self.score_better = better;
124        self.score_accepted = accepted;
125        self
126    }
127
128    /// Set reaction factor.
129    pub fn with_reaction_factor(mut self, factor: f64) -> Self {
130        self.reaction_factor = factor;
131        self
132    }
133
134    /// Set temperature parameters for SA acceptance.
135    pub fn with_temperature(mut self, initial: f64, cooling_rate: f64, final_temp: f64) -> Self {
136        self.initial_temperature = initial;
137        self.cooling_rate = cooling_rate;
138        self.final_temperature = final_temp;
139        self
140    }
141
142    /// Checks every field against its range; the builders store what they are
143    /// given.
144    ///
145    /// # Errors
146    /// `ConfigError` naming the first field out of range: `reaction_factor`
147    /// outside `[0, 1]`, `cooling_rate` outside `[0.9, 1]`, `initial_temperature`
148    /// below 0.01, `final_temperature` below 0.001, `min_weight` negative,
149    /// `segment_size` below 1.
150    pub fn validate(&self) -> crate::error::Result<()> {
151        use crate::error::{check_at_least, check_range};
152        check_at_least("segment_size", self.segment_size as f64, 1.0)?;
153        check_range("reaction_factor", self.reaction_factor, 0.0, 1.0)?;
154        check_range("cooling_rate", self.cooling_rate, 0.9, 1.0)?;
155        check_at_least("initial_temperature", self.initial_temperature, 0.01)?;
156        check_at_least("final_temperature", self.final_temperature, 0.001)?;
157        check_at_least("min_weight", self.min_weight, 0.0)?;
158        Ok(())
159    }
160
161    /// Set random seed for reproducibility.
162    pub fn with_seed(mut self, seed: u64) -> Self {
163        self.seed = Some(seed);
164        self
165    }
166}
167
168/// Statistics for an operator.
169#[derive(Debug, Clone)]
170pub struct OperatorStats {
171    /// Current weight
172    pub weight: f64,
173    /// Number of times used
174    pub times_used: usize,
175    /// Total score accumulated
176    pub total_score: f64,
177    /// Score in current segment
178    pub segment_score: f64,
179    /// Uses in current segment
180    pub segment_uses: usize,
181}
182
183impl Default for OperatorStats {
184    fn default() -> Self {
185        Self {
186            weight: 1.0,
187            times_used: 0,
188            total_score: 0.0,
189            segment_score: 0.0,
190            segment_uses: 0,
191        }
192    }
193}
194
195impl OperatorStats {
196    /// Create new stats with initial weight.
197    pub fn new(initial_weight: f64) -> Self {
198        Self {
199            weight: initial_weight,
200            ..Default::default()
201        }
202    }
203
204    /// Record operator usage with a score.
205    pub fn record_use(&mut self, score: f64) {
206        self.times_used += 1;
207        self.total_score += score;
208        self.segment_score += score;
209        self.segment_uses += 1;
210    }
211
212    /// Update weight at end of segment.
213    pub fn update_weight(&mut self, reaction_factor: f64, min_weight: f64) {
214        if self.segment_uses > 0 {
215            let segment_avg = self.segment_score / self.segment_uses as f64;
216            self.weight = self.weight * (1.0 - reaction_factor) + segment_avg * reaction_factor;
217            self.weight = self.weight.max(min_weight);
218        }
219        // Reset segment counters
220        self.segment_score = 0.0;
221        self.segment_uses = 0;
222    }
223}
224
225/// Identifies a destroy operator.
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
227pub enum DestroyOperatorId {
228    /// Random removal
229    Random,
230    /// Worst removal (highest cost items)
231    Worst,
232    /// Shaw removal (related: nearby items around a seed)
233    Shaw,
234    /// Custom operator by index
235    Custom(usize),
236}
237
238/// Identifies a repair operator.
239#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
240pub enum RepairOperatorId {
241    /// Greedy repair: largest item first, each at its best position
242    Greedy,
243    /// Random repair: items in random order
244    Random,
245    /// Custom operator by index
246    Custom(usize),
247}
248
249/// Result of a destroy operation.
250#[derive(Debug, Clone)]
251pub struct DestroyResult {
252    /// Indices of removed items
253    pub removed_indices: Vec<usize>,
254    /// Operator used
255    pub operator: DestroyOperatorId,
256}
257
258/// Result of a repair operation.
259#[derive(Debug, Clone)]
260pub struct RepairResult {
261    /// Number of items successfully placed
262    pub placed_count: usize,
263    /// Number of items that could not be placed
264    pub unplaced_count: usize,
265    /// Operator used
266    pub operator: RepairOperatorId,
267}
268
269/// Progress information for ALNS callbacks.
270#[derive(Debug, Clone)]
271pub struct AlnsProgress {
272    /// Current iteration number
273    pub iteration: usize,
274    /// Best fitness found so far
275    pub best_fitness: f64,
276    /// Current fitness
277    pub current_fitness: f64,
278    /// Current temperature
279    pub temperature: f64,
280    /// Current segment number
281    pub segment: usize,
282    /// Elapsed time in milliseconds
283    pub elapsed_ms: u64,
284    /// Acceptance rate in current segment
285    pub acceptance_rate: f64,
286    /// Best destroy operator (by weight)
287    pub best_destroy: DestroyOperatorId,
288    /// Best repair operator (by weight)
289    pub best_repair: RepairOperatorId,
290}
291
292/// Result of ALNS optimization.
293#[derive(Debug, Clone)]
294pub struct AlnsResult<S> {
295    /// Best solution found
296    pub best_solution: S,
297    /// Best fitness value
298    pub best_fitness: f64,
299    /// Total iterations performed
300    pub iterations: usize,
301    /// Total time elapsed in milliseconds
302    pub elapsed_ms: u64,
303    /// Why the run stopped: its iteration cap, its time limit or cancellation
304    pub stop_reason: StopReason,
305    /// Number of improvements found
306    pub improvements: usize,
307    /// Final temperature
308    pub final_temperature: f64,
309    /// Final operator weights
310    pub destroy_weights: Vec<(DestroyOperatorId, f64)>,
311    /// Final repair operator weights
312    pub repair_weights: Vec<(RepairOperatorId, f64)>,
313}
314
315/// Trait for solutions that can be optimized by ALNS.
316pub trait AlnsSolution: Clone + Debug {
317    /// Get the fitness value (lower is better, 0 = optimal).
318    fn fitness(&self) -> f64;
319
320    /// Get the number of placed items.
321    fn placed_count(&self) -> usize;
322
323    /// Get the total number of items.
324    fn total_count(&self) -> usize;
325}
326
327/// Trait for problems that can be solved by ALNS.
328pub trait AlnsProblem {
329    /// Solution type
330    type Solution: AlnsSolution;
331
332    /// Create an initial solution.
333    fn create_initial_solution(&mut self) -> Self::Solution;
334
335    /// Clone a solution.
336    fn clone_solution(&self, solution: &Self::Solution) -> Self::Solution;
337
338    /// Get available destroy operators.
339    fn destroy_operators(&self) -> Vec<DestroyOperatorId>;
340
341    /// Get available repair operators.
342    fn repair_operators(&self) -> Vec<RepairOperatorId>;
343
344    /// Apply a destroy operator.
345    fn destroy(
346        &mut self,
347        solution: &mut Self::Solution,
348        operator: DestroyOperatorId,
349        degree: f64,
350        rng: &mut rand::rngs::StdRng,
351    ) -> DestroyResult;
352
353    /// Apply a repair operator. A repair that makes random choices draws them
354    /// from `rng`, the run's generator, so a seeded run is reproduced.
355    fn repair(
356        &mut self,
357        solution: &mut Self::Solution,
358        destroyed: &DestroyResult,
359        operator: RepairOperatorId,
360        rng: &mut rand::rngs::StdRng,
361    ) -> RepairResult;
362
363    /// Calculate relatedness between two items (for Shaw removal).
364    fn relatedness(&self, solution: &Self::Solution, i: usize, j: usize) -> f64 {
365        // Default: no relatedness information
366        let _ = (solution, i, j);
367        0.0
368    }
369}
370
371/// The ALNS algorithm runner.
372pub struct AlnsRunner {
373    config: AlnsConfig,
374    cancelled: Arc<AtomicBool>,
375}
376
377impl AlnsRunner {
378    /// Create a new ALNS runner with the given configuration.
379    ///
380    /// # Errors
381    /// As [`AlnsConfig::validate`].
382    pub fn new(config: AlnsConfig) -> crate::error::Result<Self> {
383        Self::with_cancellation(config, Arc::new(AtomicBool::new(false)))
384    }
385
386    /// Creates a runner that stops when `cancelled` is set -- the caller's
387    /// own flag, observed between iterations.
388    ///
389    /// # Errors
390    /// As [`AlnsConfig::validate`].
391    pub fn with_cancellation(
392        config: AlnsConfig,
393        cancelled: Arc<AtomicBool>,
394    ) -> crate::error::Result<Self> {
395        config.validate()?;
396        Ok(Self { config, cancelled })
397    }
398
399    /// Run the ALNS algorithm on the given problem.
400    pub fn run<P, F>(&self, problem: &mut P, mut progress_callback: F) -> AlnsResult<P::Solution>
401    where
402        P: AlnsProblem,
403        F: FnMut(&AlnsProgress),
404    {
405        use rand::prelude::*;
406        use rand::SeedableRng;
407
408        // Initialize RNG
409        let mut rng = match self.config.seed {
410            Some(seed) => rand::rngs::StdRng::seed_from_u64(seed),
411            None => rand::make_rng::<rand::rngs::StdRng>(),
412        };
413
414        let start_time = Timer::now();
415
416        // Create initial solution
417        let mut current = problem.create_initial_solution();
418        let mut best = problem.clone_solution(&current);
419        let mut best_fitness = best.fitness();
420
421        // Get operators
422        let destroy_ops = problem.destroy_operators();
423        let repair_ops = problem.repair_operators();
424
425        // Initialize operator statistics
426        let mut destroy_stats: Vec<(DestroyOperatorId, OperatorStats)> = destroy_ops
427            .iter()
428            .map(|&op| (op, OperatorStats::new(1.0)))
429            .collect();
430
431        let mut repair_stats: Vec<(RepairOperatorId, OperatorStats)> = repair_ops
432            .iter()
433            .map(|&op| (op, OperatorStats::new(1.0)))
434            .collect();
435
436        // Initialize temperature
437        let mut temperature = self.config.initial_temperature;
438
439        // Statistics
440        let mut iteration = 0;
441        let mut segment = 0;
442        let mut improvements = 0;
443        let mut segment_accepts = 0;
444        let mut segment_total = 0;
445
446        // Main loop -- each exit names its reason
447        let stop_reason = loop {
448            // Check termination conditions
449            let elapsed = start_time.elapsed();
450            let elapsed_ms = elapsed.as_millis() as u64;
451
452            if iteration >= self.config.max_iterations {
453                break StopReason::IterationLimit;
454            }
455
456            if self.cancelled.load(Ordering::Relaxed) {
457                break StopReason::Cancelled;
458            }
459
460            if self.config.time_limit_ms > 0 && elapsed_ms >= self.config.time_limit_ms {
461                break StopReason::TimeLimit;
462            }
463
464            // Select destroy operator using roulette wheel
465            let destroy_idx = self.select_operator_by_weight(&destroy_stats, &mut rng);
466            let destroy_op = destroy_stats[destroy_idx].0;
467
468            // Select repair operator using roulette wheel
469            let repair_idx = self.select_operator_by_weight(&repair_stats, &mut rng);
470            let repair_op = repair_stats[repair_idx].0;
471
472            // Clone current solution
473            let mut candidate = problem.clone_solution(&current);
474
475            // Apply destroy (remove 10-40% of items)
476            let degree = rng.random_range(0.1..=0.4);
477            let destroy_result = problem.destroy(&mut candidate, destroy_op, degree, &mut rng);
478
479            // Apply repair
480            let _repair_result =
481                problem.repair(&mut candidate, &destroy_result, repair_op, &mut rng);
482
483            let candidate_fitness = candidate.fitness();
484            let current_fitness = current.fitness();
485
486            // Determine acceptance and score
487            let (accepted, score) = if candidate_fitness < best_fitness {
488                // New best solution
489                best = problem.clone_solution(&candidate);
490                best_fitness = candidate_fitness;
491                improvements += 1;
492                (true, self.config.score_best)
493            } else if candidate_fitness < current_fitness {
494                // Better than current
495                (true, self.config.score_better)
496            } else {
497                // SA acceptance criterion
498                let delta = candidate_fitness - current_fitness;
499                let accept_prob = (-delta / temperature).exp();
500                if rng.random::<f64>() < accept_prob {
501                    (true, self.config.score_accepted)
502                } else {
503                    (false, 0.0)
504                }
505            };
506
507            // Update current if accepted
508            if accepted {
509                current = candidate;
510                segment_accepts += 1;
511            }
512
513            // Record operator usage
514            destroy_stats[destroy_idx].1.record_use(score);
515            repair_stats[repair_idx].1.record_use(score);
516
517            segment_total += 1;
518
519            // Update temperature
520            temperature *= self.config.cooling_rate;
521            temperature = temperature.max(self.config.final_temperature);
522
523            // Check if segment is complete
524            if iteration > 0 && iteration % self.config.segment_size == 0 {
525                // Update weights
526                for (_, stats) in &mut destroy_stats {
527                    stats.update_weight(self.config.reaction_factor, self.config.min_weight);
528                }
529                for (_, stats) in &mut repair_stats {
530                    stats.update_weight(self.config.reaction_factor, self.config.min_weight);
531                }
532
533                segment += 1;
534                segment_accepts = 0;
535                segment_total = 0;
536            }
537
538            // Progress callback
539            let acceptance_rate = if segment_total > 0 {
540                segment_accepts as f64 / segment_total as f64
541            } else {
542                0.0
543            };
544
545            let best_destroy = destroy_stats
546                .iter()
547                .max_by(|a, b| {
548                    a.1.weight
549                        .partial_cmp(&b.1.weight)
550                        .unwrap_or(std::cmp::Ordering::Equal)
551                })
552                .map(|(op, _)| *op)
553                .unwrap_or(DestroyOperatorId::Random);
554
555            let best_repair = repair_stats
556                .iter()
557                .max_by(|a, b| {
558                    a.1.weight
559                        .partial_cmp(&b.1.weight)
560                        .unwrap_or(std::cmp::Ordering::Equal)
561                })
562                .map(|(op, _)| *op)
563                .unwrap_or(RepairOperatorId::Greedy);
564
565            let progress = AlnsProgress {
566                iteration,
567                best_fitness,
568                current_fitness: current.fitness(),
569                temperature,
570                segment,
571                elapsed_ms,
572                acceptance_rate,
573                best_destroy,
574                best_repair,
575            };
576
577            progress_callback(&progress);
578
579            iteration += 1;
580        };
581
582        let elapsed_ms = start_time.elapsed_ms();
583
584        AlnsResult {
585            best_solution: best,
586            best_fitness,
587            iterations: iteration,
588            elapsed_ms,
589            stop_reason,
590            improvements,
591            final_temperature: temperature,
592            destroy_weights: destroy_stats
593                .iter()
594                .map(|(op, stats)| (*op, stats.weight))
595                .collect(),
596            repair_weights: repair_stats
597                .iter()
598                .map(|(op, stats)| (*op, stats.weight))
599                .collect(),
600        }
601    }
602
603    /// Select an operator index using roulette wheel selection.
604    fn select_operator_by_weight<T>(
605        &self,
606        stats: &[(T, OperatorStats)],
607        rng: &mut rand::rngs::StdRng,
608    ) -> usize {
609        use rand::prelude::*;
610
611        let total_weight: f64 = stats.iter().map(|(_, s)| s.weight).sum();
612        if total_weight <= 0.0 || stats.is_empty() {
613            return 0;
614        }
615
616        let mut roll = rng.random::<f64>() * total_weight;
617        for (i, (_, stat)) in stats.iter().enumerate() {
618            roll -= stat.weight;
619            if roll <= 0.0 {
620                return i;
621            }
622        }
623
624        stats.len() - 1
625    }
626}
627
628#[cfg(test)]
629mod tests {
630    use super::*;
631
632    #[test]
633    fn test_alns_config_default() {
634        let config = AlnsConfig::default();
635        assert_eq!(config.max_iterations, 10000);
636        assert_eq!(config.time_limit_ms, 60000);
637        assert_eq!(config.segment_size, 100);
638        assert!((config.score_best - 33.0).abs() < 1e-9);
639    }
640
641    #[test]
642    fn test_alns_config_builder() {
643        let config = AlnsConfig::new()
644            .with_max_iterations(5000)
645            .with_time_limit_ms(30000)
646            .with_segment_size(50)
647            .with_scores(10.0, 5.0, 1.0)
648            .with_reaction_factor(0.2)
649            .with_temperature(50.0, 0.999, 0.001)
650            .with_seed(42);
651
652        assert_eq!(config.max_iterations, 5000);
653        assert_eq!(config.time_limit_ms, 30000);
654        assert_eq!(config.segment_size, 50);
655        assert!((config.score_best - 10.0).abs() < 1e-9);
656        assert!((config.score_better - 5.0).abs() < 1e-9);
657        assert!((config.score_accepted - 1.0).abs() < 1e-9);
658        assert!((config.reaction_factor - 0.2).abs() < 1e-9);
659        assert!((config.initial_temperature - 50.0).abs() < 1e-9);
660        assert!((config.cooling_rate - 0.999).abs() < 1e-9);
661        assert!((config.final_temperature - 0.001).abs() < 1e-9);
662        assert_eq!(config.seed, Some(42));
663    }
664
665    #[test]
666    fn test_operator_stats() {
667        let mut stats = OperatorStats::new(1.0);
668
669        stats.record_use(10.0);
670        stats.record_use(20.0);
671
672        assert_eq!(stats.times_used, 2);
673        assert!((stats.total_score - 30.0).abs() < 1e-9);
674        assert!((stats.segment_score - 30.0).abs() < 1e-9);
675        assert_eq!(stats.segment_uses, 2);
676
677        stats.update_weight(0.5, 0.1);
678
679        // New weight = 1.0 * 0.5 + 15.0 * 0.5 = 8.0
680        assert!((stats.weight - 8.0).abs() < 1e-9);
681        assert!((stats.segment_score - 0.0).abs() < 1e-9);
682        assert_eq!(stats.segment_uses, 0);
683    }
684
685    #[test]
686    fn test_destroy_operator_ids() {
687        let ops = [
688            DestroyOperatorId::Random,
689            DestroyOperatorId::Worst,
690            DestroyOperatorId::Shaw,
691            DestroyOperatorId::Custom(0),
692        ];
693
694        assert_eq!(ops.len(), 4);
695        assert_eq!(DestroyOperatorId::Random, DestroyOperatorId::Random);
696        assert_ne!(DestroyOperatorId::Random, DestroyOperatorId::Worst);
697    }
698
699    #[test]
700    fn test_repair_operator_ids() {
701        let ops = [
702            RepairOperatorId::Greedy,
703            RepairOperatorId::Random,
704            RepairOperatorId::Custom(0),
705        ];
706
707        assert_eq!(ops.len(), 3);
708        assert_eq!(RepairOperatorId::Greedy, RepairOperatorId::Greedy);
709        assert_ne!(RepairOperatorId::Greedy, RepairOperatorId::Random);
710    }
711
712    #[test]
713    fn test_destroy_result() {
714        let result = DestroyResult {
715            removed_indices: vec![0, 3, 5],
716            operator: DestroyOperatorId::Random,
717        };
718
719        assert_eq!(result.removed_indices.len(), 3);
720        assert_eq!(result.operator, DestroyOperatorId::Random);
721    }
722
723    #[test]
724    fn test_repair_result() {
725        let result = RepairResult {
726            placed_count: 8,
727            unplaced_count: 2,
728            operator: RepairOperatorId::Greedy,
729        };
730
731        assert_eq!(result.placed_count, 8);
732        assert_eq!(result.unplaced_count, 2);
733        assert_eq!(result.operator, RepairOperatorId::Greedy);
734    }
735
736    #[test]
737    fn test_alns_progress() {
738        let progress = AlnsProgress {
739            iteration: 100,
740            best_fitness: 0.85,
741            current_fitness: 0.90,
742            temperature: 50.0,
743            segment: 1,
744            elapsed_ms: 5000,
745            acceptance_rate: 0.45,
746            best_destroy: DestroyOperatorId::Worst,
747            best_repair: RepairOperatorId::Greedy,
748        };
749
750        assert_eq!(progress.iteration, 100);
751        assert!((progress.best_fitness - 0.85).abs() < 1e-9);
752        assert_eq!(progress.segment, 1);
753        assert_eq!(progress.best_destroy, DestroyOperatorId::Worst);
754        assert_eq!(progress.best_repair, RepairOperatorId::Greedy);
755    }
756
757    // Mock implementation for testing the runner
758    #[derive(Clone, Debug)]
759    struct MockSolution {
760        fitness: f64,
761        placed: usize,
762        total: usize,
763    }
764
765    impl AlnsSolution for MockSolution {
766        fn fitness(&self) -> f64 {
767            self.fitness
768        }
769
770        fn placed_count(&self) -> usize {
771            self.placed
772        }
773
774        fn total_count(&self) -> usize {
775            self.total
776        }
777    }
778
779    struct MockProblem {
780        improvement_per_iteration: f64,
781    }
782
783    impl AlnsProblem for MockProblem {
784        type Solution = MockSolution;
785
786        fn create_initial_solution(&mut self) -> MockSolution {
787            MockSolution {
788                fitness: 1.0,
789                placed: 8,
790                total: 10,
791            }
792        }
793
794        fn clone_solution(&self, solution: &MockSolution) -> MockSolution {
795            solution.clone()
796        }
797
798        fn destroy_operators(&self) -> Vec<DestroyOperatorId> {
799            vec![DestroyOperatorId::Random, DestroyOperatorId::Worst]
800        }
801
802        fn repair_operators(&self) -> Vec<RepairOperatorId> {
803            vec![RepairOperatorId::Greedy, RepairOperatorId::Random]
804        }
805
806        fn destroy(
807            &mut self,
808            _solution: &mut MockSolution,
809            operator: DestroyOperatorId,
810            _degree: f64,
811            _rng: &mut rand::rngs::StdRng,
812        ) -> DestroyResult {
813            DestroyResult {
814                removed_indices: vec![0, 1, 2],
815                operator,
816            }
817        }
818
819        fn repair(
820            &mut self,
821            solution: &mut MockSolution,
822            _destroyed: &DestroyResult,
823            operator: RepairOperatorId,
824            _rng: &mut rand::rngs::StdRng,
825        ) -> RepairResult {
826            // Simulate improvement
827            solution.fitness -= self.improvement_per_iteration;
828            solution.fitness = solution.fitness.max(0.1);
829            RepairResult {
830                placed_count: solution.placed,
831                unplaced_count: 0,
832                operator,
833            }
834        }
835    }
836
837    #[test]
838    fn test_alns_runner_basic() {
839        let config = AlnsConfig::new()
840            .with_max_iterations(100)
841            .with_time_limit_ms(5000)
842            .with_seed(42);
843
844        let mut problem = MockProblem {
845            improvement_per_iteration: 0.01,
846        };
847
848        let runner = AlnsRunner::new(config).expect("valid config");
849        let mut last_progress: Option<AlnsProgress> = None;
850
851        let result = runner.run(&mut problem, |progress| {
852            last_progress = Some(progress.clone());
853        });
854
855        assert!(result.best_fitness <= 1.0);
856        assert_eq!(result.iterations, 100);
857        assert!(last_progress.is_some());
858        assert!(!result.destroy_weights.is_empty());
859        assert!(!result.repair_weights.is_empty());
860    }
861
862    #[test]
863    fn test_alns_runner_time_limit() {
864        let config = AlnsConfig::new()
865            // High enough that no machine reaches it inside the limit below:
866            // a mock iteration is a few instructions, and a million of them take
867            // well under 100 ms in release, which used to end this run on the
868            // iteration cap and fail the assertion about the time limit.
869            .with_max_iterations(usize::MAX)
870            .with_time_limit_ms(100)
871            .with_seed(42);
872
873        let mut problem = MockProblem {
874            improvement_per_iteration: 0.001,
875        };
876
877        let runner = AlnsRunner::new(config).expect("valid config");
878        let result = runner.run(&mut problem, |_| {});
879
880        // Should have terminated due to time limit
881        assert!(result.iterations < usize::MAX);
882        assert!(result.elapsed_ms >= 100);
883    }
884
885    #[test]
886    fn test_alns_weight_adaptation() {
887        let config = AlnsConfig::new()
888            .with_max_iterations(200)
889            .with_segment_size(50)
890            .with_reaction_factor(0.5)
891            .with_seed(42);
892
893        let mut problem = MockProblem {
894            improvement_per_iteration: 0.01,
895        };
896
897        let runner = AlnsRunner::new(config).expect("valid config");
898        let result = runner.run(&mut problem, |_| {});
899
900        // Weights should have changed from initial values
901        let _initial_weight_sum: f64 = 2.0; // 2 destroy operators, each starting at 1.0
902        let _final_destroy_sum: f64 = result.destroy_weights.iter().map(|(_, w)| *w).sum();
903
904        // At least some adaptation should have occurred
905        // (weights won't all be exactly 1.0 anymore)
906        let max_destroy_weight = result
907            .destroy_weights
908            .iter()
909            .map(|(_, w)| *w)
910            .fold(0.0, f64::max);
911
912        assert!(max_destroy_weight >= 0.1); // At least min_weight
913        assert!(result.iterations == 200);
914    }
915}