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§
Sourcefn initialise(&mut self) -> Result<(), OError>
fn initialise(&mut self) -> Result<(), OError>
Initialise the algorithm.
return: Result<(), OError>
Sourcefn generation(&self) -> u32
fn generation(&self) -> u32
Return the current step of the algorithm evolution.
return: u32
.
Sourcefn number_of_function_evaluations(&self) -> u32
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
.
Sourcefn start_time(&self) -> &Instant
fn start_time(&self) -> &Instant
Get the time when the algorithm started.
return: &Instant
.
Sourcefn stopping_condition(&self) -> &StoppingCondition
fn stopping_condition(&self) -> &StoppingCondition
Return the stopping condition.
return: &StoppingConditionType
.
Sourcefn population(&self) -> &Population
fn population(&self) -> &Population
Return the evolved population.
return: &Population
.
Sourcefn export_history(&self) -> Option<&ExportHistory>
fn export_history(&self) -> Option<&ExportHistory>
Return the history export configuration, if provided by the algorithm.
return: Option<&ExportHistory>
.
fn algorithm_options(&self) -> AlgorithmOptions
Provided Methods§
Sourcefn additional_export_data(&self) -> Option<HashMap<String, DataValue>>
fn additional_export_data(&self) -> Option<HashMap<String, DataValue>>
Export additional data stored by the algorithm.
return: Option<HashMap<String, DataValue>>
Sourcefn elapsed(&self) -> [u64; 3]
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.
Sourcefn elapsed_as_string(&self) -> String
fn elapsed_as_string(&self) -> String
Format the elapsed time as string.
return: String
.
Sourcefn do_parallel_evaluation(
individuals: &mut [Individual],
nfe: &mut u32,
) -> Result<(), OError>
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>
Sourcefn do_evaluation(
individuals: &mut [Individual],
nfe: &mut u32,
) -> Result<(), OError>
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>
.
Sourcefn evaluate_individual(idx: usize, i: &mut Individual) -> Result<(), OError>
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>
Sourcefn count_unevaluated(individuals: &[Individual]) -> u32
fn count_unevaluated(individuals: &[Individual]) -> u32
Count the number on unevaluated individuals.
§Arguments
individuals
: The individuals to check.
returns: u32
Sourcefn is_stopping_condition_met(
&self,
condition: &StoppingCondition,
) -> Result<bool, OError>
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>
Sourcefn get_results(&self) -> AlgorithmExport
fn get_results(&self) -> AlgorithmExport
Get the results of the run.
return: AlgorithmExport
.
Sourcefn save_to_json(
&self,
destination: &PathBuf,
file_prefix: Option<&str>,
) -> Result<(), OError>
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 whenNone
.
return Result<(), OError>
Sourcefn read_json_file(
file: &PathBuf,
) -> Result<AlgorithmSerialisedExport<AlgorithmOptions>, OError>
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>
Sourcefn read_json_files(
folder: &PathBuf,
) -> Result<Vec<AlgorithmSerialisedExport<AlgorithmOptions>>, OError>
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>
Sourcefn seed_population_from_file(
problem: Arc<Problem>,
name: &str,
expected_individuals: usize,
file: &PathBuf,
) -> Result<Population, OError>
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.