17. Global and Derivative-Free Solver Reference
This chapter covers three interface groups: differential evolution and particle swarm on bounded boxes, bounded derivative-free local search, and the shared budget and observer machinery. The theory is in chapters 8 and 9.
17.1 Shared inputs: Bounds and value_only
All three method families require finite box constraints:
API pages (docs.rs): Bounds, value_only.
#![allow(unused)]
fn main() {
use nlopt4r::Bounds;
let bounds = Bounds::uniform(3, -4.0, 4.0)?; // same lower and upper bound
let bounds = Bounds::new(
vec![-4.0, -1.0], vec![4.0, 1.0],
)?; // per-coordinate bounds
Ok::<(), nlopt4r::Error>(())
}
The objective enters through value_only:
#![allow(unused)]
fn main() {
use nlopt4r::{EvaluationError, value_only};
let sphere = value_only(|x: &[f64]| {
Ok::<_, EvaluationError>(x.iter().map(|v| v * v).sum())
});
}
value_only returns an explicit error on a gradient request; the three modules request objective values alone.
17.2 Differential evolution
pub fn differential_evolution<P: Evaluator + ?Sized>(
p: &P,
bounds: &Bounds,
options: &DeOptions,
) -> Result<GlobalSolution, Error>
API pages (docs.rs): differential_evolution, DeOptions.
Algorithm state:
- a population of \(N\) candidates updates synchronously: every donor vector reads the preceding generation;
- mutation \(v_i=x_{r_0}+F(x_{r_1}-x_{r_2})\), with three distinct donor indices excluding the target index;
- binomial crossover keeps at least one donor coordinate;
- selection accepts candidates that are at least as good (ties accept the trial);
- out-of-bound coordinates repair toward the midpoint between the target point and the coordinate boundary.
DeOptions defaults:
| Field | Default | Notes |
|---|---|---|
budget | BudgetOptions::default() | 10000 objective calls, no target value |
seed | 0 | ChaCha8 seed |
population_size | 40 | At least 4 |
max_generations | 1000 | Generation zero still runs initialization |
weight | 0.8 | \(F\in(0,2]\) |
crossover | 0.9 | \(CR\in[0,1]\) |
initial_population | None | Optional initial population of exact size, finite and feasible |
#![allow(unused)]
fn main() {
use nlopt4r::{Bounds, Vector, value_only, global::{differential_evolution, DeOptions}};
let problem = value_only(|x: &[f64]| {
Ok::<_, nlopt4r::EvaluationError>((x[0] - 0.3).powi(2) + (x[1] + 0.2).powi(2))
});
let mut options = DeOptions::default();
options.seed = 7;
options.budget.target_value = Some(1e-10);
let result = differential_evolution(&problem, &Bounds::uniform(2, -1.0, 1.0)?, &options)?;
assert!(result.target_reached());
Ok::<(), nlopt4r::Error>(())
}
17.3 Particle swarm
pub fn particle_swarm<P: Evaluator + ?Sized>(
p: &P,
bounds: &Bounds,
options: &PsoOptions,
) -> Result<GlobalSolution, Error>
API pages (docs.rs): particle_swarm, PsoOptions.
Update rules:
where \(p_i\) is the personal best, \(g\) the previous generation’s global-best snapshot, and \(r_1,r_2\) independent uniform draws per coordinate.
- velocity components clip to \(\pm c_v,(u_j-l_j)\);
- positions project into the box; a projected coordinate resets its velocity to zero;
- personal bests update on strict improvement;
- initial velocities are zero.
PsoOptions defaults:
| Field | Default | Notes |
|---|---|---|
budget | BudgetOptions::default() | 10000 objective calls |
seed | 0 | ChaCha8 seed |
population_size | 40 | At least 2 |
max_generations | 1000 | Initialization counted separately |
inertia | 0.7 | \(\omega\in[0,1)\) |
cognitive | 1.5 | \(c_1\ge0\) |
social | 1.5 | \(c_2\ge0\) |
velocity_limit | 0.2 | \(c_v\in(0,1]\) |
initial_population | None | Feasible initial positions of exact size |
17.4 Observers and cancellation
Both algorithms have *_with_observer variants (differential_evolution_with_observer and particle_swarm_with_observer); the observer runs after each valid evaluation and returns Control::Continue or Control::Stop:
API pages (docs.rs): differential_evolution_with_observer, particle_swarm_with_observer, EvaluationEvent, Control, SearchTermination.
#![allow(unused)]
fn main() {
use nlopt4r::{Bounds, value_only, core::{Control, EvaluationEvent, SearchTermination},
global::{DeOptions, differential_evolution_with_observer}};
let problem = value_only(|x: &[f64]| Ok::<_, nlopt4r::EvaluationError>(x[0] * x[0]));
let mut observer = |event: &EvaluationEvent<'_>| {
Ok(if event.evaluations.objective >= 5 { Control::Stop } else { Control::Continue })
};
let result = differential_evolution_with_observer(
&problem,
&Bounds::uniform(1, -1.0, 1.0)?,
&DeOptions::default(),
Some(&mut observer),
)?;
assert_eq!(result.termination_reason, SearchTermination::Cancelled);
assert_eq!(result.evaluations.objective, 5);
Ok::<(), nlopt4r::Error>(())
}
An observer error reports the search with the best valid point as its checkpoint.
17.5 Reading global results
API page (docs.rs): GlobalSolution.
GlobalSolution fields:
| Field | Meaning |
|---|---|
x, value | Best point and value over all valid evaluations |
generations | Complete generations, with initialization and an unfinished generation excluded |
evaluations | Attempted-call counts |
termination_reason | TargetReached, MaxEvaluations, MaxIterations, Stagnated or Cancelled |
seed | Seed of this run |
diversity | Maximum over coordinates of the population spread divided by the domain width |
target_reached() corresponds to the TargetReached status alone. The returned point is the best evaluated point; it is not a certified global optimum. The measured tables of chapter 13 give empirical hit rates and ERT per problem class.
17.6 Bounded derivative-free local search
pub fn pattern_search<P: Evaluator + ?Sized>(
p: &P,
x0: &Vector,
bounds: &Bounds,
options: &Options,
) -> Result<Solution, Error>
pub fn nelder_mead<P: Evaluator + ?Sized>(
p: &P,
x0: &Vector,
bounds: &Bounds,
options: &Options,
) -> Result<Solution, Error>
API pages (docs.rs): pattern_search, nelder_mead, derivative_free::Options, derivative_free::Solution.
pattern_search: positive and negative coordinate trials around the current best point, halving the step after a failed poll; zero-width coordinates are skipped, and points left unchanged by projection are skipped;nelder_mead: the initial simplex has one vertex per free coordinate, with reflection 1, expansion 2, contraction 1/2 and shrink 1/2; projection can compress the simplex;- the initial point
x0must be finite, of the correct dimension and feasible.
derivative_free::Options defaults:
| Field | Default |
|---|---|
budget | 10000 objective calls |
max_iterations | 2000 |
initial_step | 0.1 (fraction of the coordinate width, in \((0,1]\)) |
step_tolerance | 1e-8 (normalized step/simplex diameter) |
#![allow(unused)]
fn main() {
use nlopt4r::{Bounds, Vector, value_only, derivative_free::{nelder_mead, Options}};
let problem = value_only(|x: &[f64]| Ok::<_, nlopt4r::EvaluationError>((x[0] - 0.25).powi(2)));
let mut options = Options::default();
options.budget.target_value = Some(1e-8);
let result = nelder_mead(&problem, &Vector::zeros(1), &Bounds::uniform(1, -1.0, 1.0)?, &options)?;
assert!(result.value <= 1e-8);
Ok::<(), nlopt4r::Error>(())
}
Solution fields: x, value, iterations, evaluations, termination_reason. A step or diameter below the threshold reports Stagnated, a state recording that the step/diameter fell below the threshold; the optimality certificate comes from separate criteria.
17.7 Budget and stopping semantics
API pages (docs.rs): BudgetOptions, SearchTermination.
BudgetOptions.max_evaluationsis a strict cap, and the count includes initialization and failed callbacks;- with
target_valuepresent, any valid point reaching \(f(x)\le\) target stops the search; max_generations = 0still runs the initial population evaluation;- observer cancellation takes effect before the next objective call;
- every stop reports as a
SearchTermination; gradient convergence status stays with gradient criteria.
Theory background
- Differential evolution and particle swarm: see chapter 9.
- Pattern search and Nelder-Mead: see chapter 8.
- Budgets, target values and cancellation: see chapter 12.
- Benchmark protocol and measured hit rates: see chapter 13.