sceua 0.1.0

Shuffled Complex Evolution - University of Arizona global optimizer
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::{
    cce::{evolve_simplex, CceScratch, ComplexLayout},
    config::{Config, ResolvedConfig},
    error::SceuaError,
    population::{
        compress_complexes, parameter_stats, random_point, sort_points, ParameterStats, Point,
        SimplexSampler,
    },
    rng::DuanRng,
};

/// Final parameter estimate, criterion value, and search metadata.
#[derive(Clone, Debug, PartialEq)]
pub struct OptimizationResult {
    /// Best point found by the optimization search.
    pub best_x: Vec<f64>,
    /// Function value of `best_x`.
    pub best_f: f64,
    /// Number of function evaluations used.
    pub evaluations: usize,
    /// Number of completed shuffling loops.
    pub loops: usize,
    /// Reason the search terminated.
    pub termination: TerminationReason,
    /// Results recorded for the initial population and each shuffling loop.
    pub history: Vec<HistoryEntry>,
}

/// Population summary recorded after shuffling loops.
#[derive(Clone, Debug, PartialEq)]
pub struct HistoryEntry {
    /// Shuffling-loop index. The initial population is recorded as loop `0`.
    pub loop_index: usize,
    /// Number of function evaluations used at this loop.
    pub evaluations: usize,
    /// Number of complexes in the current population.
    pub complexes: usize,
    /// Best criterion value in the current population.
    pub best_f: f64,
    /// Worst criterion value in the current population.
    pub worst_f: f64,
    /// Normalized geometric mean of parameter ranges.
    pub geometric_range: f64,
    /// Best point in the current population.
    pub best_x: Vec<f64>,
}

/// Search termination condition.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TerminationReason {
    /// Search terminated because the limit on the maximum number of trials was
    /// exceeded.
    MaxEvaluations,
    /// Optimization terminated because the criterion value has not changed by
    /// the configured percentage in the configured number of shuffling loops.
    ObjectiveConvergence,
    /// Optimization terminated because the population has converged into a
    /// sufficiently small percentage of the feasible space.
    ParameterConvergence,
}

/// Run the Shuffled Complex Evolution method for global optimization.
///
/// The objective function receives a parameter set and must return the
/// criterion value to minimize. `lower` and `upper` are the lower and upper
/// bounds on the parameters.
///
/// This follows Duan's SCE-UA main routine: generate an initial population,
/// arrange points in order of increasing function value, evolve complexes,
/// shuffle, and stop when one of the convergence checks is satisfied.
///
/// Source routine: <https://github.com/naddor/fuse/blob/e5fe0fbed82125eec4711854e1c5492da254df41/build/FUSE_SRC/FUSE_SCE/sce.f#L152-L399>
pub fn minimize<F>(
    mut objective: F,
    lower: &[f64],
    upper: &[f64],
    config: Config,
) -> Result<OptimizationResult, SceuaError>
where
    F: FnMut(&[f64]) -> f64,
{
    validate_problem(lower, upper, &config)?;
    let resolved = config.resolve(lower.len())?;
    let mut rng = DuanRng::new(resolved.seed);
    let (population, evaluations) =
        initialize_population_serial(&mut objective, lower, upper, &config, resolved, &mut rng)?;
    continue_minimize(
        objective,
        lower,
        upper,
        resolved,
        population,
        evaluations,
        rng,
    )
}

fn initialize_population_serial<F>(
    objective: &mut F,
    lower: &[f64],
    upper: &[f64],
    config: &Config,
    resolved: ResolvedConfig,
    rng: &mut DuanRng,
) -> Result<(Vec<Point>, usize), SceuaError>
where
    F: FnMut(&[f64]) -> f64,
{
    let target_population = resolved.complexes * resolved.points_per_complex;
    let mut evaluations = 0usize;
    let mut population = Vec::with_capacity(target_population);

    if config.include_initial {
        let initial = config
            .initial_point
            .as_deref()
            .ok_or(SceuaError::InvalidConfig(
                "include_initial requires initial_point",
            ))?;
        population.push(evaluate_point(objective, initial, &mut evaluations)?);
    }

    while population.len() < target_population && evaluations < resolved.max_evaluations {
        let point = random_point(lower, upper, rng);
        population.push(evaluate_owned_point(objective, point, &mut evaluations)?);
    }

    Ok((population, evaluations))
}

