Ultra 0.9.4
Loading...
Searching...
No Matches
ultra::basic_search< ES, E > Class Template Reference

#include <search.h>

Inheritance diagram for ultra::basic_search< ES, E >:
ultra::src::basic_search< ES, E >

Public Types

using individual_t = evaluator_individual_t<E>
using fitness_t = evaluator_fitness_t<E>
using after_generation_callback_t
using refinement_callback_t
using on_training_new_best_callback_t

Public Member Functions

 basic_search (problem &, E)
search_stats< individual_t, fitness_t > run (unsigned=1, const model_measurements< fitness_t > &={})
basic_search & after_generation (after_generation_callback_t)
basic_search & logger (search_log &) noexcept
basic_search & refinement (refinement_callback_t)
basic_search & on_training_new_best (on_training_new_best_callback_t)
basic_search & messages (bool) noexcept
basic_search & tag (const std::string &)
basic_search & stop_source (std::stop_source)
template<ValidationStrategy, class... Args>
basic_search & validation_strategy (Args &&...)
basic_search & validation_strategy (const ultra::validation_strategy &)
virtual bool is_valid () const
template<ValidationStrategy V, class... Args>
basic_search< ES, E > & validation_strategy (Args &&...args)
template<ValidationStrategy V, class... Args>
basic_search< ES, E > & validation_strategy (Args &&...args)
template<template< class > class ES, Evaluator E>
basic_search< ES, E > & validation_strategy (const ultra::validation_strategy &vs)

Protected Member Functions

virtual void after_evolution (unsigned, const std::vector< model_measurements< fitness_t > > &)
virtual model_measurements< fitness_t > calculate_metrics (const individual_t &) const
virtual void tune_parameters ()

Protected Attributes

evaluator_proxy< E > eva_
std::unique_ptr< ultra::validation_strategy > vs_ {std::make_unique<as_is_validation>()}
problem & prob_
after_generation_callback_t after_generation_callback_ {}
refinement_callback_t refinement_callback_ {}
on_training_new_best_callback_t on_training_new_best_callback_ {}

Detailed Description

template<template< class > class ES, Evaluator E>
class ultra::basic_search< ES, E >

basic_search drives the evolution.

The class offers a general / customizable search strategy.

Member Typedef Documentation

◆ after_generation_callback_t

template<template< class > class ES, Evaluator E>
using ultra::basic_search< ES, E >::after_generation_callback_t
Initial value:
ultra::after_generation_callback_t<individual_t, fitness_t>

◆ on_training_new_best_callback_t

template<template< class > class ES, Evaluator E>
using ultra::basic_search< ES, E >::on_training_new_best_callback_t
Initial value:
ultra::on_new_best_callback_t<individual_t, fitness_t>

◆ refinement_callback_t

template<template< class > class ES, Evaluator E>
using ultra::basic_search< ES, E >::refinement_callback_t
Initial value:
std::function< std::optional< evaluator_fitness_t< E > >( evaluator_individual_t< E > &, const E &, const parameters::refinement_parameters &)> refinement_callback_t
Definition evolution_callbacks.h:46

Constructor & Destructor Documentation

◆ basic_search()

template<template< class > class ES, Evaluator E>
basic_search::basic_search ( problem & prob,
E eva )
Parameters
[in]probthe problem we're working on. The lifetime of prob must exceed lifetime of this class
[in]evaevaluator used during evolution to build the evaluator proxy

Member Function Documentation

◆ after_evolution()

template<template< class > class ES, Evaluator E>
void basic_search::after_evolution ( unsigned run,
const std::vector< model_measurements< fitness_t > > & mm )
protectedvirtual

Template method of the run member function called at the end of each run.

Parameters
[in]runcurrent run
[in]mmmeasurements related to the best program of the current run (training set measurements and possibly validation set)

◆ after_generation()

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & basic_search::after_generation ( after_generation_callback_t f)

Sets a callback function executed at the end of every generation.

Parameters
[in]fcallback function
Returns
a reference to this object (method chaining / fluent interface)

◆ calculate_metrics()

template<template< class > class ES, Evaluator E>
model_measurements< typename basic_search< ES, E >::fitness_t > basic_search::calculate_metrics ( const individual_t & prg) const
nodiscardprotectedvirtual

Calculates and stores the fitness of the best individual so far.

Parameters
[in]prgbest individual from the evolution run just finished
Returns
measurements about the individual

Specializations of this method can calculate further / distinct problem-specific metrics regarding the candidate solution.

If a validation set / simulation is available, it's used for the calculations.

Reimplemented in ultra::src::basic_search< ES, E >, ultra::src::basic_search< alps_es, class_evaluator_t >, and ultra::src::basic_search< alps_es, reg_evaluator_t >.

