source: src/FunctionApproximation/FunctionApproximation.cpp@ b8f2ea

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 Candidate_v1.7.0 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 b8f2ea was b8f2ea, checked in by Frederik Heber <heber@…>, 10 years ago

FIX: PotentialTrainer did not use user-specified threshold so far.

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