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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! MO-BMR / MO-BWR / MO-BMWR — multi-objective extensions of the BMR/BWR/BMWR
//! family (Rao 2025/2026, MDPI Metals 15/9/1057, MDPI Energies 19/1/34, MDPI
//! JMMP 9/8/249).
//!
//! Wraps the single-objective base update with five features per the JMMP
//! 2025 paper:
//! 1. Elite seeding — preserve top-rank Pareto solutions across iterations.
//! 2. Fast non-dominated sorting (Deb-style; via `crate::moo`).
//! 3. Constraint repairing → penalty fallback.
//! 4. Local exploration — Gaussian perturbation around elites.
//! 5. Edge boosting — extend the Pareto front by perturbing extremes.
//!
//! Total complexity: O(I·(M·c² + c·(m + tf + tp))) per the paper.
use crate::common::{
MultiObjectiveIndividual, MultiObjectiveProblem, MultiObjectiveResult, SolverConfig,
};
use crate::moo::{evaluate_population, hypervolume_2d};
use ndarray::Array1;
use rand::prelude::*;
use rand_distr::{Distribution, Normal};
#[derive(Debug, Clone, Copy)]
pub enum MOBMWRVariant {
MOBMR,
MOBWR,
MOBMWR,
}
pub struct MOBMWRSolver {
pub config: SolverConfig,
pub variant: MOBMWRVariant,
/// Local-exploration step size as a fraction of the bound range.
pub local_step: f64,
/// Probability of edge boosting per iteration.
pub edge_boost_prob: f64,
}
impl MOBMWRSolver {
pub fn new(config: SolverConfig, variant: MOBMWRVariant) -> Self {
Self {
config,
variant,
local_step: 0.05,
edge_boost_prob: 0.2,
}
}
pub fn solve<P: MultiObjectiveProblem>(&self, problem: &P) -> MultiObjectiveResult {
let mut rng = thread_rng();
let dim = problem.dim();
let (lower, upper) = problem.bounds();
let pop_size = self.config.population_size;
let mut population: Vec<MultiObjectiveIndividual> = (0..pop_size)
.map(|_| {
let mut vars = Array1::zeros(dim);
for i in 0..dim {
vars[i] = rng.gen_range(lower[i]..upper[i]);
}
let fitness = problem.objectives(&vars);
let viol: f64 = problem.penalties(&vars).iter().sum();
MultiObjectiveIndividual::new(vars, fitness, viol)
})
.collect();
evaluate_population(&mut population);
let mut history = Vec::with_capacity(self.config.max_iterations);
for iter in 0..self.config.max_iterations {
if iter % 10 == 0 {
println!(
"MO-{:?} Solver: Iteration {}/{}",
self.variant, iter, self.config.max_iterations
);
}
// --- Elite seeding: pick best rank-0 individual as the "best" reference.
let elites: Vec<&MultiObjectiveIndividual> =
population.iter().filter(|ind| ind.rank == 0).collect();
if elites.is_empty() {
break;
}
let best_ref = &elites[rng.gen_range(0..elites.len())].variables;
// For MO, "worst" is taken from the highest rank.
let worst_rank = population.iter().map(|i| i.rank).max().unwrap_or(0);
let worst_pool: Vec<&MultiObjectiveIndividual> = population
.iter()
.filter(|ind| ind.rank == worst_rank)
.collect();
let worst_ref = &worst_pool[rng.gen_range(0..worst_pool.len())].variables;
// Mean across population.
let mut mean_vars = Array1::<f64>::zeros(dim);
for ind in &population {
mean_vars += &ind.variables;
}
mean_vars /= pop_size as f64;
let snapshot: Vec<Array1<f64>> =
population.iter().map(|i| i.variables.clone()).collect();
// --- Generate offspring via base update.
let mut offspring: Vec<MultiObjectiveIndividual> = Vec::with_capacity(pop_size);
for k in 0..pop_size {
let mut local_rng = thread_rng();
let r1: f64 = local_rng.gen();
let r2: f64 = local_rng.gen();
let r3: f64 = local_rng.gen();
let r4: f64 = local_rng.gen();
let r5: f64 = local_rng.gen();
let t: f64 = local_rng.gen_range(1..3) as f64;
let mut new_vars = Array1::zeros(dim);
if r4 > 0.5 {
let mut rk = local_rng.gen_range(0..pop_size);
if rk == k && pop_size > 1 {
rk = (rk + 1) % pop_size;
}
let rand_vars = &snapshot[rk];
let v = &population[k].variables;
for j in 0..dim {
let delta = match self.variant {
MOBMWRVariant::MOBMR => {
r1 * (best_ref[j] - t * mean_vars[j])
+ r2 * (best_ref[j] - rand_vars[j])
}
MOBMWRVariant::MOBWR => {
r1 * (best_ref[j] - t * rand_vars[j])
- r2 * (worst_ref[j] - rand_vars[j])
}
MOBMWRVariant::MOBMWR => {
r1 * (best_ref[j] - t * mean_vars[j])
+ r2 * (best_ref[j] - rand_vars[j])
- r5 * (worst_ref[j] - rand_vars[j])
}
};
new_vars[j] = (v[j] + delta).clamp(lower[j], upper[j]);
}
} else {
for j in 0..dim {
new_vars[j] =
(upper[j] - (upper[j] - lower[j]) * r3).clamp(lower[j], upper[j]);
}
}
// Constraint repair: clip to bounds (already done) — for box constraints
// this is full repair. For inequality penalties, fall through to penalty.
let fit = problem.objectives(&new_vars);
let viol: f64 = problem.penalties(&new_vars).iter().sum();
offspring.push(MultiObjectiveIndividual::new(new_vars, fit, viol));
}
// --- Local exploration: Gaussian perturbation around random elite.
let normal = Normal::new(0.0, 1.0).unwrap();
for _ in 0..(pop_size / 10).max(1) {
let elite = &elites[rng.gen_range(0..elites.len())].variables;
let mut new_vars = elite.clone();
for j in 0..dim {
let sigma = self.local_step * (upper[j] - lower[j]);
new_vars[j] = (new_vars[j] + sigma * normal.sample(&mut rng))
.clamp(lower[j], upper[j]);
}
let fit = problem.objectives(&new_vars);
let viol: f64 = problem.penalties(&new_vars).iter().sum();
offspring.push(MultiObjectiveIndividual::new(new_vars, fit, viol));
}
// --- Edge boosting: occasionally push extreme objectives further.
if rng.gen::<f64>() < self.edge_boost_prob {
let m = problem.num_objectives();
for obj in 0..m {
// Find current extreme on objective `obj`.
let extreme_idx = elites
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| {
a.fitness[obj].partial_cmp(&b.fitness[obj]).unwrap()
})
.map(|(i, _)| i)
.unwrap_or(0);
let mut new_vars = elites[extreme_idx].variables.clone();
for j in 0..dim {
let sigma = 0.5 * self.local_step * (upper[j] - lower[j]);
new_vars[j] = (new_vars[j] + sigma * normal.sample(&mut rng))
.clamp(lower[j], upper[j]);
}
let fit = problem.objectives(&new_vars);
let viol: f64 = problem.penalties(&new_vars).iter().sum();
offspring.push(MultiObjectiveIndividual::new(new_vars, fit, viol));
}
}
// --- Combine and select via FNDS + crowding.
population.extend(offspring);
evaluate_population(&mut population);
population.sort_by(|a, b| {
if a.rank != b.rank {
a.rank.cmp(&b.rank)
} else {
b.crowding_distance
.partial_cmp(&a.crowding_distance)
.unwrap_or(std::cmp::Ordering::Equal)
}
});
population.truncate(pop_size);
// History: hypervolume if 2-objective, else first objective of best.
let hist_val = if problem.num_objectives() == 2 {
let front: Vec<Vec<f64>> = population
.iter()
.filter(|i| i.rank == 0)
.map(|i| i.fitness.clone())
.collect();
let max0 = front
.iter()
.map(|p| p[0])
.fold(f64::NEG_INFINITY, f64::max)
.max(1.0);
let max1 = front
.iter()
.map(|p| p[1])
.fold(f64::NEG_INFINITY, f64::max)
.max(1.0);
hypervolume_2d(&front, [max0 + 1.0, max1 + 1.0])
} else {
population
.iter()
.filter(|i| i.rank == 0)
.map(|i| i.fitness[0])
.fold(f64::INFINITY, f64::min)
};
history.push(hist_val);
}
MultiObjectiveResult {
pareto_front: population.into_iter().filter(|i| i.rank == 0).collect(),
history,
}
}
}