source: src/FunctionApproximation/FunctionApproximation.cpp@ f48ad3

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since f48ad3 was 371c8b, checked in by Frederik Heber <heber@…>, 12 years ago

Added FunctionApproximation::checkParameterDerivatives().

  • FunctionApproximation::prepareModel() and ::setParameters() now allow less than required number of parameters.
  • Property mode set to 100644
File size: 12.0 KB
Line 
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
55void 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
76void 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 */
94void 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
104void 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
114void FunctionApproximation::prepareParameters(double *&p, int &m) const
115{
116 m = model.getParameterDimension();
117 const FunctionModel::parameters_t params = model.getParameters();
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 }
127}
128
129void FunctionApproximation::prepareOutput(double *&x, int &n) const
130{
131 n = output_data.size();
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 }
140 }
141}
142
143void 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);
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
171 if (mode == FiniteDifferences) {
172 ret=dlevmar_dif(
173 &FunctionApproximation::LevMarCallback,
174 p, x, m, n, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
175 } else if (mode == ParameterDerivative) {
176 ret=dlevmar_der(
177 &FunctionApproximation::LevMarCallback,
178 &FunctionApproximation::LevMarDerivativeCallback,
179 p, x, m, n, 1000, opts, info, work, covar, this); // no Jacobian, caller allocates work memory, covariance estimated
180 } else {
181 ASSERT(0, "FunctionApproximation::operator() - Unknown jacobian method chosen.");
182 }
183
184 {
185 std::stringstream covar_msg;
186 covar_msg << "Covariance of the fit:\n";
187 for(i=0; i<m; ++i){
188 for(j=0; j<m; ++j)
189 covar_msg << covar[i*m+j] << " ";
190 covar_msg << std::endl;
191 }
192 covar_msg << std::endl;
193 LOG(1, "INFO: " << covar_msg.str());
194 }
195
196 free(work);
197 }
198
199 {
200 std::stringstream result_msg;
201 result_msg << "Levenberg-Marquardt returned " << ret << " in " << info[5] << " iter, reason " << info[6] << "\nSolution: ";
202 for(i=0; i<m; ++i)
203 result_msg << p[i] << " ";
204 result_msg << "\n\nMinimization info:\n";
205 std::vector<std::string> infonames(LM_INFO_SZ);
206 infonames[0] = std::string("||e||_2 at initial p");
207 infonames[1] = std::string("||e||_2");
208 infonames[2] = std::string("||J^T e||_inf");
209 infonames[3] = std::string("||Dp||_2");
210 infonames[4] = std::string("mu/max[J^T J]_ii");
211 infonames[5] = std::string("# iterations");
212 infonames[6] = std::string("reason for termination");
213 infonames[7] = std::string(" # function evaluations");
214 infonames[8] = std::string(" # Jacobian evaluations");
215 infonames[9] = std::string(" # linear systems solved");
216 for(i=0; i<LM_INFO_SZ; ++i)
217 result_msg << infonames[i] << ": " << info[i] << " ";
218 result_msg << std::endl;
219 LOG(1, "INFO: " << result_msg.str());
220 }
221
222 delete[] p;
223 delete[] x;
224}
225
226bool FunctionApproximation::checkParameterDerivatives()
227{
228 double *p;
229 int m;
230 const FunctionModel::parameters_t backupparams = model.getParameters();
231 prepareParameters(p,m);
232 int n = output_data.size();
233 double *err = new double[n];
234 dlevmar_chkjac(
235 &FunctionApproximation::LevMarCallback,
236 &FunctionApproximation::LevMarDerivativeCallback,
237 p, m, n, this, err);
238 int i;
239 for(i=0; i<n; ++i)
240 LOG(1, "INFO: gradient " << i << ", err " << err[i] << ".");
241 bool status = true;
242 for(i=0; i<n; ++i)
243 status &= err[i] > 0.5;
244
245 if (!status) {
246 int faulty;
247 ELOG(0, "At least one of the parameter derivatives are incorrect.");
248 for (faulty=1; faulty<=m; ++faulty) {
249 LOG(1, "INFO: Trying with only the first " << faulty << " parameters...");
250 model.setParameters(backupparams);
251 dlevmar_chkjac(
252 &FunctionApproximation::LevMarCallback,
253 &FunctionApproximation::LevMarDerivativeCallback,
254 p, faulty, n, this, err);
255 bool status = true;
256 for(i=0; i<n; ++i)
257 status &= err[i] > 0.5;
258 for(i=0; i<n; ++i)
259 LOG(1, "INFO: gradient(" << faulty << ") " << i << ", err " << err[i] << ".");
260 if (!status)
261 break;
262 }
263 ELOG(0, "The faulty parameter derivative is with respect to the " << faulty << " parameter.");
264 } else
265 LOG(1, "INFO: parameter derivatives are ok.");
266
267 delete[] err;
268 delete[] p;
269 model.setParameters(backupparams);
270
271 return status;
272}
273
274double SquaredDifference(const double res1, const double res2)
275{
276 return (res1-res2)*(res1-res2);
277}
278
279void FunctionApproximation::prepareModel(double *p, int m)
280{
281// ASSERT( (size_t)m == model.getParameterDimension(),
282// "FunctionApproximation::prepareModel() - LevMar expects "+toString(m)
283// +" parameters but the model function expects "+toString(model.getParameterDimension())+".");
284 FunctionModel::parameters_t params(m, 0.);
285 std::copy(p, p+m, params.begin());
286 model.setParameters(params);
287}
288
289void FunctionApproximation::evaluate(double *p, double *x, int m, int n, void *data)
290{
291 // first set parameters
292 prepareModel(p,m);
293
294 // then evaluate
295 ASSERT( (size_t)n == output_data.size(),
296 "FunctionApproximation::evaluate() - LevMar expects "+toString(n)
297 +" outputs but we provide "+toString(output_data.size())+".");
298 if (!output_data.empty()) {
299 inputs_t::const_iterator initer = input_data.begin();
300 outputs_t::const_iterator outiter = output_data.begin();
301 size_t index = 0;
302 for (; initer != input_data.end(); ++initer, ++outiter) {
303 // result may be a vector, calculate L2 norm
304 const FunctionModel::results_t functionvalue =
305 model(*initer);
306 x[index++] = functionvalue[0];
307// std::vector<double> differences(functionvalue.size(), 0.);
308// std::transform(
309// functionvalue.begin(), functionvalue.end(), outiter->begin(),
310// differences.begin(),
311// &SquaredDifference);
312// x[index] = std::accumulate(differences.begin(), differences.end(), 0.);
313 }
314 }
315}
316
317void FunctionApproximation::evaluateDerivative(double *p, double *jac, int m, int n, void *data)
318{
319 // first set parameters
320 prepareModel(p,m);
321
322 // then evaluate
323 ASSERT( (size_t)n == output_data.size(),
324 "FunctionApproximation::evaluateDerivative() - LevMar expects "+toString(n)
325 +" outputs but we provide "+toString(output_data.size())+".");
326 if (!output_data.empty()) {
327 inputs_t::const_iterator initer = input_data.begin();
328 outputs_t::const_iterator outiter = output_data.begin();
329 size_t index = 0;
330 for (; initer != input_data.end(); ++initer, ++outiter) {
331 // result may be a vector, calculate L2 norm
332 for (int paramindex = 0; paramindex < m; ++paramindex) {
333 const FunctionModel::results_t functionvalue =
334 model.parameter_derivative(*initer, paramindex);
335 jac[index++] = functionvalue[0];
336 }
337// std::vector<double> differences(functionvalue.size(), 0.);
338// std::transform(
339// functionvalue.begin(), functionvalue.end(), outiter->begin(),
340// differences.begin(),
341// &SquaredDifference);
342// x[index] = std::accumulate(differences.begin(), differences.end(), 0.);
343 }
344 }
345}
Note: See TracBrowser for help on using the repository browser.