Vita
constrained_evaluator.tcc
1/**
2 * \file
3 * \remark This file is part of VITA.
4 *
5 * \copyright Copyright (C) 2014-2019 EOS di Manlio Morini.
6 *
7 * \license
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
10 * You can obtain one at http://mozilla.org/MPL/2.0/
11 */
12
13#if !defined(VITA_CONSTRAINED_EVALUATOR_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(VITA_CONSTRAINED_EVALUATOR_TCC)
18#define VITA_CONSTRAINED_EVALUATOR_TCC
19
20///
21/// \param[in] e the base evaluator
22/// \param[in] p the penalty function (as described in "An Efficient Constraint
23/// Handling Method for Genetic Algorithms" - Kalyanmoy Deb)
24///
25template<class T, class E, class P>
26constrained_evaluator<T, E, P>::constrained_evaluator(E e, P p)
27 : eva_(std::move(e)), penalty_(std::move(p))
28{
29}
30
31///
32/// \param[in] prg the program (individual/team) whose fitness we want to know
33/// \return the fitness of `ind`
34///
35template<class T, class E, class P>
36fitness_t constrained_evaluator<T, E, P>::operator()(const T &prg)
37{
38 return combine(fitness_t{static_cast<fitness_t::value_type>(-penalty_(prg))},
39 eva_(prg));
40}
41
42///
43/// \param[in] prg a program (individual/team)
44/// \return the an approximation of the fitness of `prg`
45///
46template<class T, class E, class P>
47fitness_t constrained_evaluator<T, E, P>::fast(const T &prg)
48{
49 return combine(fitness_t{static_cast<fitness_t::value_type>(-penalty_(prg))},
50 eva_.fast(prg));
51}
52
53///
54/// \param[in] prg a program (individual/team)
55/// \return a pointer to the executable version of `prg`
56///
57template<class T, class E, class P>
58std::unique_ptr<basic_lambda_f> constrained_evaluator<T, E, P>::lambdify(
59 const T &prg) const
60{
61 return eva_.lambdify(prg);
62}
63
64#endif // include guard