///|
/// Seeded synchronous global-best particle swarm on a finite box.
/// Velocities start at zero and are limited to `velocity_limit` times the
/// coordinate's box width. A particle that crosses a bound is clamped and
/// its velocity in that coordinate is reset. All particles in a generation
/// use the same best point from the previous generation.
///
/// The returned best point includes partial initialization and generations;
/// reaching a target does not certify a global optimum. Set
/// `rust_compatible_rng=true` for Rust-compatible random sampling.
/// The optional observer can stop after any successful evaluation; cancellation
/// takes precedence over a target or budget reached at the same evaluation.
///
/// ```mbt check
/// test {
/// let result = @search.particle_swarm(
/// x => x[0] * x[0],
/// [-1.0],
/// [1.0],
/// seed=42UL,
/// max_evaluations=1000,
/// target_value=1.0e-6,
/// )
/// assert_true(result.termination is TargetReached)
/// }
/// ```
pub fn particle_swarm(
objective : (ArrayView[Double]) -> Double raise,
lower : ArrayView[Double],
upper : ArrayView[Double],
seed~ : UInt64 = 0UL,
population_size~ : Int32 = 40,
max_generations~ : Int32 = 1000,
max_evaluations~ : Int32 = 10000,
inertia~ : Double = 0.7,
cognitive~ : Double = 1.5,
social~ : Double = 1.5,
velocity_limit~ : Double = 0.2,
rust_compatible_rng~ : Bool = false,
initial_population? : Array[Array[Double]],
target_value? : Double,
observer? : (SearchEvent) -> SearchControl raise,
) -> GlobalSolution raise {
validate_search_limits(max_generations, max_evaluations, target_value)
if population_size < 2 ||
!inertia.is_finite() ||
inertia < 0.0 ||
inertia >= 1.0 ||
!cognitive.is_finite() ||
cognitive < 0.0 ||
!social.is_finite() ||
social < 0.0 ||
!velocity_limit.is_finite() ||
velocity_limit <= 0.0 ||
velocity_limit > 1.0 {
raise OptimizeError::InvalidInput("invalid particle swarm options")
}
let (lo, hi, widths) = validate_box(lower, lower, upper)
let random = population_rng(seed, rust_compatible_rng)
let mut population = sample_population(
random,
lo[:],
hi[:],
widths[:],
population_size,
initial_population,
)
let first = value_only(objective, population[0][:])
let run : ValueSearch = {
objective,
max_evaluations,
target_value,
best: population[0].copy(),
best_value: first,
evaluations: 1,
observer,
cancelled: false,
}
run.observe(population[0][:], first)
let values = [first]
if run.stop() is Some(reason) {
return global_result(run, 0, seed, population[:1], widths[:], reason)
}
for i in 1.. Array::make(widths.length(), 0.0)
]
for generation in 0.. hi[j] - point[j] {
trial[j] = hi[j]
velocities[i][j] = 0.0
} else {
trial[j] = point[j] + clipped
velocities[i][j] = clipped
}
}
let value = run.evaluate(trial)
next[i] = trial
if value < values[i] {
personal_best[i] = trial
values[i] = value
}
if run.stop() is Some(reason) {
let completed = if i + 1 == population_size {
generation + 1
} else {
generation
}
return global_result(run, completed, seed, next[:], widths[:], reason)
}
}
population = next
}
global_result(
run,
max_generations,
seed,
population[:],
widths[:],
MaxIterations,
)
}