pub struct RandomRestart<L> { /* private fields */ }Expand description
Random-restart wrapper around an inner LocalSearch.
Runs the wrapped searcher restarts + 1 times — once from the unperturbed
input and restarts times from Gaussian-perturbed, bounds-clamped starting
points — and returns the argmax over all runs (ties broken toward the
earliest run). The total evaluation budget is the product
(restarts + 1) * inner.max_iters; this wrapper adds no cap of its own.
§Type parameters
L: the wrapped inner searcher.
§Example
use burn::backend::Flex;
use rand::{rngs::StdRng, SeedableRng};
use rlevo_evolution::fitness::FitnessFn;
use rlevo_core::bounds::Bounds;
use rlevo_evolution::local_search::{
HillClimbing, HillClimbingParams, LocalSearch, RandomRestart, RandomRestartParams,
};
// Maximize the negated 2-D sphere; the optimum is the origin with fitness 0.
struct NegSphere;
impl FitnessFn<Vec<f32>> for NegSphere {
fn evaluate_one(&mut self, x: &Vec<f32>) -> f32 {
-x.iter().map(|v| v * v).sum::<f32>()
}
}
// Wrap hill climbing in random restart: 3 perturbed restarts + run 0.
let searcher = RandomRestart::new(HillClimbing);
let inner = HillClimbingParams::default_for(Bounds::new(-5.12, 5.12));
let mut params = RandomRestartParams::default_for(inner, Bounds::new(-5.12, 5.12));
params.restarts = 3;
let mut fitness = NegSphere;
let mut rng = StdRng::seed_from_u64(7);
let start = vec![2.5_f32, -1.5];
let start_fit: f32 = -start.iter().map(|v| v * v).sum::<f32>();
let (refined, refined_fit) =
LocalSearch::<Flex>::refine(&searcher, ¶ms, start, &mut fitness, &mut rng);
assert_eq!(refined.len(), 2); // dimensionality preserved
assert!(refined_fit >= start_fit); // monotone non-worseningImplementations§
Source§impl<L> RandomRestart<L>
impl<L> RandomRestart<L>
Trait Implementations§
Source§impl<L: Clone> Clone for RandomRestart<L>
impl<L: Clone> Clone for RandomRestart<L>
Source§fn clone(&self) -> RandomRestart<L>
fn clone(&self) -> RandomRestart<L>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl<L: Copy> Copy for RandomRestart<L>
Source§impl<L: Debug> Debug for RandomRestart<L>
impl<L: Debug> Debug for RandomRestart<L>
Source§impl<B: Backend, L: LocalSearch<B>> LocalSearch<B> for RandomRestart<L>
impl<B: Backend, L: LocalSearch<B>> LocalSearch<B> for RandomRestart<L>
Source§fn refine(
&self,
params: &RandomRestartParams<L::Params>,
genome: Vec<f32>,
fitness_fn: &mut dyn FitnessFn<Vec<f32>>,
rng: &mut dyn Rng,
) -> (Vec<f32>, f32)
fn refine( &self, params: &RandomRestartParams<L::Params>, genome: Vec<f32>, fitness_fn: &mut dyn FitnessFn<Vec<f32>>, rng: &mut dyn Rng, ) -> (Vec<f32>, f32)
§Panics
Panics if params.restarts > 0 and params.perturbation is not strictly
positive; see refine_impl.
Source§fn refine_with_known_fitness(
&self,
params: &RandomRestartParams<L::Params>,
genome: Vec<f32>,
known_fitness: f32,
fitness_fn: &mut dyn FitnessFn<Vec<f32>>,
rng: &mut dyn Rng,
) -> (Vec<f32>, f32)
fn refine_with_known_fitness( &self, params: &RandomRestartParams<L::Params>, genome: Vec<f32>, known_fitness: f32, fitness_fn: &mut dyn FitnessFn<Vec<f32>>, rng: &mut dyn Rng, ) -> (Vec<f32>, f32)
Forwards known_fitness to run 0 (the unperturbed input) so its inner
searcher skips its seeding eval; perturbed runs are unaffected. See
refine_impl.
§Panics
Panics if params.restarts > 0 and params.perturbation is not strictly
positive; see refine_impl.
Source§type Params = RandomRestartParams<<L as LocalSearch<B>>::Params>
type Params = RandomRestartParams<<L as LocalSearch<B>>::Params>
Static configuration for a refinement run (bounds, budget, step
sizes, …). Cloned by a memetic wrapper once per generation.
Auto Trait Implementations§
impl<L> Freeze for RandomRestart<L>where
L: Freeze,
impl<L> RefUnwindSafe for RandomRestart<L>where
L: RefUnwindSafe,
impl<L> Send for RandomRestart<L>where
L: Send,
impl<L> Sync for RandomRestart<L>where
L: Sync,
impl<L> Unpin for RandomRestart<L>where
L: Unpin,
impl<L> UnsafeUnpin for RandomRestart<L>where
L: UnsafeUnpin,
impl<L> UnwindSafe for RandomRestart<L>where
L: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<C> CloneExpand for Cwhere
C: Clone,
impl<C> CloneExpand for Cwhere
C: Clone,
fn __expand_clone_method(&self, _scope: &mut Scope) -> C
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more