1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Error types for the Differential Evolution optimizer.
//!
//! This module provides structured error handling for DE optimization,
//! following the Microsoft Rust Guidelines pattern of using `thiserror`
//! for library error types with helper methods for error categorization.
use thiserror::Error;
/// Errors that can occur during Differential Evolution optimization.
#[derive(Debug, Error)]
pub enum DEError {
/// Lower and upper bounds have different lengths.
#[error("bounds mismatch: lower has {lower_len} elements, upper has {upper_len}")]
BoundsMismatch {
/// Length of the lower bounds array
lower_len: usize,
/// Length of the upper bounds array
upper_len: usize,
},
/// A lower bound exceeds its corresponding upper bound.
#[error("invalid bounds at index {index}: lower ({lower}) > upper ({upper})")]
InvalidBounds {
/// Index of the invalid bound pair
index: usize,
/// The lower bound value
lower: f64,
/// The upper bound value
upper: f64,
},
/// Population size is too small (must be >= 4).
#[error("population size ({pop_size}) must be >= 4 for DE algorithm")]
PopulationTooSmall {
/// The invalid population size
pop_size: usize,
},
/// Mutation factor is out of valid range [0, 2].
#[error("invalid mutation factor: {factor} (must be in [0, 2])")]
InvalidMutationFactor {
/// The invalid mutation factor
factor: f64,
},
/// Crossover rate is out of valid range [0, 1].
#[error("invalid crossover rate: {rate} (must be in [0, 1])")]
InvalidCrossoverRate {
/// The invalid crossover rate
rate: f64,
},
/// Initial guess (x0) has wrong dimension.
#[error("x0 dimension mismatch: expected {expected}, got {got}")]
X0DimensionMismatch {
/// Expected dimension
expected: usize,
/// Actual dimension provided
got: usize,
},
/// Integrality mask has wrong dimension.
#[error("integrality mask dimension mismatch: expected {expected}, got {got}")]
IntegralityDimensionMismatch {
/// Expected dimension
expected: usize,
/// Actual dimension provided
got: usize,
},
}
/// A specialized `Result` type for DE operations.
pub type Result<T> = std::result::Result<T, DEError>;
impl DEError {
/// Returns `true` if this is a bounds-related error.
///
/// This includes `BoundsMismatch` and `InvalidBounds` variants.
pub fn is_bounds_error(&self) -> bool {
matches!(
self,
DEError::BoundsMismatch { .. } | DEError::InvalidBounds { .. }
)
}
/// Returns `true` if this is a configuration-related error.
///
/// This includes `PopulationTooSmall`, `InvalidMutationFactor`,
/// and `InvalidCrossoverRate` variants.
pub fn is_config_error(&self) -> bool {
matches!(
self,
DEError::PopulationTooSmall { .. }
| DEError::InvalidMutationFactor { .. }
| DEError::InvalidCrossoverRate { .. }
)
}
/// Returns `true` if this is a dimension mismatch error.
///
/// This includes `X0DimensionMismatch` and `IntegralityDimensionMismatch`.
pub fn is_dimension_error(&self) -> bool {
matches!(
self,
DEError::X0DimensionMismatch { .. } | DEError::IntegralityDimensionMismatch { .. }
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = DEError::BoundsMismatch {
lower_len: 3,
upper_len: 5,
};
assert_eq!(
err.to_string(),
"bounds mismatch: lower has 3 elements, upper has 5"
);
}
#[test]
fn test_is_bounds_error() {
let bounds_err = DEError::BoundsMismatch {
lower_len: 1,
upper_len: 2,
};
let config_err = DEError::PopulationTooSmall { pop_size: 2 };
assert!(bounds_err.is_bounds_error());
assert!(!config_err.is_bounds_error());
}
#[test]
fn test_is_config_error() {
let config_err = DEError::InvalidCrossoverRate { rate: 1.5 };
let bounds_err = DEError::InvalidBounds {
index: 0,
lower: 5.0,
upper: 3.0,
};
assert!(config_err.is_config_error());
assert!(!bounds_err.is_config_error());
}
#[test]
fn test_is_dimension_error() {
let dim_err = DEError::X0DimensionMismatch {
expected: 10,
got: 5,
};
let bounds_err = DEError::BoundsMismatch {
lower_len: 1,
upper_len: 2,
};
assert!(dim_err.is_dimension_error());
assert!(!bounds_err.is_dimension_error());
}
}