| [66cfc7] | 1 | /* | 
|---|
|  | 2 | * FunctionApproximation.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: 02.10.2012 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef FUNCTIONAPPROXIMATION_HPP_ | 
|---|
|  | 9 | #define FUNCTIONAPPROXIMATION_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include <vector> | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "FunctionApproximation/FunctionModel.hpp" | 
|---|
|  | 19 |  | 
|---|
|  | 20 | /** This class encapsulates the solution to approximating a high-dimensional | 
|---|
|  | 21 | * function represented by two vectors of tuples, being input variables and | 
|---|
|  | 22 | * output of the function via a model function, manipulated by a set of | 
|---|
|  | 23 | * parameters. | 
|---|
|  | 24 | * | 
|---|
|  | 25 | * \note For this reason the input and output dimension has to be given in | 
|---|
|  | 26 | * the constructor since these are fixed parameters to the problem as a | 
|---|
|  | 27 | * whole and usually: a different input dimension means we have a completely | 
|---|
|  | 28 | * different problem (and hence we may as well construct and new instance of | 
|---|
|  | 29 | * this class). | 
|---|
|  | 30 | * | 
|---|
|  | 31 | * The "training data", i.e. the two sets of input and output values, is | 
|---|
|  | 32 | * given extra. | 
|---|
|  | 33 | * | 
|---|
|  | 34 | * The problem is then that a given high-dimensional function is supplied, | 
|---|
|  | 35 | * the "model", and we have to fit this function via its set of variable | 
|---|
|  | 36 | * parameters. This fitting procedure is executed via a Levenberg-Marquardt | 
|---|
|  | 37 | * algorithm as implemented in the | 
|---|
|  | 38 | * <a href="http://www.ics.forth.gr/~lourakis/levmar/index.html">LevMar</a> | 
|---|
|  | 39 | * package. | 
|---|
|  | 40 | * | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | class FunctionApproximation | 
|---|
|  | 43 | { | 
|---|
|  | 44 | public: | 
|---|
|  | 45 | //!> typedef for a vector of input arguments | 
|---|
|  | 46 | typedef std::vector<FunctionModel::arguments_t> inputs_t; | 
|---|
|  | 47 | //!> typedef for a vector of output values | 
|---|
|  | 48 | typedef std::vector<FunctionModel::results_t> outputs_t; | 
|---|
|  | 49 | public: | 
|---|
|  | 50 | /** Constructor of the class FunctionApproximation. | 
|---|
|  | 51 | * | 
|---|
|  | 52 | * \param _input_dimension input dimension for this function approximation | 
|---|
|  | 53 | * \param _output_dimension output dimension for this function approximation | 
|---|
|  | 54 | */ | 
|---|
|  | 55 | FunctionApproximation( | 
|---|
|  | 56 | const size_t &_input_dimension, | 
|---|
|  | 57 | const size_t &_output_dimension, | 
|---|
|  | 58 | FunctionModel &_model) : | 
|---|
|  | 59 | input_dimension(_input_dimension), | 
|---|
|  | 60 | output_dimension(_output_dimension), | 
|---|
|  | 61 | model(_model) | 
|---|
|  | 62 | {} | 
|---|
|  | 63 | /** Destructor for class FunctionApproximation. | 
|---|
|  | 64 | * | 
|---|
|  | 65 | */ | 
|---|
|  | 66 | ~FunctionApproximation() | 
|---|
|  | 67 | {} | 
|---|
|  | 68 |  | 
|---|
|  | 69 | /** Setter for the training data to be used. | 
|---|
|  | 70 | * | 
|---|
|  | 71 | * \param input vector of input tuples, needs to be of | 
|---|
|  | 72 | *        FunctionApproximation::input_dimension size | 
|---|
|  | 73 | * \param output vector of output tuples, needs to be of | 
|---|
|  | 74 | *        FunctionApproximation::output_dimension size | 
|---|
|  | 75 | */ | 
|---|
|  | 76 | void setTrainingData(const inputs_t &input, const outputs_t &output); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | /** Setter for the model function to be used in the approximation. | 
|---|
|  | 79 | * | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | void setModelFunction(FunctionModel &_model); | 
|---|
|  | 82 |  | 
|---|
| [76e63d] | 83 | /** This enum steers whether we use finite differences or | 
|---|
|  | 84 | * FunctionModel::parameter_derivative to calculate the jacobian. | 
|---|
|  | 85 | * | 
|---|
|  | 86 | */ | 
|---|
|  | 87 | enum JacobianMode { | 
|---|
|  | 88 | FiniteDifferences, | 
|---|
|  | 89 | ParameterDerivative, | 
|---|
|  | 90 | MAXMODE | 
|---|
|  | 91 | }; | 
|---|
|  | 92 |  | 
|---|
| [66cfc7] | 93 | /** This starts the fitting process, resulting in the parameters to | 
|---|
|  | 94 | * the model function being optimized with respect to the given training | 
|---|
|  | 95 | * data. | 
|---|
| [76e63d] | 96 | * | 
|---|
|  | 97 | * \param mode whether to use finite differences or the parameter derivative | 
|---|
|  | 98 | *        in calculating the jacobian | 
|---|
| [66cfc7] | 99 | */ | 
|---|
| [76e63d] | 100 | void operator()(const enum JacobianMode mode = FiniteDifferences); | 
|---|
| [66cfc7] | 101 |  | 
|---|
|  | 102 | /** Evaluates the model function for each pair of training tuple and returns | 
|---|
| [5b5724] | 103 | * the output of the function as a vector. | 
|---|
| [66cfc7] | 104 | * | 
|---|
|  | 105 | * This function as a signature compatible to the one required by the | 
|---|
|  | 106 | * LevMar package (with double precision). | 
|---|
|  | 107 | * | 
|---|
|  | 108 | * \param *p array of parameters for the model function of dimension \a m | 
|---|
|  | 109 | * \param *x array of result values of dimension \a n | 
|---|
|  | 110 | * \param m parameter dimension | 
|---|
|  | 111 | * \param n output dimension | 
|---|
|  | 112 | * \param *data additional data, unused here | 
|---|
|  | 113 | */ | 
|---|
|  | 114 | void evaluate(double *p, double *x, int m, int n, void *data); | 
|---|
|  | 115 |  | 
|---|
| [5b5724] | 116 | /** Evaluates the parameter derivative of the model function for each pair of | 
|---|
|  | 117 | * training tuple and returns the output of the function as vector. | 
|---|
|  | 118 | * | 
|---|
|  | 119 | * This function as a signature compatible to the one required by the | 
|---|
|  | 120 | * LevMar package (with double precision). | 
|---|
|  | 121 | * | 
|---|
|  | 122 | * \param *p array of parameters for the model function of dimension \a m | 
|---|
|  | 123 | * \param *jac on output jacobian matrix of result values of dimension \a n times \a m | 
|---|
|  | 124 | * \param m parameter dimension | 
|---|
|  | 125 | * \param n output dimension times parameter dimension | 
|---|
|  | 126 | * \param *data additional data, unused here | 
|---|
|  | 127 | */ | 
|---|
|  | 128 | void evaluateDerivative(double *p, double *jac, int m, int n, void *data); | 
|---|
|  | 129 |  | 
|---|
| [371c8b] | 130 | /** This functions checks whether the parameter derivative of the FunctionModel | 
|---|
|  | 131 | * has been correctly implemented by validating against finite differences. | 
|---|
|  | 132 | * | 
|---|
|  | 133 | * We use LevMar's dlevmar_chkjac() function. | 
|---|
|  | 134 | * | 
|---|
|  | 135 | * \return true - gradients are ok (>0.5), false - else | 
|---|
|  | 136 | */ | 
|---|
|  | 137 | bool checkParameterDerivatives(); | 
|---|
|  | 138 |  | 
|---|
| [66cfc7] | 139 | private: | 
|---|
|  | 140 | static void LevMarCallback(double *p, double *x, int m, int n, void *data); | 
|---|
|  | 141 |  | 
|---|
| [5b5724] | 142 | static void LevMarDerivativeCallback(double *p, double *x, int m, int n, void *data); | 
|---|
|  | 143 |  | 
|---|
|  | 144 | void prepareModel(double *p, int m); | 
|---|
|  | 145 |  | 
|---|
| [63b9f7] | 146 | void prepareParameters(double *&p, int &m) const; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | void prepareOutput(double *&x, int &n) const; | 
|---|
|  | 149 |  | 
|---|
| [66cfc7] | 150 | private: | 
|---|
|  | 151 | //!> input dimension (is fixed from construction) | 
|---|
|  | 152 | const size_t input_dimension; | 
|---|
|  | 153 | //!> output dimension (is fixed from construction) | 
|---|
|  | 154 | const size_t output_dimension; | 
|---|
|  | 155 |  | 
|---|
|  | 156 | //!> current input set of training data | 
|---|
|  | 157 | inputs_t input_data; | 
|---|
|  | 158 | //!> current output set of training data | 
|---|
|  | 159 | outputs_t output_data; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | //!> the model function to be used in the high-dimensional approximation | 
|---|
|  | 162 | FunctionModel &model; | 
|---|
|  | 163 | }; | 
|---|
|  | 164 |  | 
|---|
|  | 165 | #endif /* FUNCTIONAPPROXIMATION_HPP_ */ | 
|---|