Ultra 0.9.4
Loading...
Searching...
No Matches
evaluator_proxy.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_EVALUATOR_PROXY_H)
14#define ULTRA_EVALUATOR_PROXY_H
15
16#include "kernel/cache.h"
17#include "kernel/evaluator.h"
18
19#include "utility/log.h"
20
21namespace ultra
22{
29/// internal cache that stores fitness values keyed by individual signatures.
30/// This avoids repeated evaluation of semantically equivalent individuals and
31/// allows expensive evaluators to be reused efficiently.
32///
33/// The proxy preserves the semantics of the underlying evaluator while
34/// extending its capabilities with:
35/// - memoisation of fitness values;
36/// - optional fast (approximate) evaluation;
37/// - optional persistence of both evaluator state and cache contents.
38///
39/// The cache is logically mutable: cache updates do not alter the observable
40/// behaviour of the evaluator. For this reason, most member functions are
41/// `const`.
42///
43template<Evaluator E>
45{
46public:
48
49 // Serialization.
50 bool load(std::istream &);
51 bool save(std::ostream &) const;
52
53 bool load_cache(std::istream &) const;
54 bool save_cache(std::ostream &) const;
55
56 void clear() const noexcept;
57 void clear(const hash_t &) const;
58
59 [[nodiscard]] evaluator_fitness_t<E> operator()(
60 const evaluator_individual_t<E> &) const;
61
62 [[nodiscard]] evaluator_fitness_t<E> fast(
63 const evaluator_individual_t<E> &) const;
64
65 bool set_parallel_evaluation(unsigned, std::size_t = 0);
66
67 [[nodiscard]] const E &core() const noexcept;
68
69private:
70 E eva_; // wrapped evaluator
71
72 // Cache storing fitness values indexed by individual signatures.
73 mutable cache<evaluator_fitness_t<E>> cache_;
74};
75
76#include "kernel/evaluator_proxy.tcc"
77
78} // namespace ultra
79
80#endif // include guard
Definition cache.h:50
bool save_cache(std::ostream &) const
Definition evaluator_proxy.h:156
bool save(std::ostream &) const
Definition evaluator_proxy.h:172
bool load(std::istream &)
Definition evaluator_proxy.h:144
evaluator_proxy(E, bitwidth)
Definition evaluator_proxy.h:29
void clear() const noexcept
Definition evaluator_proxy.h:184
const E & core() const noexcept
Definition evaluator_proxy.h:229
bool set_parallel_evaluation(unsigned, std::size_t=0)
Definition evaluator_proxy.h:214
bool load_cache(std::istream &) const
Definition evaluator_proxy.h:126
evaluator_fitness_t< E > fast(const evaluator_individual_t< E > &) const
Definition evaluator_proxy.h:106
STL namespace.
unsigned bitwidth
Type alias for the number of bits.
Definition cache.h:30
Definition hash_t.h:34