Trait Algorithm

Source
pub trait Algorithm<AlgorithmOptions: Serialize + DeserializeOwned>: Display {
Show 25 methods // Required methods fn initialise(&mut self) -> Result<(), OError>; fn evolve(&mut self) -> Result<(), OError>; fn generation(&self) -> u32; fn number_of_function_evaluations(&self) -> u32; fn name(&self) -> String; fn start_time(&self) -> &Instant; fn stopping_condition(&self) -> &StoppingCondition; fn population(&self) -> &Population; fn problem(&self) -> Arc<Problem>; fn export_history(&self) -> Option<&ExportHistory>; fn algorithm_options(&self) -> AlgorithmOptions; // Provided methods fn additional_export_data(&self) -> Option<HashMap<String, DataValue>> { ... } fn elapsed(&self) -> [u64; 3] { ... } fn elapsed_as_string(&self) -> String { ... } fn do_parallel_evaluation( individuals: &mut [Individual], nfe: &mut u32, ) -> Result<(), OError> { ... } fn do_evaluation( individuals: &mut [Individual], nfe: &mut u32, ) -> Result<(), OError> { ... } fn evaluate_individual(idx: usize, i: &mut Individual) -> Result<(), OError> { ... } fn count_unevaluated(individuals: &[Individual]) -> u32 { ... } fn run(&mut self) -> Result<(), OError> { ... } fn is_stopping_condition_met( &self, condition: &StoppingCondition, ) -> Result<bool, OError> { ... } fn get_results(&self) -> AlgorithmExport { ... } fn save_to_json( &self, destination: &PathBuf, file_prefix: Option<&str>, ) -> Result<(), OError> { ... } fn read_json_file( file: &PathBuf, ) -> Result<AlgorithmSerialisedExport<AlgorithmOptions>, OError> { ... } fn read_json_files( folder: &PathBuf, ) -> Result<Vec<AlgorithmSerialisedExport<AlgorithmOptions>>, OError> { ... } fn seed_population_from_file( problem: Arc<Problem>, name: &str, expected_individuals: usize, file: &PathBuf, ) -> Result<Population, OError> { ... }
}
Expand description

The trait to use to implement an algorithm.

Required Methods§

Source

fn initialise(&mut self) -> Result<(), OError>

Initialise the algorithm.

return: Result<(), OError>

Source

fn evolve(&mut self) -> Result<(), OError>

Evolve the population.

return: Result<(), OError>

Source

fn generation(&self) -> u32

Return the current step of the algorithm evolution.

return: u32.

Source

fn number_of_function_evaluations(&self) -> u32

Return the number of function evaluations. This is the number of times the algorithm evaluates an individual’s objectives and constraints using Algorithm::evaluate_individual. If no new solutions/individuals are chosen by an algorithm, this counter will not increase, as past solutions are already evaluated.

return: u32.

Source

fn name(&self) -> String

Return the algorithm name.

return: String.

Source

fn start_time(&self) -> &Instant

Get the time when the algorithm started.

return: &Instant.

Source

fn stopping_condition(&self) -> &StoppingCondition

Return the stopping condition.

return: &StoppingConditionType.

Source

fn population(&self) -> &Population

Return the evolved population.

return: &Population.

Source

fn problem(&self) -> Arc<Problem>

Return the problem.

return: Arc<Problem>.

Source

fn export_history(&self) -> Option<&ExportHistory>

Return the history export configuration, if provided by the algorithm.

return: Option<&ExportHistory>.

Source

fn algorithm_options(&self) -> AlgorithmOptions

Provided Methods§

Source

fn additional_export_data(&self) -> Option<HashMap<String, DataValue>>

Export additional data stored by the algorithm.

return: Option<HashMap<String, DataValue>>

Source

fn elapsed(&self) -> [u64; 3]

Get the elapsed hours, minutes and seconds since the start of the algorithm.

return: [u64; 3]. An array with the number of elapsed hours, minutes and seconds.

Source

fn elapsed_as_string(&self) -> String

Format the elapsed time as string.

return: String.

Source

fn do_parallel_evaluation( individuals: &mut [Individual], nfe: &mut u32, ) -> Result<(), OError>

Evaluate the objectives and constraints for unevaluated individuals in the population. This updates the individual data only, runs the evaluation function in threads and increase the nfe counter by the number of evaluated individuals. This returns an error if the evaluation function fails or the evaluation function does not provide a value for a problem constraints or objectives for one individual.

§Arguments
  • individuals: The individuals to evaluate.
  • nfe: The reference to the number of function evaluation counter.

return Result<(), OError>

Source

fn do_evaluation( individuals: &mut [Individual], nfe: &mut u32, ) -> Result<(), OError>

Evaluate the objectives and constraints for unevaluated individuals in the population. This updates the individual data only, runs the evaluation function in a plain loop and increase the nfe counter by the number of evaluated individuals. This returns an error if the evaluation function fails or the evaluation function does not provide a value for a problem constraints or objectives for one individual. Evaluation may be performed in threads using Self::do_parallel_evaluation.

§Arguments
  • individuals: The individuals to evaluate.
  • nfe: The reference to the number of function evaluation counter.

return Result<usize, OError>.

Source

fn evaluate_individual(idx: usize, i: &mut Individual) -> Result<(), OError>

Evaluate the objectives and constraints for one unevaluated individual. This returns an error if the evaluation function fails or the evaluation function does not provide a value for a problem constraints or objectives.

§Arguments
  • idx: The individual index.
  • individual: The individual to evaluate.

return Result<(), OError>

Source

fn count_unevaluated(individuals: &[Individual]) -> u32

Count the number on unevaluated individuals.

§Arguments
  • individuals: The individuals to check.

returns: u32

Source

fn run(&mut self) -> Result<(), OError>

Run the algorithm.

return: Result<(), OError>

Source

fn is_stopping_condition_met( &self, condition: &StoppingCondition, ) -> Result<bool, OError>

Check if the given stopping condition is met.

§Arguments
  • condition: The stopping condition type.

returns: Result<bool, OError>

Source

fn get_results(&self) -> AlgorithmExport

Get the results of the run.

return: AlgorithmExport.

Source

fn save_to_json( &self, destination: &PathBuf, file_prefix: Option<&str>, ) -> Result<(), OError>

Save the algorithm data (individuals’ objective, variables and constraints, the problem, …) to a JSON file. This returns an error if the file cannot be saved.

§Arguments
  • destination: The path to the JSON file.
  • file_prefix: A prefix to prepend at the beginning of the file name. Empty when None.

return Result<(), OError>

Source

fn read_json_file( file: &PathBuf, ) -> Result<AlgorithmSerialisedExport<AlgorithmOptions>, OError>

Read the results previously exported with Self::save_to_json.

§Arguments
  • file: The path to the JSON file exported from this library.

returns: Result<AlgorithmSerialisedExport<T>, OError>

Source

fn read_json_files( folder: &PathBuf, ) -> Result<Vec<AlgorithmSerialisedExport<AlgorithmOptions>>, OError>

Read the results from files exported during an algorithm evolution. This returns an error if the path does not exist or does not contain valid JSON files.

§Arguments
  • folder: The folder path to the JSON files.

returns: Result<Vec<AlgorithmSerialisedExport<T>>, OError>

Source

fn seed_population_from_file( problem: Arc<Problem>, name: &str, expected_individuals: usize, file: &PathBuf, ) -> Result<Population, OError>

Seed the population using the values of variables, objectives and constraints exported to a JSON file.

§Arguments
  • problem: The problem.
  • name: The algorithm name.
  • expected_individuals: The number of individuals to expect in the file. If this does not match the population size, being used in the algorithm, an error is thrown.
  • file: The path to the JSON file exported from this library.

returns: Result<Population, OError>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Algorithm<NSGA2Arg> for NSGA2

Implementation of Section IIIC of the paper.

Source§

impl Algorithm<NSGA3Arg> for NSGA3

Implementation of Section IV of the paper.