Struct argmin::core::PopulationState
source · [−]pub struct PopulationState<P, F> {Show 16 fields
pub individual: Option<P>,
pub prev_individual: Option<P>,
pub best_individual: Option<P>,
pub prev_best_individual: Option<P>,
pub cost: F,
pub prev_cost: F,
pub best_cost: F,
pub prev_best_cost: F,
pub target_cost: F,
pub population: Option<Vec<P>>,
pub iter: u64,
pub last_best_iter: u64,
pub max_iters: u64,
pub counts: HashMap<String, u64>,
pub time: Option<Duration>,
pub termination_reason: TerminationReason,
}
Expand description
Maintains the state from iteration to iteration of a population-based solver
This struct is passed from one iteration of an algorithm to the next.
Keeps track of
- individual of current and previous iteration
- best individual of current and previous iteration
- current and previous best cost function value
- target cost function value
- population (for population based algorithms)
- current iteration number
- iteration number where the last best individual was found
- maximum number of iterations that will be executed
- problem function evaluation counts
- elapsed time
- termination reason (set to
TerminationReason::NotTerminated
if not terminated yet)
Fields
individual: Option<P>
Current individual vector
prev_individual: Option<P>
Previous individual vector
best_individual: Option<P>
Current best individual vector
prev_best_individual: Option<P>
Previous best individual vector
cost: F
Current cost function value
prev_cost: F
Previous cost function value
best_cost: F
Current best cost function value
prev_best_cost: F
Previous best cost function value
target_cost: F
Target cost function value
population: Option<Vec<P>>
All members of the population
iter: u64
Current iteration
last_best_iter: u64
Iteration number of last best cost
max_iters: u64
Maximum number of iterations
counts: HashMap<String, u64>
Evaluation counts
time: Option<Duration>
Time required so far
termination_reason: TerminationReason
Reason of termination
Implementations
sourceimpl<P, F> PopulationState<P, F> where
Self: State<Float = F>,
F: ArgminFloat,
impl<P, F> PopulationState<P, F> where
Self: State<Float = F>,
F: ArgminFloat,
sourcepub fn individual(self, individual: P) -> Self
pub fn individual(self, individual: P) -> Self
Set best individual of current iteration. This shifts the stored individual to the previous individual.
Example
let state = state.individual(individual);
sourcepub fn cost(self, cost: F) -> Self
pub fn cost(self, cost: F) -> Self
Set the current cost function value. This shifts the stored cost function value to the previous cost function value.
Example
let state = state.cost(cost);
sourcepub fn target_cost(self, target_cost: F) -> Self
pub fn target_cost(self, target_cost: F) -> Self
Set target cost.
When this cost is reached, the algorithm will stop. The default is
Self::Float::NEG_INFINITY
.
Example
let state = state.target_cost(0.0);
sourcepub fn population(self, population: Vec<P>) -> Self
pub fn population(self, population: Vec<P>) -> Self
Set population.
A population is a Vec
of individuals.
Example
let state = state.population(vec![individual1, individual2]);
sourcepub fn get_prev_cost(&self) -> F
pub fn get_prev_cost(&self) -> F
sourcepub fn get_best_cost(&self) -> F
pub fn get_best_cost(&self) -> F
sourcepub fn get_prev_best_cost(&self) -> F
pub fn get_prev_best_cost(&self) -> F
Returns the previous best cost function value
Example
let prev_best_cost = state.get_prev_best_cost();
sourcepub fn get_target_cost(&self) -> F
pub fn get_target_cost(&self) -> F
sourcepub fn take_individual(&mut self) -> Option<P>
pub fn take_individual(&mut self) -> Option<P>
Moves the current individual out and replaces it internally with None
Example
let individual = state.take_individual(); // Option<P>
sourcepub fn get_prev_individual(&self) -> Option<&P>
pub fn get_prev_individual(&self) -> Option<&P>
Returns a reference to previous individual
Example
let prev_individual = state.get_prev_individual(); // Option<&P>
sourcepub fn take_prev_individual(&mut self) -> Option<P>
pub fn take_prev_individual(&mut self) -> Option<P>
Moves the previous individual out and replaces it internally with None
Example
let prev_individual = state.take_prev_individual(); // Option<P>
sourcepub fn get_prev_best_individual(&self) -> Option<&P>
pub fn get_prev_best_individual(&self) -> Option<&P>
Returns a reference to previous best individual
Example
let prev_best_individual = state.get_prev_best_individual(); // Option<&P>
sourcepub fn take_best_individual(&mut self) -> Option<P>
pub fn take_best_individual(&mut self) -> Option<P>
Moves the best individual out and replaces it internally with None
Example
let best_individual = state.take_best_individual(); // Option<P>
sourcepub fn take_prev_best_individual(&mut self) -> Option<P>
pub fn take_prev_best_individual(&mut self) -> Option<P>
Moves the previous best individual out and replaces it internally with None
Example
let prev_best_individual = state.take_prev_best_individual(); // Option<P>
sourcepub fn get_population(&self) -> Option<&Vec<P>>
pub fn get_population(&self) -> Option<&Vec<P>>
sourcepub fn take_population(&mut self) -> Option<Vec<P>>
pub fn take_population(&mut self) -> Option<Vec<P>>
Takes population and replaces it internally with None
.
Example
let population = state.get_population();
Trait Implementations
sourceimpl<P: Clone, F: Clone> Clone for PopulationState<P, F>
impl<P: Clone, F: Clone> Clone for PopulationState<P, F>
sourcefn clone(&self) -> PopulationState<P, F>
fn clone(&self) -> PopulationState<P, F>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<P: Debug, F: Debug> Debug for PopulationState<P, F>
impl<P: Debug, F: Debug> Debug for PopulationState<P, F>
sourceimpl<P: Default, F: Default> Default for PopulationState<P, F>
impl<P: Default, F: Default> Default for PopulationState<P, F>
sourcefn default() -> PopulationState<P, F>
fn default() -> PopulationState<P, F>
Returns the “default value” for a type. Read more
sourceimpl<'de, P, F> Deserialize<'de> for PopulationState<P, F> where
P: Deserialize<'de>,
F: Deserialize<'de>,
impl<'de, P, F> Deserialize<'de> for PopulationState<P, F> where
P: Deserialize<'de>,
F: Deserialize<'de>,
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<P: PartialEq, F: PartialEq> PartialEq<PopulationState<P, F>> for PopulationState<P, F>
impl<P: PartialEq, F: PartialEq> PartialEq<PopulationState<P, F>> for PopulationState<P, F>
sourcefn eq(&self, other: &PopulationState<P, F>) -> bool
fn eq(&self, other: &PopulationState<P, F>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourceimpl<P, F> Serialize for PopulationState<P, F> where
P: Serialize,
F: Serialize,
impl<P, F> Serialize for PopulationState<P, F> where
P: Serialize,
F: Serialize,
sourceimpl<O, P, F> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F> where
O: CostFunction<Param = P, Output = F> + SyncAlias,
P: SerializeAlias + Clone + SyncAlias + ArgminAdd<P, P> + ArgminSub<P, P> + ArgminMul<F, P> + ArgminZeroLike + ArgminRandom + ArgminMinMax,
F: ArgminFloat,
impl<O, P, F> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F> where
O: CostFunction<Param = P, Output = F> + SyncAlias,
P: SerializeAlias + Clone + SyncAlias + ArgminAdd<P, P> + ArgminSub<P, P> + ArgminMul<F, P> + ArgminZeroLike + ArgminRandom + ArgminMinMax,
F: ArgminFloat,
sourcefn next_iter(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>
fn next_iter(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>
Perform one iteration of algorithm
sourceconst NAME: &'static str = "Particle Swarm Optimization"
const NAME: &'static str = "Particle Swarm Optimization"
Name of the solver. Mainly used in Observers.
sourcefn init(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>
fn init(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>
Initializes the algorithm. Read more
sourcefn terminate_internal(&mut self, state: &I) -> TerminationReason
fn terminate_internal(&mut self, state: &I) -> TerminationReason
Checks whether basic termination reasons apply. Read more
sourcefn terminate(&mut self, _state: &I) -> TerminationReason
fn terminate(&mut self, _state: &I) -> TerminationReason
Used to implement stopping criteria, in particular criteria which are not covered by
(terminate_internal
. Read more
sourceimpl<P, F> State for PopulationState<P, F> where
P: Clone,
F: ArgminFloat,
impl<P, F> State for PopulationState<P, F> where
P: Clone,
F: ArgminFloat,
type Param = P
type Param = P
Type of an individual
type Float = F
type Float = F
Floating point precision
sourcefn new() -> Self
fn new() -> Self
Create a new PopulationState instance
Example
let state: PopulationState<Vec<f64>, f64> = PopulationState::new();
sourcefn update(&mut self)
fn update(&mut self)
Checks if the current individual is better than the previous best individual. If a new best individual was found, the state is updated accordingly.
Example
let mut state: PopulationState<Vec<f64>, f64> = PopulationState::new();
// Simulating a new, better individual
state.best_individual = Some(vec![1.0f64]);
state.best_cost = 10.0;
state.individual = Some(vec![2.0f64]);
state.cost = 5.0;
// Calling update
state.update();
// Check if update was successful
assert_eq!(state.best_individual.as_ref().unwrap()[0], 2.0f64);
assert_eq!(state.best_cost.to_ne_bytes(), state.best_cost.to_ne_bytes());
assert!(state.is_best());
For algorithms which do not compute the cost function, every new individual will be the new best:
let mut state: PopulationState<Vec<f64>, f64> = PopulationState::new();
// Simulating a new, better individual
state.best_individual = Some(vec![1.0f64]);
state.individual = Some(vec![2.0f64]);
// Calling update
state.update();
// Check if update was successful
assert_eq!(state.best_individual.as_ref().unwrap()[0], 2.0f64);
assert_eq!(state.best_cost.to_ne_bytes(), state.best_cost.to_ne_bytes());
assert!(state.is_best());
sourcefn get_param(&self) -> Option<&P>
fn get_param(&self) -> Option<&P>
Returns a reference to the current individual
Example
let individual = state.get_param(); // Option<&P>
sourcefn get_best_param(&self) -> Option<&P>
fn get_best_param(&self) -> Option<&P>
Returns a reference to the current best individual
Example
let best_individual = state.get_best_param(); // Option<&P>
sourcefn terminate_with(self, reason: TerminationReason) -> Self
fn terminate_with(self, reason: TerminationReason) -> Self
Sets the termination reason (default: TerminationReason::NotTerminated
)
Example
let state = state.terminate_with(TerminationReason::MaxItersReached);
sourcefn get_best_cost(&self) -> Self::Float
fn get_best_cost(&self) -> Self::Float
sourcefn get_target_cost(&self) -> Self::Float
fn get_target_cost(&self) -> Self::Float
sourcefn get_last_best_iter(&self) -> u64
fn get_last_best_iter(&self) -> u64
Returns iteration number of last best individual
Example
let last_best_iter = state.get_last_best_iter();
sourcefn get_max_iters(&self) -> u64
fn get_max_iters(&self) -> u64
sourcefn get_termination_reason(&self) -> TerminationReason
fn get_termination_reason(&self) -> TerminationReason
sourcefn increment_iter(&mut self)
fn increment_iter(&mut self)
sourcefn func_counts<O>(&mut self, problem: &Problem<O>)
fn func_counts<O>(&mut self, problem: &Problem<O>)
Set all function evaluation counts to the evaluation counts of another Problem
.
state.func_counts(&problem);
sourcefn get_func_counts(&self) -> &HashMap<String, u64>
fn get_func_counts(&self) -> &HashMap<String, u64>
sourcefn is_best(&self) -> bool
fn is_best(&self) -> bool
Returns whether the current individual is also the best individual found so far.
Example
let is_best = state.is_best();
sourcefn terminated(&self) -> bool
fn terminated(&self) -> bool
Return whether the algorithm has terminated or not
impl<P: Eq, F: Eq> Eq for PopulationState<P, F>
impl<P, F> StructuralEq for PopulationState<P, F>
impl<P, F> StructuralPartialEq for PopulationState<P, F>
Auto Trait Implementations
impl<P, F> RefUnwindSafe for PopulationState<P, F> where
F: RefUnwindSafe,
P: RefUnwindSafe,
impl<P, F> Send for PopulationState<P, F> where
F: Send,
P: Send,
impl<P, F> Sync for PopulationState<P, F> where
F: Sync,
P: Sync,
impl<P, F> Unpin for PopulationState<P, F> where
F: Unpin,
P: Unpin,
impl<P, F> UnwindSafe for PopulationState<P, F> where
F: UnwindSafe,
P: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more