◆ is_valid()

template<template< class > class ES, Evaluator E>
bool basic_search::is_valid ( ) const
nodiscardvirtual

◆ logger()

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & basic_search::logger ( search_log & sl)
noexcept

Sets the search/evolution logger.

Parameters
[in]sllogger
Returns
a reference to this object (method chaining / fluent interface)
Remarks
Logger must be set before calling run. By default, data logging is disabled.

◆ on_training_new_best()

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & basic_search::on_training_new_best ( on_training_new_best_callback_t f)

Hook invoked whenever the evolution discovers a new training-set best.

Parameters
[in]fcallback function
Returns
a reference to this object (method chaining / fluent interface)

◆ run()

template<template< class > class ES, Evaluator E>
search_stats< typename basic_search< ES, E >::individual_t, typename basic_search< ES, E >::fitness_t > basic_search::run ( unsigned n = 1,
const model_measurements< fitness_t > & threshold = {} )

Executes a given number of evolutionary-runs possibly saving good runs.

Parameters
[in]nnumber of runs
[in]thresholdused to identify successfully learned (matched, classified, resolved...) examples
Returns
a summary of the search

◆ stop_source()

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & basic_search::stop_source ( std::stop_source ss)

Set a stop source for performing cooperative task shutdown.

Parameters
[in]ssstop source to issue a stop request
Returns
a reference to this object (method chaining / fluent interface)
Note
std::stop_source is copyable; the copies all share the same internal stop-state (along with any stop_tokens created from them). So there's no reason to pass them by reference.

◆ tag()

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & basic_search::tag ( const std::string & t)

Sets the identification tag for this object.

Parameters
[in]tidentification tag
Returns
a reference to this object (method chaining / fluent interface)

The tag is used to identify this object when multiple searches are performed in parallel.

◆ tune_parameters()

template<template< class > class ES, Evaluator E>
void basic_search::tune_parameters ( )
protectedvirtual

Tries to tune search parameters for the current problem.

Parameter tuning is a typical approach to algorithm design. Such tuning is done by experimenting with different values and selecting the ones that give the best results on the test problems at hand.

However, the number of possible parameters and their different values means that this is a very complex and time-consuming task; it is something we do not want users to worry about (power users can force many parameters, but our idea is "simple by default").

So if user sets an environment parameter he will force the search class to use it as is. Otherwise this function will try to guess a good starting point and changes its hint after every run. The code is a mix of black magic, experience, common logic and randomness but it seems reasonable.

Note
It has been formally proven, in the No-Free-Lunch theorem, that it is impossible to tune a search algorithm such that it will have optimal settings for all possible problems, but parameters can be properly set for a given problem.
See also

Tries to tune search parameters for the current problem.

General scheme:

  • honour user specified parameters;
  • try to derive meaningful values when possible (directly here or via ES<E>::shape());
  • fall back to very general values from parameters::init().

Reimplemented in ultra::de::search< E >, ultra::ga::search< E >, ultra::hga::search< E >, ultra::src::basic_search< ES, E >, ultra::src::basic_search< alps_es, class_evaluator_t >, and ultra::src::basic_search< alps_es, reg_evaluator_t >.

◆ validation_strategy() [1/4]

template<template< class > class ES, Evaluator E>
template<ValidationStrategy V, class... Args>
basic_search< ES, E > & ultra::basic_search< ES, E >::validation_strategy ( Args &&... args)

Builds and sets the active validation strategy.

Parameters
[in]argsparameters for the validation strategy
Returns
a reference to the search class (used for method chaining)

◆ validation_strategy() [2/4]

template<template< class > class ES, Evaluator E>
template<ValidationStrategy V, class... Args>
basic_search< ES, E > & ultra::basic_search< ES, E >::validation_strategy ( Args &&... args)

Builds and sets the active validation strategy.

Parameters
[in]argsparameters for the validation strategy
Returns
a reference to the search class (used for method chaining)

◆ validation_strategy() [3/4]

template<template< class > class ES, Evaluator E>
basic_search< ES, E > & ultra::basic_search< ES, E >::validation_strategy ( const ultra::validation_strategy & vs)

Sets the active validation strategy.

Parameters
[in]vsa validation strategy
Returns
reference to the search class (used for method chaining)

◆ validation_strategy() [4/4]

template<template< class > class ES, Evaluator E>
template<template< class > class ES, Evaluator E>
basic_search< ES, E > & ultra::basic_search< ES, E >::validation_strategy ( const ultra::validation_strategy & vs)

Sets the active validation strategy.

Parameters
[in]vsa validation strategy
Returns
reference to the search class (used for method chaining)

The documentation for this class was generated from the following files: