Vita
interpreter.tcc
1/**
2 * \file
3 * \remark This file is part of VITA.
4 *
5 * \copyright Copyright (C) 2013-2020 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_SRC_INTERPRETER_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(VITA_SRC_INTERPRETER_TCC)
18#define VITA_SRC_INTERPRETER_TCC
19
20///
21/// Calculates the output of a program (individual) given a specific input.
22///
23/// \param[in] ex a vector of values for the problem's variables
24/// \return the output value of the src_interpreter
25///
26template<class T>
27value_t src_interpreter<T>::run(const std::vector<value_t> &ex)
28{
29 example_ = &ex;
30 return this->run();
31}
32
33///
34/// Used by the vita::variable class to retrieve the value of a variable.
35///
36/// \param[in] i the index of a variable
37/// \return the value of the `i`-th variable
38///
39template<class T>
40value_t src_interpreter<T>::fetch_var(unsigned i)
41{
42 Expects(i < example_->size());
43 return (*example_)[i];
44}
45
46///
47/// A handy short-cut for one-time execution of an individual.
48///
49/// \param[in] ind individual/program to be run
50/// \param[in] ex input values for the current training case (used to valorize
51/// the variables of `ind`)
52/// \return possible output value of the individual
53///
54template<class T>
55value_t run(const T &ind, const std::vector<value_t> &ex)
56{
57 return src_interpreter<T>(&ind).run(ex);
58}
59
60#endif // include guard