| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the COPYING file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * FunctionApproximation.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: 02.10.2012 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "FunctionApproximation.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include <algorithm> | 
|---|
| 41 | #include <boost/bind.hpp> | 
|---|
| 42 | #include <boost/function.hpp> | 
|---|
| 43 | #include <iostream> | 
|---|
| 44 | #include <iterator> | 
|---|
| 45 | #include <numeric> | 
|---|
| 46 | #include <sstream> | 
|---|
| 47 |  | 
|---|
| 48 | #include <levmar.h> | 
|---|
| 49 |  | 
|---|
| 50 | #include "CodePatterns/Assert.hpp" | 
|---|
| 51 | #include "CodePatterns/Log.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | #include "FunctionApproximation/FunctionModel.hpp" | 
|---|
| 54 |  | 
|---|
| 55 | void FunctionApproximation::setTrainingData(const inputs_t &input, const outputs_t &output) | 
|---|
| 56 | { | 
|---|
| 57 | ASSERT( input.size() == output.size(), | 
|---|
| 58 | "FunctionApproximation::setTrainingData() - the number of input and output tuples differ: "+toString(input.size())+"!=" | 
|---|
| 59 | +toString(output.size())+"."); | 
|---|
| 60 | if (input.size() != 0) { | 
|---|
| 61 | ASSERT( input[0].size() == input_dimension, | 
|---|
| 62 | "FunctionApproximation::setTrainingData() - the dimension of the input tuples and input dimension differ: "+toString(input[0].size())+"!=" | 
|---|
| 63 | +toString(input_dimension)+"."); | 
|---|
| 64 | input_data = input; | 
|---|
| 65 | ASSERT( output[0].size() == output_dimension, | 
|---|
| 66 | "FunctionApproximation::setTrainingData() - the dimension of the output tuples and output dimension differ: "+toString(output[0].size())+"!=" | 
|---|
| 67 | +toString(output_dimension)+"."); | 
|---|
| 68 | output_data = output; | 
|---|
| 69 | } else { | 
|---|
| 70 | ELOG(2, "Given vectors of training data are empty, clearing internal vectors accordingly."); | 
|---|
| 71 | input_data.clear(); | 
|---|
| 72 | output_data.clear(); | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void FunctionApproximation::setModelFunction(FunctionModel &_model) | 
|---|
| 77 | { | 
|---|
| 78 | model= _model; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /* Meyer's (reformulated) problem, minimum at (2.48, 6.18, 3.45) */ | 
|---|
| 83 | void meyer(double *p, double *x, int m, int n, void *data) | 
|---|
| 84 | { | 
|---|
| 85 | register int i; | 
|---|
| 86 | double ui; | 
|---|
| 87 |  | 
|---|
| 88 | for(i=0; i<n; ++i){ | 
|---|
| 89 | ui=0.45+0.05*i; | 
|---|
| 90 | x[i]=p[0]*exp(10.0*p[1]/(ui+p[2]) - 13.0); | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void jacmeyer(double *p, double *jac, int m, int n, void *data) | 
|---|
| 95 | { | 
|---|
| 96 | register int i, j; | 
|---|
| 97 | double ui, tmp; | 
|---|
| 98 |  | 
|---|
| 99 | for(i=j=0; i<n; ++i){ | 
|---|
| 100 | ui=0.45+0.05*i; | 
|---|
| 101 | tmp=exp(10.0*p[1]/(ui+p[2]) - 13.0); | 
|---|
| 102 |  | 
|---|
| 103 | jac[j++]=tmp; | 
|---|
| 104 | jac[j++]=10.0*p[0]*tmp/(ui+p[2]); | 
|---|
| 105 | jac[j++]=-10.0*p[0]*p[1]*tmp/((ui+p[2])*(ui+p[2])); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /** Callback to circumvent boost::bind, boost::function and function pointer problem. | 
|---|
| 110 | * | 
|---|
| 111 | * See here (second answer!) to the nature of the problem: | 
|---|
| 112 | * http://stackoverflow.com/questions/282372/demote-boostfunction-to-a-plain-function-pointer | 
|---|
| 113 | * | 
|---|
| 114 | * We cannot use a boost::bind bounded boost::function as a function pointer. | 
|---|
| 115 | * boost::function::target() will just return NULL because the signature does not | 
|---|
| 116 | * match. We have to use a C-style callback function and our luck is that | 
|---|
| 117 | * the levmar signature provides for a void* additional data pointer which we | 
|---|
| 118 | * can cast back to our FunctionApproximation class, as we need access to the | 
|---|
| 119 | * data contained, e.g. the FunctionModel reference FunctionApproximation::model. | 
|---|
| 120 | * | 
|---|
| 121 | */ | 
|---|
| 122 | void FunctionApproximation::LevMarCallback(double *p, double *x, int m, int n, void *data) | 
|---|
| 123 | { | 
|---|
| 124 | FunctionApproximation *approximator = static_cast<FunctionApproximation *>(data); | 
|---|
| 125 | ASSERT( approximator != NULL, | 
|---|
| 126 | "LevMarCallback() - received data does not represent a FunctionApproximation object."); | 
|---|
| 127 | boost::function<void(double*,double*,int,int,void*)> function = | 
|---|
| 128 | boost::bind(&FunctionApproximation::evaluate, approximator, _1, _2, _3, _4, _5); | 
|---|
| 129 | function(p,x,m,n,data); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void FunctionApproximation::operator()() | 
|---|
| 133 | { | 
|---|
| 134 | // let levmar optimize | 
|---|
| 135 | register int i, j; | 
|---|
| 136 | int ret; | 
|---|
| 137 | double *p; | 
|---|
| 138 | double *x; // we set zero vector by giving NULL | 
|---|
| 139 | int m, n; | 
|---|
| 140 | double opts[LM_OPTS_SZ], info[LM_INFO_SZ]; | 
|---|
| 141 |  | 
|---|
| 142 | opts[0]=LM_INIT_MU; opts[1]=1E-15; opts[2]=1E-15; opts[3]=1E-20; | 
|---|
| 143 | opts[4]= LM_DIFF_DELTA; // relevant only if the Jacobian is approximated using finite differences; specifies forward differencing | 
|---|
| 144 | //opts[4]=-LM_DIFF_DELTA; // specifies central differencing to approximate Jacobian; more accurate but more expensive to compute! | 
|---|
| 145 |  | 
|---|
| 146 | m = model.getParameterDimension(); | 
|---|
| 147 | n = 1; | 
|---|
| 148 | const FunctionModel::parameters_t params = model.getParameters(); | 
|---|
| 149 | p = new double(m); | 
|---|
| 150 | x = new double(n); | 
|---|
| 151 | size_t index = 0; | 
|---|
| 152 | for(FunctionModel::parameters_t::const_iterator paramiter = params.begin(); | 
|---|
| 153 | paramiter != params.end(); | 
|---|
| 154 | ++paramiter, ++index) { | 
|---|
| 155 | p[index] = params[index]; | 
|---|
| 156 | } | 
|---|
| 157 | x[0] = 0.; | 
|---|
| 158 |  | 
|---|
| 159 | { | 
|---|
| 160 | double *work, *covar; | 
|---|
| 161 | work=(double *)malloc((LM_DIF_WORKSZ(m, n)+m*m)*sizeof(double)); | 
|---|
| 162 | if(!work){ | 
|---|
| 163 | ELOG(0, "FunctionApproximation::operator() - memory allocation request failed."); | 
|---|
| 164 | return; | 
|---|
| 165 | } | 
|---|
| 166 | covar=work+LM_DIF_WORKSZ(m, n); | 
|---|
| 167 |  | 
|---|
| 168 | // give this pointer as additional data to construct function pointer in | 
|---|
| 169 | // LevMarCallback and call | 
|---|
| 170 | ret=dlevmar_dif(&FunctionApproximation::LevMarCallback, p, x, m, n, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated | 
|---|
| 171 |  | 
|---|
| 172 | { | 
|---|
| 173 | std::stringstream covar_msg; | 
|---|
| 174 | covar_msg << "Covariance of the fit:\n"; | 
|---|
| 175 | for(i=0; i<m; ++i){ | 
|---|
| 176 | for(j=0; j<m; ++j) | 
|---|
| 177 | covar_msg << covar[i*m+j] << " "; | 
|---|
| 178 | covar_msg << std::endl; | 
|---|
| 179 | } | 
|---|
| 180 | covar_msg << std::endl; | 
|---|
| 181 | LOG(1, "INFO: " << covar_msg.str()); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | free(work); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | //  { | 
|---|
| 188 | //   double err[16]; | 
|---|
| 189 | //   dlevmar_chkjac(meyer, jacmeyer, p, m, n, NULL, err); | 
|---|
| 190 | //   for(i=0; i<n; ++i) printf("gradient %d, err %g\n", i, err[i]); | 
|---|
| 191 | //  } | 
|---|
| 192 |  | 
|---|
| 193 | { | 
|---|
| 194 | std::stringstream result_msg; | 
|---|
| 195 | result_msg << "Levenberg-Marquardt returned " << ret << " in " << info[5] << " iter, reason " << info[6] << "\nSolution: "; | 
|---|
| 196 | for(i=0; i<m; ++i) | 
|---|
| 197 | result_msg << p[i] << " "; | 
|---|
| 198 | result_msg << "\n\nMinimization info:\n"; | 
|---|
| 199 | std::vector<std::string> infonames(LM_INFO_SZ); | 
|---|
| 200 | infonames[0] = std::string("||e||_2 at initial p"); | 
|---|
| 201 | infonames[1] = std::string("||e||_2"); | 
|---|
| 202 | infonames[2] = std::string("||J^T e||_inf"); | 
|---|
| 203 | infonames[3] = std::string("||Dp||_2"); | 
|---|
| 204 | infonames[4] = std::string("mu/max[J^T J]_ii"); | 
|---|
| 205 | infonames[5] = std::string("# iterations"); | 
|---|
| 206 | infonames[6] = std::string("reason for termination"); | 
|---|
| 207 | infonames[7] = std::string(" # function evaluations"); | 
|---|
| 208 | infonames[8] = std::string(" # Jacobian evaluations"); | 
|---|
| 209 | infonames[9] = std::string(" # linear systems solved"); | 
|---|
| 210 | for(i=0; i<LM_INFO_SZ; ++i) | 
|---|
| 211 | result_msg << infonames[i] << ": " << info[i] << " "; | 
|---|
| 212 | result_msg << std::endl; | 
|---|
| 213 | LOG(1, "INFO: " << result_msg.str()); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | delete p; | 
|---|
| 217 | delete x; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | double SquaredDifference(const double res1, const double res2) | 
|---|
| 221 | { | 
|---|
| 222 | return (res1-res2)*(res1-res2); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | void FunctionApproximation::evaluate(double *p, double *x, int m, int n, void *data) | 
|---|
| 226 | { | 
|---|
| 227 | // first set parameters | 
|---|
| 228 | ASSERT( (size_t)m == model.getParameterDimension(), | 
|---|
| 229 | "FunctionApproximation::evaluate() - LevMar expects "+toString(m) | 
|---|
| 230 | +" parameters but the model function expects "+toString(model.getParameterDimension())+"."); | 
|---|
| 231 | ASSERT( n == 1, | 
|---|
| 232 | "FunctionApproximation::evaluate() - we return precisely a single value, LevMar expects something else."); | 
|---|
| 233 | FunctionModel::parameters_t params(m, 0.); | 
|---|
| 234 | std::copy(p, p+m, params.begin()); | 
|---|
| 235 | model.setParameters(params); | 
|---|
| 236 |  | 
|---|
| 237 | // then evaluate | 
|---|
| 238 | if (!input_data.empty()) { | 
|---|
| 239 | inputs_t::const_iterator initer = input_data.begin(); | 
|---|
| 240 | outputs_t::const_iterator outiter = output_data.begin(); | 
|---|
| 241 | size_t index = 0; | 
|---|
| 242 | for (; initer != input_data.end(); ++initer, ++outiter, ++index) { | 
|---|
| 243 | // result may be a vector, calculate L2 norm | 
|---|
| 244 | const FunctionModel::results_t functionvalue = | 
|---|
| 245 | model(*initer); | 
|---|
| 246 | std::vector<double> differences(functionvalue.size(), 0.); | 
|---|
| 247 | std::transform( | 
|---|
| 248 | functionvalue.begin(), functionvalue.end(), outiter->begin(), | 
|---|
| 249 | differences.begin(), | 
|---|
| 250 | &SquaredDifference); | 
|---|
| 251 | x[index] = std::accumulate(differences.begin(), differences.end(), 0.); | 
|---|
| 252 | } | 
|---|
| 253 | } else { | 
|---|
| 254 | for (register int i=0;i<n;++i) | 
|---|
| 255 | x[i] = 0.; | 
|---|
| 256 | } | 
|---|
| 257 | } | 
|---|