Skip to main content

aprender_tsp/
model.rs

1//! TSP model persistence in .apr format.
2//!
3//! Toyota Way Principle: *Standardized Work* - Consistent .apr format enables
4//! reproducible results across environments.
5
6use crate::error::{TspError, TspResult};
7use crate::solver::TspAlgorithm;
8use std::io::{Read, Write};
9use std::path::Path;
10
11/// Magic number for TSP .apr files
12const MAGIC: &[u8; 4] = b"APR\x00";
13
14/// Version number
15const VERSION: u32 = 1;
16
17/// Model type identifier for TSP
18const MODEL_TYPE_TSP: u32 = 0x54_53_50_00; // "TSP\x00"
19
20/// Algorithm-specific parameters
21#[derive(Debug, Clone)]
22pub enum TspParams {
23    /// ACO parameters
24    Aco {
25        alpha: f64,
26        beta: f64,
27        rho: f64,
28        q0: f64,
29        num_ants: usize,
30    },
31    /// Tabu Search parameters
32    Tabu { tenure: usize, max_neighbors: usize },
33    /// Genetic Algorithm parameters
34    Ga {
35        population_size: usize,
36        crossover_rate: f64,
37        mutation_rate: f64,
38    },
39    /// Hybrid parameters
40    Hybrid {
41        ga_fraction: f64,
42        tabu_fraction: f64,
43        aco_fraction: f64,
44    },
45}
46
47impl Default for TspParams {
48    fn default() -> Self {
49        Self::Aco {
50            alpha: 1.0,
51            beta: 2.5,
52            rho: 0.1,
53            q0: 0.9,
54            num_ants: 20,
55        }
56    }
57}
58
59/// Training metadata
60#[derive(Debug, Clone)]
61pub struct TspModelMetadata {
62    /// Number of instances used for training
63    pub trained_instances: u32,
64    /// Average instance size
65    pub avg_instance_size: u32,
66    /// Best known gap achieved during training
67    pub best_known_gap: f64,
68    /// Training time in seconds
69    pub training_time_secs: f64,
70}
71
72impl Default for TspModelMetadata {
73    fn default() -> Self {
74        Self {
75            trained_instances: 0,
76            avg_instance_size: 0,
77            best_known_gap: 0.0,
78            training_time_secs: 0.0,
79        }
80    }
81}
82
83/// TSP model persisted in .apr format
84#[derive(Debug, Clone)]
85pub struct TspModel {
86    /// Solver algorithm
87    pub algorithm: TspAlgorithm,
88    /// Learned parameters (algorithm-specific)
89    pub params: TspParams,
90    /// Training metadata
91    pub metadata: TspModelMetadata,
92}
93
94include!("payload_reader.rs");
95include!("model_model.rs");