source: src/FunctionApproximation/TrainingData.cpp@ d94e7e

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 d94e7e was dd8094, checked in by Frederik Heber <heber@…>, 12 years ago

Added getTrainingOutputAverage() used to initialize all_energy_offset in LevMartester.

  • Property mode set to 100644
File size: 5.7 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 * TrainingData.cpp
26 *
27 * Created on: 15.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 "TrainingData.hpp"
39
40#include <algorithm>
41#include <boost/bind.hpp>
42#include <boost/lambda/lambda.hpp>
43#include <iostream>
44
45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Log.hpp"
47#include "CodePatterns/toString.hpp"
48
49#include "Fragmentation/SetValues/Fragment.hpp"
50#include "FunctionApproximation/FunctionModel.hpp"
51
52void TrainingData::operator()(const range_t &range) {
53 for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
54 // get distance out of Fragment
55 const Fragment &fragment = iter->second.first;
56 FunctionModel::arguments_t args = extractor(
57 fragment,
58 DistanceVector.size()
59 );
60 DistanceVector.push_back( args );
61 const double &energy = iter->second.second;
62 EnergyVector.push_back( FunctionModel::results_t(1, energy) );
63 }
64}
65
66const double TrainingData::getL2Error(const FunctionModel &model) const
67{
68 double L2sum = 0.;
69
70 FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
71 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
72 for (; initer != DistanceVector.end(); ++initer, ++outiter) {
73 const FunctionModel::results_t result = model((*initer));
74 const double temp = fabs((*outiter)[0] - result[0]);
75 L2sum += temp*temp;
76 }
77 return L2sum;
78}
79
80const double TrainingData::getLMaxError(const FunctionModel &model) const
81{
82 double Lmax = 0.;
83 size_t maxindex = -1;
84 FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
85 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
86 for (; initer != DistanceVector.end(); ++initer, ++outiter) {
87 const FunctionModel::results_t result = model((*initer));
88 const double temp = fabs((*outiter)[0] - result[0]);
89 if (temp > Lmax) {
90 Lmax = temp;
91 maxindex = std::distance(
92 const_cast<const FunctionApproximation::inputs_t &>(DistanceVector).begin(),
93 initer
94 );
95 }
96 }
97 return Lmax;
98}
99
100const TrainingData::DistanceEnergyTable_t TrainingData::getDistanceEnergyTable() const
101{
102 TrainingData::DistanceEnergyTable_t table;
103 const InputVector_t &DistanceVector = getTrainingInputs();
104 const OutputVector_t &EnergyVector = getTrainingOutputs();
105
106 /// extract distance member variable from argument_t and first value from results_t
107 OutputVector_t::const_iterator ergiter = EnergyVector.begin();
108 for (InputVector_t::const_iterator iter = DistanceVector.begin();
109 iter != DistanceVector.end(); ++iter, ++ergiter) {
110 ASSERT( ergiter != EnergyVector.end(),
111 "TrainingData::getDistanceEnergyTable() - less output than input values.");
112 std::vector< double > values(iter->size(), 0.);
113 // transform all distances
114 const FunctionModel::arguments_t &args = *iter;
115 std::transform(
116 args.begin(), args.end(),
117 values.begin(),
118 boost::bind(&argument_t::distance, _1));
119
120 // get first energy value
121 values.push_back((*ergiter)[0]);
122
123 // push as table row
124 table.push_back(values);
125 }
126
127 return table;
128}
129
130const FunctionModel::results_t TrainingData::getTrainingOutputAverage() const
131{
132 if (EnergyVector.size() != 0) {
133 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
134 FunctionModel::results_t result(*outiter);
135 for (++outiter; outiter != EnergyVector.end(); ++outiter)
136 for (size_t index = 0; index < (*outiter).size(); ++index)
137 result[index] += (*outiter)[index];
138 LOG(2, "DEBUG: Sum of EnergyVector is " << result << ".");
139 const double factor = 1./EnergyVector.size();
140 std::transform(result.begin(), result.end(), result.begin(),
141 boost::lambda::_1 * factor);
142 LOG(2, "DEBUG: Average EnergyVector is " << result << ".");
143 return result;
144 }
145 return FunctionModel::results_t();
146}
147
148std::ostream &operator<<(std::ostream &out, const TrainingData &data)
149{
150 const TrainingData::InputVector_t &DistanceVector = data.getTrainingInputs();
151 const TrainingData::OutputVector_t &EnergyVector = data.getTrainingOutputs();
152 out << "(" << DistanceVector.size()
153 << "," << EnergyVector.size() << ") data pairs: " << std::endl;
154 FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
155 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
156 for (; initer != DistanceVector.end(); ++initer, ++outiter) {
157 for (size_t index = 0; index < (*initer).size(); ++index)
158 out << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
159 << ") " << (*initer)[index].distance;
160 out << " with energy ";
161 out << (*outiter);
162 out << std::endl;
163 }
164 return out;
165}
Note: See TracBrowser for help on using the repository browser.