Skip to content

Commit

Permalink
feat: Add reset functions to all solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Feb 7, 2022
1 parent fb8e90e commit b12eca5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/population.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ where
}
}

/// Recreates the population with new individuals with given initializer.
pub fn reinit<R: Rng + ?Sized, I: PopulationInit<F>>(
&mut self,
f: &F,
dom: &Domain<F::Scalar>,
rng: &mut R,
initializer: &I,
) where
F: Function,
{
initializer.init_all(f, dom, rng, self.individuals.iter_mut());
self.eval(f);
self.sort();
}

/// Get the size of the population.
pub fn len(&self) -> usize {
self.individuals.len()
Expand Down
9 changes: 9 additions & 0 deletions src/solver/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ where
pub fn population(&self) -> &Population<F> {
&self.population
}

/// Resets the internal state of the solver.
pub fn reset(&mut self, f: &F, dom: &Domain<F::Scalar>)
where
F: Function,
{
self.population
.reinit(f, dom, &mut self.rng, &self.options.population_init);
}
}

/// Error returned from [`Cuckoo`] solver.
Expand Down
8 changes: 8 additions & 0 deletions src/solver/nelder_mead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ where
sort_perm: Vec::with_capacity(f.dim().value() + 1),
}
}

/// Resets the internal state of the solver.
pub fn reset(&mut self) {
// Causes simplex to be initialized again.
self.simplex.clear();
self.errors.clear();
self.sort_perm.clear();
}
}

/// Error returned from [`NelderMead`] solver.
Expand Down
11 changes: 11 additions & 0 deletions src/solver/trust_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ where
rejections_cnt: 0,
}
}

/// Resets the internal state of the solver.
pub fn reset(&mut self) {
self.delta = match self.options.delta_init {
DeltaInit::Fixed(fixed) => fixed,
DeltaInit::Estimated => F::Scalar::zero(),
};
self.mu = convert(0.5);
self.iter = 1;
self.rejections_cnt = 0;
}
}

/// Error returned from [`TrustRegion`] solver.
Expand Down

0 comments on commit b12eca5

Please sign in to comment.