fn continue_minimize<F>(
    mut objective: F,
    lower: &[f64],
    upper: &[f64],
    resolved: ResolvedConfig,
    mut population: Vec<Point>,
    evaluations: usize,
    mut rng: DuanRng,
) -> Result<OptimizationResult, SceuaError>
where
    F: FnMut(&[f64]) -> f64,
{
    sort_points(&mut population);
    let mut current_complexes = resolved.complexes;
    let mut evaluations = evaluations;
    let mut history = Vec::new();
    let mut current_stats = parameter_stats(&population, lower, upper);
    push_history(
        &mut history,
        0,
        evaluations,
        current_complexes,
        &population,
        &current_stats,
    );

    if evaluations >= resolved.max_evaluations {
        return Ok(result(
            population,
            evaluations,
            0,
            TerminationReason::MaxEvaluations,
            history,
        ));
    }

    if current_stats.geometric_range <= resolved.parameter_epsilon {
        return Ok(result(
            population,
            evaluations,
            0,
            TerminationReason::ParameterConvergence,
            history,
        ));
    }

    let mut best_by_loop = Vec::new();
    let mut sampler = SimplexSampler::default();
    let mut cce_scratch = CceScratch::default();
    let mut loops = 0usize;

    loop {
        loops += 1;

        for complex_index in 0..current_complexes {
            let layout = ComplexLayout::new(complex_index, current_complexes);
            for _ in 0..resolved.evolution_steps {
                if evaluations >= resolved.max_evaluations {
                    break;
                }

                let simplex_indices =
                    sampler.sample(resolved.points_per_complex, resolved.simplex_size, &mut rng);

                if let Some(changed_rank) = evolve_simplex(
                    &mut population,
                    layout,
                    simplex_indices,
                    lower,
                    upper,
                    &current_stats.normalized_std,
                    &mut cce_scratch,
                    &mut rng,
                    &mut evaluations,
                    resolved.max_evaluations,
                    &mut objective,
                )? {
                    reposition_complex_point(
                        &mut population,
                        complex_index,
                        current_complexes,
                        resolved.points_per_complex,
                        changed_rank,
                    );
                }
            }

            if evaluations >= resolved.max_evaluations {
                break;
            }
        }

        sort_points(&mut population);
        let next_stats = parameter_stats(&population, lower, upper);
        push_history(
            &mut history,
            loops,
            evaluations,
            current_complexes,
            &population,
            &next_stats,
        );
        best_by_loop.push(population[0].value);

        if evaluations >= resolved.max_evaluations {
            return Ok(result(
                population,
                evaluations,
                loops,
                TerminationReason::MaxEvaluations,
                history,
            ));
        }

        if best_by_loop.len() > resolved.kstop {
            let current = *best_by_loop.last().expect("best_by_loop is not empty");
            let old = best_by_loop[0];
            let denominator = (old + current).abs() / 2.0;
            let timeout = if denominator == 0.0 {
                if old == current {
                    0.0
                } else {
                    f64::INFINITY
                }
            } else {
                (old - current).abs() / denominator
            };
            if timeout < resolved.pcento {
                return Ok(result(
                    population,
                    evaluations,
                    loops,
                    TerminationReason::ObjectiveConvergence,
                    history,
                ));
            }
            best_by_loop.remove(0);
        }

        if next_stats.geometric_range <= resolved.parameter_epsilon {
            return Ok(result(
                population,
                evaluations,
                loops,
                TerminationReason::ParameterConvergence,
                history,
            ));
        }

        current_stats = next_stats;
        if current_complexes > resolved.min_complexes {
            let reduced = current_complexes - 1;
            population = compress_complexes(
                population,
                current_complexes,
                reduced,
                resolved.points_per_complex,
            );
            current_complexes = reduced;
        }
    }
}

fn validate_problem(lower: &[f64], upper: &[f64], config: &Config) -> Result<(), SceuaError> {
    if lower.len() != upper.len() {
        return Err(SceuaError::BoundsLengthMismatch {
            lower: lower.len(),
            upper: upper.len(),
        });
    }
    if lower.is_empty() {
        return Err(SceuaError::EmptyProblem);
    }
    for (index, (&lo, &hi)) in lower.iter().zip(upper).enumerate() {
        if !lo.is_finite() || !hi.is_finite() || hi <= lo {
            return Err(SceuaError::InvalidBounds {
                index,
                lower: lo,
                upper: hi,
            });
        }
    }
    if let Some(initial) = &config.initial_point {
        if initial.len() != lower.len() {
            return Err(SceuaError::InitialPointLengthMismatch {
                expected: lower.len(),
                actual: initial.len(),
            });
        }
        for (index, ((&value, &lo), &hi)) in initial.iter().zip(lower).zip(upper).enumerate() {
            if !value.is_finite() || value < lo || value > hi {
                return Err(SceuaError::InvalidBounds {
                    index,
                    lower: lo,
                    upper: hi,
                });
            }
        }
    }
    Ok(())
}

