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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Constraints — penalty-based constraint handling for single-objective GA.
//!
//! This module defines the strategy types and helper functions used by
//! [`Ga`](crate::ga::Ga) to handle constrained optimization problems.
//! Three penalty strategies (static, dynamic, adaptive) modify fitness based
//! on constraint violation severity. Deb's feasibility rules compare solutions
//! by feasibility first, then fitness. The `RepairOperator` trait fixes
//! infeasible chromosomes.
//!
//! # Key items
//!
//! | Item | Description |
//! |------|-------------|
//! | [`ConstraintHandling`] | Master configuration struct for constraint enforcement |
//! | [`PenaltyStrategy`] | Enum: Static, Dynamic, or Adaptive penalty scaling |
//! | `RepairOperator` | Trait for repairing infeasible chromosomes |
//!
//! # When to use
//! Enable constraint handling when your optimization problem has constraints
//! that cannot be handled by bounding genes alone (e.g., inequality constraints,
//! non-linear constraints). Pass a [`ConstraintHandling`] instance to the
//! engine builder via `with_constraint_handling()`.
use crateGaError;
/// Strategy for applying penalty to infeasible solutions.
///
/// The penalty is computed per-generation based on constraint violations
/// and then added to the raw fitness value. All variants work with
/// minimization, maximization, and fixed-fitness problems.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::constraints::PenaltyStrategy;
/// let strategy = PenaltyStrategy::Static { coefficient: 10.0 };
/// ```
/// Constraint handling method for comparisons in selection, survivor, and elite operations.
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::constraints::ConstraintHandling;
///
/// let handling = ConstraintHandling::FeasibilityRules;
/// assert_eq!(handling, ConstraintHandling::FeasibilityRules);
/// ```
/// Computes the total constraint violation from the per-constraint violations.
///
/// Each constraint violation should be >= 0 (0 means the constraint is satisfied).
/// Returns the sum of all violations.
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::constraints::total_violation;
///
/// assert_eq!(total_violation(&[0.5, 1.5, 0.0]), 2.0);
/// assert_eq!(total_violation(&[]), 0.0);
/// ```
/// Applies a static penalty to the raw fitness value.
///
/// `fitness_penalized = fitness + R * total_violation`
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::constraints::apply_static_penalty;
///
/// let penalized = apply_static_penalty(10.0, 2.0, 5.0);
/// assert_eq!(penalized, 20.0);
/// ```
/// Applies the Joines & Houck (1994) dynamic penalty.
///
/// `fitness_penalized = fitness + (C * generation)^alpha * total_violation^beta`
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::constraints::apply_dynamic_penalty;
///
/// let penalized = apply_dynamic_penalty(5.0, 5.0, 5, 1.0, 1.0, 1.0);
/// assert!((penalized - 30.0).abs() < 1e-9);
/// ```
/// Validates a penalty strategy configuration.
///
/// Returns `Ok(())` if valid, or `Err(GaError::InvalidConstraintConfiguration)` with
/// a descriptive message.
///
/// # Examples
///
/// ```rust
/// use genetic_algorithms::constraints::{PenaltyStrategy, validate_penalty_strategy};
///
/// assert!(validate_penalty_strategy(&PenaltyStrategy::None).is_ok());
/// assert!(validate_penalty_strategy(&PenaltyStrategy::Static { coefficient: 5.0 }).is_ok());
/// assert!(validate_penalty_strategy(&PenaltyStrategy::Static { coefficient: -1.0 }).is_err());
/// ```