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_status: TerminationStatus,
}
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 status

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_status: TerminationStatus

Status of optimization execution

Implementations§

Set best individual of current iteration. This shifts the stored individual to the previous individual.

Example
let state = state.individual(individual);

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);

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);

Set population.

A population is a Vec of individuals.

Example
let state = state.population(vec![individual1, individual2]);

Set maximum number of iterations

Example
let state = state.max_iters(1000);

Returns the current cost function value

Example
let cost = state.get_cost();

Returns the previous cost function value

Example
let prev_cost = state.get_prev_cost();

Returns the current best cost function value

Example
let best_cost = state.get_best_cost();

Returns the previous best cost function value

Example
let prev_best_cost = state.get_prev_best_cost();

Returns the target cost function value

Example
let target_cost = state.get_target_cost();

Moves the current individual out and replaces it internally with None

Example
let individual = state.take_individual();  // Option<P>

Returns a reference to previous individual

Example
let prev_individual = state.get_prev_individual();  // Option<&P>

Moves the previous individual out and replaces it internally with None

Example
let prev_individual = state.take_prev_individual();  // Option<P>

Returns a reference to previous best individual

Example
let prev_best_individual = state.get_prev_best_individual();  // Option<&P>

Moves the best individual out and replaces it internally with None

Example
let best_individual = state.take_best_individual();  // 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>

Returns a reference to the population

Example
let population = state.get_population();

Takes population and replaces it internally with None.

Example
let population = state.get_population();

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Perform one iteration of algorithm

Name of the solver. Mainly used in Observers.
Initializes the algorithm. Read more
Checks whether basic termination reasons apply. Read more
Used to implement stopping criteria, in particular criteria which are not covered by (terminate_internal. Read more

Type of an individual

Floating point precision

Create a new PopulationState instance

Example
let state: PopulationState<Vec<f64>, f64> = PopulationState::new();

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());

Returns a reference to the current individual

Example
let individual = state.get_param();  // Option<&P>

Returns a reference to the current best individual

Example
let best_individual = state.get_best_param();  // Option<&P>

Sets the termination status to Terminated with the given reason

Example
let state = state.terminate_with(TerminationReason::MaxItersReached);

Sets the time required so far.

Example
let state = state.time(Some(instant::Duration::new(0, 12)));

Returns current cost function value.

Example
let cost = state.get_cost();

Returns current best cost function value.

Example
let best_cost = state.get_best_cost();

Returns target cost function value.

Example
let target_cost = state.get_target_cost();

Returns current number of iterations.

Example
let iter = state.get_iter();

Returns iteration number of last best individual

Example
let last_best_iter = state.get_last_best_iter();

Returns the maximum number of iterations.

Example
let max_iters = state.get_max_iters();

Returns the termination reason.

Example
let termination_status = state.get_termination_status();

Returns the termination reason if terminated, otherwise None.

Example
let termination_reason = state.get_termination_reason();

Returns the time elapsed since the start of the optimization.

Example
let time = state.get_time();

Increments the number of iterations by one

Example
state.increment_iter();

Set all function evaluation counts to the evaluation counts of another Problem.

state.func_counts(&problem);

Returns function evaluation counts

Example
let counts = state.get_func_counts();

Returns whether the current individual is also the best individual found so far.

Example
let is_best = state.is_best();
Return whether the algorithm has terminated or not

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.