3 * \remark This file is part of VITA.
5 * \copyright Copyright (C) 2014-2017 EOS di Manlio Morini.
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/
13#if !defined(VITA_GA_EVALUATOR_H)
14# error "Don't include this file directly, include the specific .h instead"
17#if !defined(VITA_GA_EVALUATOR_TCC)
18#define VITA_GA_EVALUATOR_TCC
21/// GP-evaluators use datasets, GA-evaluators need functions to be maximized.
23/// \param[in] f an objective function
25template<class T, class F>
26ga_evaluator<T, F>::ga_evaluator(F f) : f_(f)
28 // The assertion `assert(f)` works with function pointers and non capturing
29 // lambdas (which are implicitly convertible to function pointers) but
30 // fails with capturing lambdas
31 // (e.g. <http://stackoverflow.com/q/7852101/3235496>).
35/// \param[in] f objective function
36/// \return an evaluator based on `f`
38/// This is the so called object generator idiom. Here it's used because C++11
39/// standard doesn't allow for template argument deduction from the parameters
40/// passed to the constructor.
42/// Any reasonable compiler will optimize away the temporary object and this
43/// is usually faster than a solution based on `std::function`.
46/// * <http://stackoverflow.com/q/984394/3235496>
47/// * <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3602.html>
49template<class T, class F>
50ga_evaluator<T, F> make_ga_evaluator(F f)
52 return ga_evaluator<T, F>(f);
56/// \return the fitness of the individual
58template<class T, class F>
59fitness_t ga_evaluator<T, F>::operator()(const T &ind)
61 const auto f_v(f_(ind));
69#endif // include guard