fn evaluate_point<F>(
    objective: &mut F,
    point: &[f64],
    evaluations: &mut usize,
) -> Result<Point, SceuaError>
where
    F: FnMut(&[f64]) -> f64,
{
    let value = objective(point);
    *evaluations += 1;
    if value.is_finite() {
        Ok(Point {
            x: point.to_vec(),
            value,
        })
    } else {
        Err(SceuaError::NonFiniteObjective { value })
    }
}

fn evaluate_owned_point<F>(
    objective: &mut F,
    point: Vec<f64>,
    evaluations: &mut usize,
) -> Result<Point, SceuaError>
where
    F: FnMut(&[f64]) -> f64,
{
    let value = objective(&point);
    *evaluations += 1;
    if value.is_finite() {
        Ok(Point { x: point, value })
    } else {
        Err(SceuaError::NonFiniteObjective { value })
    }
}

fn reposition_complex_point(
    population: &mut [Point],
    complex_index: usize,
    complexes: usize,
    points_per_complex: usize,
    mut rank: usize,
) {
    while rank > 0 {
        let current = rank * complexes + complex_index;
        let previous = (rank - 1) * complexes + complex_index;
        if population[current]
            .value
            .total_cmp(&population[previous].value)
            .is_lt()
        {
            population.swap(current, previous);
            rank -= 1;
        } else {
            break;
        }
    }

    while rank + 1 < points_per_complex {
        let current = rank * complexes + complex_index;
        let next = (rank + 1) * complexes + complex_index;
        if population[next]
            .value
            .total_cmp(&population[current].value)
            .is_lt()
        {
            population.swap(current, next);
            rank += 1;
        } else {
            break;
        }
    }
}

fn push_history(
    history: &mut Vec<HistoryEntry>,
    loop_index: usize,
    evaluations: usize,
    complexes: usize,
    population: &[Point],
    stats: &ParameterStats,
) {
    let best = &population[0];
    let worst = population.last().expect("population is not empty");
    history.push(HistoryEntry {
        loop_index,
        evaluations,
        complexes,
        best_f: best.value,
        worst_f: worst.value,
        geometric_range: stats.geometric_range,
        best_x: best.x.clone(),
    });
}

fn result(
    population: Vec<Point>,
    evaluations: usize,
    loops: usize,
    termination: TerminationReason,
    history: Vec<HistoryEntry>,
) -> OptimizationResult {
    let best = &population[0];
    OptimizationResult {
        best_x: best.x.clone(),
        best_f: best.value,
        evaluations,
        loops,
        termination,
        history,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn point(x: f64, value: f64) -> Point {
        Point { x: vec![x], value }
    }

    #[test]
    fn reposition_complex_point_matches_stable_sort_order() {
        let mut population = vec![
            point(0.0, 1.0),
            point(1.0, 2.0),
            point(2.0, 2.0),
            point(3.0, 3.0),
        ];
        population[1].value = 3.0;
        let mut expected = population.clone();
        sort_points(&mut expected);

        reposition_complex_point(&mut population, 0, 1, 4, 1);

        assert_eq!(population, expected);
    }

    #[test]
    fn reposition_complex_point_uses_strided_complex_layout() {
        let mut population = vec![
            point(0.0, 0.0),
            point(10.0, 1.0),
            point(1.0, 2.0),
            point(11.0, 5.0),
            point(2.0, 4.0),
            point(12.0, 6.0),
        ];
        population[3].value = 7.0;

        reposition_complex_point(&mut population, 1, 2, 3, 1);

        assert_eq!(population[1], point(10.0, 1.0));
        assert_eq!(population[3], point(12.0, 6.0));
        assert_eq!(population[5], point(11.0, 7.0));
    }

    #[test]
    fn minimize_rejects_mismatched_bounds() {
        let err = minimize(|x| x[0], &[0.0], &[1.0, 2.0], Config::default()).unwrap_err();
        assert_eq!(err, SceuaError::BoundsLengthMismatch { lower: 1, upper: 2 });
    }

    // Full optimiser test identical to the Fortran version.
    // https://github.com/naddor/fuse/blob/e5fe0fbed82125eec4711854e1c5492da254df41/build/FUSE_SRC/FUSE_SCE/sce.f#L152-L399

    #[test]
    fn minimize_converges_on_two_dimensional_sphere() {
        let config = Config {
            max_evaluations: 5_000,
            kstop: 5,
            pcento: 1.0e-8,
            seed: 1969,
            complexes: 5,
            ..Config::default()
        };
        let result = minimize(
            |x| x.iter().map(|value| value * value).sum::<f64>(),
            &[-5.0, -5.0],
            &[5.0, 5.0],
            config,
        )
        .unwrap();

        assert!(result.best_f < 1.0e-6, "{result:?}");
        assert!(matches!(
            result.termination,
            TerminationReason::ObjectiveConvergence
                | TerminationReason::ParameterConvergence
                | TerminationReason::MaxEvaluations
        ));
    }
}