[66cfc7] | 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 | /** Callback to circumvent boost::bind, boost::function and function pointer problem.
|
---|
| 82 | *
|
---|
| 83 | * See here (second answer!) to the nature of the problem:
|
---|
| 84 | * http://stackoverflow.com/questions/282372/demote-boostfunction-to-a-plain-function-pointer
|
---|
| 85 | *
|
---|
| 86 | * We cannot use a boost::bind bounded boost::function as a function pointer.
|
---|
| 87 | * boost::function::target() will just return NULL because the signature does not
|
---|
| 88 | * match. We have to use a C-style callback function and our luck is that
|
---|
| 89 | * the levmar signature provides for a void* additional data pointer which we
|
---|
| 90 | * can cast back to our FunctionApproximation class, as we need access to the
|
---|
| 91 | * data contained, e.g. the FunctionModel reference FunctionApproximation::model.
|
---|
| 92 | *
|
---|
| 93 | */
|
---|
| 94 | void FunctionApproximation::LevMarCallback(double *p, double *x, int m, int n, void *data)
|
---|
| 95 | {
|
---|
| 96 | FunctionApproximation *approximator = static_cast<FunctionApproximation *>(data);
|
---|
| 97 | ASSERT( approximator != NULL,
|
---|
| 98 | "LevMarCallback() - received data does not represent a FunctionApproximation object.");
|
---|
| 99 | boost::function<void(double*,double*,int,int,void*)> function =
|
---|
| 100 | boost::bind(&FunctionApproximation::evaluate, approximator, _1, _2, _3, _4, _5);
|
---|
| 101 | function(p,x,m,n,data);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[5b5724] | 104 | void FunctionApproximation::LevMarDerivativeCallback(double *p, double *x, int m, int n, void *data)
|
---|
| 105 | {
|
---|
| 106 | FunctionApproximation *approximator = static_cast<FunctionApproximation *>(data);
|
---|
| 107 | ASSERT( approximator != NULL,
|
---|
| 108 | "LevMarDerivativeCallback() - received data does not represent a FunctionApproximation object.");
|
---|
| 109 | boost::function<void(double*,double*,int,int,void*)> function =
|
---|
| 110 | boost::bind(&FunctionApproximation::evaluateDerivative, approximator, _1, _2, _3, _4, _5);
|
---|
| 111 | function(p,x,m,n,data);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[63b9f7] | 114 | void FunctionApproximation::prepareParameters(double *&p, int &m) const
|
---|
[66cfc7] | 115 | {
|
---|
| 116 | m = model.getParameterDimension();
|
---|
| 117 | const FunctionModel::parameters_t params = model.getParameters();
|
---|
[c62f96] | 118 | {
|
---|
| 119 | p = new double[m];
|
---|
| 120 | size_t index = 0;
|
---|
| 121 | for(FunctionModel::parameters_t::const_iterator paramiter = params.begin();
|
---|
| 122 | paramiter != params.end();
|
---|
| 123 | ++paramiter, ++index) {
|
---|
| 124 | p[index] = *paramiter;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[63b9f7] | 127 | }
|
---|
| 128 |
|
---|
| 129 | void FunctionApproximation::prepareOutput(double *&x, int &n) const
|
---|
| 130 | {
|
---|
| 131 | n = output_data.size();
|
---|
[c62f96] | 132 | {
|
---|
| 133 | x = new double[n];
|
---|
| 134 | size_t index = 0;
|
---|
| 135 | for(outputs_t::const_iterator outiter = output_data.begin();
|
---|
| 136 | outiter != output_data.end();
|
---|
| 137 | ++outiter, ++index) {
|
---|
| 138 | x[index] = (*outiter)[0];
|
---|
| 139 | }
|
---|
[66cfc7] | 140 | }
|
---|
[63b9f7] | 141 | }
|
---|
| 142 |
|
---|
| 143 | void FunctionApproximation::operator()(const enum JacobianMode mode)
|
---|
| 144 | {
|
---|
| 145 | // let levmar optimize
|
---|
| 146 | register int i, j;
|
---|
| 147 | int ret;
|
---|
| 148 | double *p;
|
---|
| 149 | double *x;
|
---|
| 150 | int m, n;
|
---|
| 151 | double opts[LM_OPTS_SZ], info[LM_INFO_SZ];
|
---|
| 152 |
|
---|
| 153 | opts[0]=LM_INIT_MU; opts[1]=1E-15; opts[2]=1E-15; opts[3]=1E-20;
|
---|
| 154 | opts[4]= LM_DIFF_DELTA; // relevant only if the Jacobian is approximated using finite differences; specifies forward differencing
|
---|
| 155 | //opts[4]=-LM_DIFF_DELTA; // specifies central differencing to approximate Jacobian; more accurate but more expensive to compute!
|
---|
| 156 |
|
---|
| 157 | prepareParameters(p,m);
|
---|
| 158 | prepareOutput(x,n);
|
---|
[66cfc7] | 159 |
|
---|
| 160 | {
|
---|
| 161 | double *work, *covar;
|
---|
| 162 | work=(double *)malloc((LM_DIF_WORKSZ(m, n)+m*m)*sizeof(double));
|
---|
| 163 | if(!work){
|
---|
| 164 | ELOG(0, "FunctionApproximation::operator() - memory allocation request failed.");
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
| 167 | covar=work+LM_DIF_WORKSZ(m, n);
|
---|
| 168 |
|
---|
| 169 | // give this pointer as additional data to construct function pointer in
|
---|
| 170 | // LevMarCallback and call
|
---|
[d03292] | 171 | if (model.isBoxConstraint()) {
|
---|
| 172 | FunctionModel::parameters_t lowerbound = model.getLowerBoxConstraints();
|
---|
| 173 | FunctionModel::parameters_t upperbound = model.getUpperBoxConstraints();
|
---|
| 174 | double *lb = new double[m];
|
---|
| 175 | double *ub = new double[m];
|
---|
| 176 | for (size_t i=0;i<m;++i) {
|
---|
| 177 | lb[i] = lowerbound[i];
|
---|
| 178 | ub[i] = upperbound[i];
|
---|
| 179 | }
|
---|
| 180 | if (mode == FiniteDifferences) {
|
---|
| 181 | ret=dlevmar_bc_dif(
|
---|
| 182 | &FunctionApproximation::LevMarCallback,
|
---|
| 183 | p, x, m, n, lb, ub, NULL, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
|
---|
| 184 | } else if (mode == ParameterDerivative) {
|
---|
| 185 | ret=dlevmar_bc_der(
|
---|
| 186 | &FunctionApproximation::LevMarCallback,
|
---|
| 187 | &FunctionApproximation::LevMarDerivativeCallback,
|
---|
| 188 | p, x, m, n, lb, ub, NULL, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
|
---|
| 189 | } else {
|
---|
| 190 | ASSERT(0, "FunctionApproximation::operator() - Unknown jacobian method chosen.");
|
---|
| 191 | }
|
---|
| 192 | delete[] lb;
|
---|
| 193 | delete[] ub;
|
---|
[76e63d] | 194 | } else {
|
---|
| 195 | ASSERT(0, "FunctionApproximation::operator() - Unknown jacobian method chosen.");
|
---|
[d03292] | 196 | if (mode == FiniteDifferences) {
|
---|
| 197 | ret=dlevmar_dif(
|
---|
| 198 | &FunctionApproximation::LevMarCallback,
|
---|
| 199 | p, x, m, n, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
|
---|
| 200 | } else if (mode == ParameterDerivative) {
|
---|
| 201 | ret=dlevmar_der(
|
---|
| 202 | &FunctionApproximation::LevMarCallback,
|
---|
| 203 | &FunctionApproximation::LevMarDerivativeCallback,
|
---|
| 204 | p, x, m, n, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
|
---|
| 205 | } else {
|
---|
| 206 | ASSERT(0, "FunctionApproximation::operator() - Unknown jacobian method chosen.");
|
---|
| 207 | }
|
---|
[76e63d] | 208 | }
|
---|
[66cfc7] | 209 |
|
---|
| 210 | {
|
---|
| 211 | std::stringstream covar_msg;
|
---|
| 212 | covar_msg << "Covariance of the fit:\n";
|
---|
| 213 | for(i=0; i<m; ++i){
|
---|
| 214 | for(j=0; j<m; ++j)
|
---|
| 215 | covar_msg << covar[i*m+j] << " ";
|
---|
| 216 | covar_msg << std::endl;
|
---|
| 217 | }
|
---|
| 218 | covar_msg << std::endl;
|
---|
| 219 | LOG(1, "INFO: " << covar_msg.str());
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | free(work);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | {
|
---|
| 226 | std::stringstream result_msg;
|
---|
| 227 | result_msg << "Levenberg-Marquardt returned " << ret << " in " << info[5] << " iter, reason " << info[6] << "\nSolution: ";
|
---|
| 228 | for(i=0; i<m; ++i)
|
---|
| 229 | result_msg << p[i] << " ";
|
---|
| 230 | result_msg << "\n\nMinimization info:\n";
|
---|
| 231 | std::vector<std::string> infonames(LM_INFO_SZ);
|
---|
| 232 | infonames[0] = std::string("||e||_2 at initial p");
|
---|
| 233 | infonames[1] = std::string("||e||_2");
|
---|
| 234 | infonames[2] = std::string("||J^T e||_inf");
|
---|
| 235 | infonames[3] = std::string("||Dp||_2");
|
---|
| 236 | infonames[4] = std::string("mu/max[J^T J]_ii");
|
---|
| 237 | infonames[5] = std::string("# iterations");
|
---|
| 238 | infonames[6] = std::string("reason for termination");
|
---|
| 239 | infonames[7] = std::string(" # function evaluations");
|
---|
| 240 | infonames[8] = std::string(" # Jacobian evaluations");
|
---|
| 241 | infonames[9] = std::string(" # linear systems solved");
|
---|
| 242 | for(i=0; i<LM_INFO_SZ; ++i)
|
---|
| 243 | result_msg << infonames[i] << ": " << info[i] << " ";
|
---|
| 244 | result_msg << std::endl;
|
---|
| 245 | LOG(1, "INFO: " << result_msg.str());
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[c62f96] | 248 | delete[] p;
|
---|
| 249 | delete[] x;
|
---|
[66cfc7] | 250 | }
|
---|
| 251 |
|
---|
[371c8b] | 252 | bool FunctionApproximation::checkParameterDerivatives()
|
---|
| 253 | {
|
---|
| 254 | double *p;
|
---|
| 255 | int m;
|
---|
| 256 | const FunctionModel::parameters_t backupparams = model.getParameters();
|
---|
| 257 | prepareParameters(p,m);
|
---|
| 258 | int n = output_data.size();
|
---|
| 259 | double *err = new double[n];
|
---|
| 260 | dlevmar_chkjac(
|
---|
| 261 | &FunctionApproximation::LevMarCallback,
|
---|
| 262 | &FunctionApproximation::LevMarDerivativeCallback,
|
---|
| 263 | p, m, n, this, err);
|
---|
| 264 | int i;
|
---|
| 265 | for(i=0; i<n; ++i)
|
---|
| 266 | LOG(1, "INFO: gradient " << i << ", err " << err[i] << ".");
|
---|
| 267 | bool status = true;
|
---|
| 268 | for(i=0; i<n; ++i)
|
---|
| 269 | status &= err[i] > 0.5;
|
---|
| 270 |
|
---|
| 271 | if (!status) {
|
---|
| 272 | int faulty;
|
---|
| 273 | ELOG(0, "At least one of the parameter derivatives are incorrect.");
|
---|
| 274 | for (faulty=1; faulty<=m; ++faulty) {
|
---|
| 275 | LOG(1, "INFO: Trying with only the first " << faulty << " parameters...");
|
---|
| 276 | model.setParameters(backupparams);
|
---|
| 277 | dlevmar_chkjac(
|
---|
| 278 | &FunctionApproximation::LevMarCallback,
|
---|
| 279 | &FunctionApproximation::LevMarDerivativeCallback,
|
---|
| 280 | p, faulty, n, this, err);
|
---|
| 281 | bool status = true;
|
---|
| 282 | for(i=0; i<n; ++i)
|
---|
| 283 | status &= err[i] > 0.5;
|
---|
| 284 | for(i=0; i<n; ++i)
|
---|
| 285 | LOG(1, "INFO: gradient(" << faulty << ") " << i << ", err " << err[i] << ".");
|
---|
| 286 | if (!status)
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 | ELOG(0, "The faulty parameter derivative is with respect to the " << faulty << " parameter.");
|
---|
| 290 | } else
|
---|
| 291 | LOG(1, "INFO: parameter derivatives are ok.");
|
---|
| 292 |
|
---|
| 293 | delete[] err;
|
---|
| 294 | delete[] p;
|
---|
| 295 | model.setParameters(backupparams);
|
---|
| 296 |
|
---|
| 297 | return status;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[66cfc7] | 300 | double SquaredDifference(const double res1, const double res2)
|
---|
| 301 | {
|
---|
| 302 | return (res1-res2)*(res1-res2);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[5b5724] | 305 | void FunctionApproximation::prepareModel(double *p, int m)
|
---|
[66cfc7] | 306 | {
|
---|
[371c8b] | 307 | // ASSERT( (size_t)m == model.getParameterDimension(),
|
---|
| 308 | // "FunctionApproximation::prepareModel() - LevMar expects "+toString(m)
|
---|
| 309 | // +" parameters but the model function expects "+toString(model.getParameterDimension())+".");
|
---|
[66cfc7] | 310 | FunctionModel::parameters_t params(m, 0.);
|
---|
| 311 | std::copy(p, p+m, params.begin());
|
---|
| 312 | model.setParameters(params);
|
---|
[5b5724] | 313 | }
|
---|
| 314 |
|
---|
| 315 | void FunctionApproximation::evaluate(double *p, double *x, int m, int n, void *data)
|
---|
| 316 | {
|
---|
| 317 | // first set parameters
|
---|
| 318 | prepareModel(p,m);
|
---|
[66cfc7] | 319 |
|
---|
| 320 | // then evaluate
|
---|
[5b5724] | 321 | ASSERT( (size_t)n == output_data.size(),
|
---|
| 322 | "FunctionApproximation::evaluate() - LevMar expects "+toString(n)
|
---|
| 323 | +" outputs but we provide "+toString(output_data.size())+".");
|
---|
[c62f96] | 324 | if (!output_data.empty()) {
|
---|
[66cfc7] | 325 | inputs_t::const_iterator initer = input_data.begin();
|
---|
| 326 | outputs_t::const_iterator outiter = output_data.begin();
|
---|
| 327 | size_t index = 0;
|
---|
[5b5724] | 328 | for (; initer != input_data.end(); ++initer, ++outiter) {
|
---|
[66cfc7] | 329 | // result may be a vector, calculate L2 norm
|
---|
| 330 | const FunctionModel::results_t functionvalue =
|
---|
| 331 | model(*initer);
|
---|
[5b5724] | 332 | x[index++] = functionvalue[0];
|
---|
| 333 | // std::vector<double> differences(functionvalue.size(), 0.);
|
---|
| 334 | // std::transform(
|
---|
| 335 | // functionvalue.begin(), functionvalue.end(), outiter->begin(),
|
---|
| 336 | // differences.begin(),
|
---|
| 337 | // &SquaredDifference);
|
---|
| 338 | // x[index] = std::accumulate(differences.begin(), differences.end(), 0.);
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | void FunctionApproximation::evaluateDerivative(double *p, double *jac, int m, int n, void *data)
|
---|
| 344 | {
|
---|
| 345 | // first set parameters
|
---|
| 346 | prepareModel(p,m);
|
---|
| 347 |
|
---|
| 348 | // then evaluate
|
---|
| 349 | ASSERT( (size_t)n == output_data.size(),
|
---|
| 350 | "FunctionApproximation::evaluateDerivative() - LevMar expects "+toString(n)
|
---|
| 351 | +" outputs but we provide "+toString(output_data.size())+".");
|
---|
| 352 | if (!output_data.empty()) {
|
---|
| 353 | inputs_t::const_iterator initer = input_data.begin();
|
---|
| 354 | outputs_t::const_iterator outiter = output_data.begin();
|
---|
| 355 | size_t index = 0;
|
---|
| 356 | for (; initer != input_data.end(); ++initer, ++outiter) {
|
---|
| 357 | // result may be a vector, calculate L2 norm
|
---|
| 358 | for (int paramindex = 0; paramindex < m; ++paramindex) {
|
---|
| 359 | const FunctionModel::results_t functionvalue =
|
---|
| 360 | model.parameter_derivative(*initer, paramindex);
|
---|
| 361 | jac[index++] = functionvalue[0];
|
---|
| 362 | }
|
---|
| 363 | // std::vector<double> differences(functionvalue.size(), 0.);
|
---|
| 364 | // std::transform(
|
---|
| 365 | // functionvalue.begin(), functionvalue.end(), outiter->begin(),
|
---|
| 366 | // differences.begin(),
|
---|
| 367 | // &SquaredDifference);
|
---|
| 368 | // x[index] = std::accumulate(differences.begin(), differences.end(), 0.);
|
---|
[66cfc7] | 369 | }
|
---|
| 370 | }
|
---|
| 371 | }
|
---|