source: src/FunctionApproximation/TrainingData.cpp@ 440ac3

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 440ac3 was 228340, checked in by Frederik Heber <heber@…>, 8 years ago

Extractors::gatherAllSymmetricDistances..() additionaly gets edge set to set bonded flag.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
7 *
8 *
9 * This file is part of MoleCuilder.
10 *
11 * MoleCuilder is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * MoleCuilder is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/*
26 * TrainingData.cpp
27 *
28 * Created on: 15.10.2012
29 * Author: heber
30 */
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "CodePatterns/MemDebug.hpp"
38
39#include "TrainingData.hpp"
40
41#include <algorithm>
42#include <boost/bind.hpp>
43#include <boost/foreach.hpp>
44#include <boost/lambda/lambda.hpp>
45#include <iostream>
46#include <sstream>
47
48#include "CodePatterns/Assert.hpp"
49#include "CodePatterns/Log.hpp"
50#include "CodePatterns/toString.hpp"
51
52#include "Fragmentation/EdgesPerFragment.hpp"
53#include "Fragmentation/Summation/SetValues/Fragment.hpp"
54#include "FunctionApproximation/FunctionArgument.hpp"
55#include "FunctionApproximation/FunctionModel.hpp"
56#include "FunctionApproximation/Extractors.hpp"
57
58void TrainingData::operator()(const range_t &range) {
59 for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
60 const HomologyGraph &graph = iter->first;
61 const Fragment &fragment = iter->second.fragment;
62 const FragmentationEdges::edges_t &edges = iter->second.edges;
63 FunctionModel::arguments_t all_args = Extractors::gatherAllSymmetricDistances(
64 fragment.getPositions(),
65 fragment.getAtomicNumbers(),
66 edges,
67 DistanceVector.size()
68 );
69 DistanceVector.push_back( all_args );
70 const double &energy = iter->second.energy;
71 EnergyVector.push_back( FunctionModel::results_t(1, energy) );
72 // filter distances out of list of all arguments
73 FunctionModel::list_of_arguments_t args = filter(graph, all_args);
74 LOG(3, "DEBUG: Filtered arguments are " << args << ".");
75 ArgumentVector.push_back( args );
76 }
77}
78
79const double TrainingData::getL2Error(const FunctionModel &model) const
80{
81 double L2sum = 0.;
82
83 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
84 OutputVector_t::const_iterator outiter = EnergyVector.begin();
85 for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
86 const FunctionModel::results_t result = model((*initer));
87 const double temp = fabs((*outiter)[0] - result[0]);
88 L2sum += temp*temp;
89 }
90 return L2sum;
91}
92
93const double TrainingData::getLMaxError(const FunctionModel &model) const
94{
95 double Lmax = 0.;
96// size_t maxindex = -1;
97 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
98 OutputVector_t::const_iterator outiter = EnergyVector.begin();
99 for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
100 const FunctionModel::results_t result = model((*initer));
101 const double temp = fabs((*outiter)[0] - result[0]);
102 if (temp > Lmax) {
103 Lmax = temp;
104// maxindex = std::distance(
105// const_cast<const FunctionApproximation::inputs_t &>(ArgumentVector).begin(),
106// initer
107// );
108 }
109 }
110 return Lmax;
111}
112
113const TrainingData::L2ErrorConfigurationIndexMap_t
114TrainingData::getWorstFragmentMap(
115 const FunctionModel &model,
116 const range_t &range) const
117{
118 L2ErrorConfigurationIndexMap_t WorseFragmentMap;
119 // fragments make it into the container in reversed order, hence count from top down
120 size_t index= std::distance(range.first, range.second)-1;
121 InputVector_t::const_iterator distanceiter = DistanceVector.begin();
122 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
123 OutputVector_t::const_iterator outiter = EnergyVector.begin();
124 for (; initer != ArgumentVector.end(); ++initer, ++outiter, ++distanceiter) {
125 // calculate value from potential
126 const FunctionModel::list_of_arguments_t &args = *initer;
127 const FunctionModel::results_t result = model(args);
128 const double energy = (*outiter)[0];
129
130 // insert difference into map
131 const double error = fabs(energy - result[0]);
132 WorseFragmentMap.insert( std::make_pair( error, index-- ) );
133
134 {
135 // give only the distances in the debugging text
136 std::stringstream streamargs;
137 BOOST_FOREACH (argument_t arg, *distanceiter) {
138 streamargs << " " << arg.distance;
139 }
140 LOG(2, "DEBUG: frag.#" << index+1 << "'s error is |" << energy << " - " << result[0]
141 << "| = " << error << " for args " << streamargs.str() << ".");
142 }
143 }
144
145 return WorseFragmentMap;
146}
147
148const TrainingData::DistanceEnergyTable_t TrainingData::getDistanceEnergyTable() const
149{
150 TrainingData::DistanceEnergyTable_t table;
151
152 /// extract distance member variable from argument_t and first value from results_t
153 OutputVector_t::const_iterator ergiter = EnergyVector.begin();
154 for (InputVector_t::const_iterator iter = DistanceVector.begin();
155 iter != DistanceVector.end(); ++iter, ++ergiter) {
156 ASSERT( ergiter != EnergyVector.end(),
157 "TrainingData::getDistanceEnergyTable() - less output than input values.");
158 std::vector< double > values(iter->size(), 0.);
159 // transform all distances
160 const FunctionModel::arguments_t &args = *iter;
161 std::transform(
162 args.begin(), args.end(),
163 values.begin(),
164 boost::bind(&argument_t::distance, _1));
165
166 // get first energy value
167 values.push_back((*ergiter)[0]);
168
169 // push as table row
170 table.push_back(values);
171 }
172
173 return table;
174}
175
176const FunctionModel::results_t TrainingData::getTrainingOutputAverage() const
177{
178 if (EnergyVector.size() != 0) {
179 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
180 FunctionModel::results_t result(*outiter);
181 for (++outiter; outiter != EnergyVector.end(); ++outiter)
182 for (size_t index = 0; index < (*outiter).size(); ++index)
183 result[index] += (*outiter)[index];
184 LOG(2, "DEBUG: Sum of EnergyVector is " << result << ".");
185 const double factor = 1./EnergyVector.size();
186 std::transform(result.begin(), result.end(), result.begin(),
187 boost::lambda::_1 * factor);
188 LOG(2, "DEBUG: Average EnergyVector is " << result << ".");
189 return result;
190 }
191 return FunctionModel::results_t();
192}
193
194std::ostream &operator<<(std::ostream &out, const TrainingData &data)
195{
196 const TrainingData::InputVector_t &DistanceVector = data.getAllArguments();
197 const TrainingData::OutputVector_t &EnergyVector = data.getTrainingOutputs();
198 out << "(" << DistanceVector.size()
199 << "," << EnergyVector.size() << ") data pairs: " << std::endl;
200 FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
201 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
202 for (; initer != DistanceVector.end(); ++initer, ++outiter) {
203 for (size_t index = 0; index < (*initer).size(); ++index)
204 out << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
205 << ") " << (*initer)[index].distance;
206 out << " with energy ";
207 out << (*outiter);
208 out << std::endl;
209 }
210 return out;
211}
Note: See TracBrowser for help on using the repository browser.