source: src/FunctionApproximation/TrainingData.cpp@ c5e75f3

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open 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 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 ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since c5e75f3 was c7aac9, checked in by Frederik Heber <heber@…>, 8 years ago

FIX: TrainingData used charges instead of atomic numbers to recognize fragments.

  • this only works if we do not DoSampleValenceOnly. Otherwise charges are no longer directly associable with the atomic number of the nuclei.
  • Extractors::gatherAllSymmetricDistanceArguments now needs atomicnumbers instead of charges.
  • TESTFIX: needed to adapt ExtractorsUnitTest.
  • Property mode set to 100644
File size: 7.4 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/Summation/SetValues/Fragment.hpp"
53#include "FunctionApproximation/FunctionArgument.hpp"
54#include "FunctionApproximation/FunctionModel.hpp"
55#include "FunctionApproximation/Extractors.hpp"
56
57void TrainingData::operator()(const range_t &range) {
58 for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
59 const Fragment &fragment = iter->second.fragment;
60 FunctionModel::arguments_t all_args = Extractors::gatherAllSymmetricDistances(
61 fragment.getPositions(),
62 fragment.getAtomicNumbers(),
63 DistanceVector.size()
64 );
65 DistanceVector.push_back( all_args );
66 const double &energy = iter->second.energy;
67 EnergyVector.push_back( FunctionModel::results_t(1, energy) );
68 // filter distances out of list of all arguments
69 FunctionModel::list_of_arguments_t args = filter(all_args);
70 LOG(3, "DEBUG: Filtered arguments are " << args << ".");
71 ArgumentVector.push_back( args );
72 }
73}
74
75const double TrainingData::getL2Error(const FunctionModel &model) const
76{
77 double L2sum = 0.;
78
79 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
80 OutputVector_t::const_iterator outiter = EnergyVector.begin();
81 for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
82 const FunctionModel::results_t result = model((*initer));
83 const double temp = fabs((*outiter)[0] - result[0]);
84 L2sum += temp*temp;
85 }
86 return L2sum;
87}
88
89const double TrainingData::getLMaxError(const FunctionModel &model) const
90{
91 double Lmax = 0.;
92// size_t maxindex = -1;
93 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
94 OutputVector_t::const_iterator outiter = EnergyVector.begin();
95 for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
96 const FunctionModel::results_t result = model((*initer));
97 const double temp = fabs((*outiter)[0] - result[0]);
98 if (temp > Lmax) {
99 Lmax = temp;
100// maxindex = std::distance(
101// const_cast<const FunctionApproximation::inputs_t &>(ArgumentVector).begin(),
102// initer
103// );
104 }
105 }
106 return Lmax;
107}
108
109const TrainingData::L2ErrorConfigurationIndexMap_t
110TrainingData::getWorstFragmentMap(
111 const FunctionModel &model,
112 const range_t &range) const
113{
114 L2ErrorConfigurationIndexMap_t WorseFragmentMap;
115 // fragments make it into the container in reversed order, hence count from top down
116 size_t index= std::distance(range.first, range.second)-1;
117 InputVector_t::const_iterator distanceiter = DistanceVector.begin();
118 FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
119 OutputVector_t::const_iterator outiter = EnergyVector.begin();
120 for (; initer != ArgumentVector.end(); ++initer, ++outiter, ++distanceiter) {
121 // calculate value from potential
122 const FunctionModel::list_of_arguments_t &args = *initer;
123 const FunctionModel::results_t result = model(args);
124 const double energy = (*outiter)[0];
125
126 // insert difference into map
127 const double error = fabs(energy - result[0]);
128 WorseFragmentMap.insert( std::make_pair( error, index-- ) );
129
130 {
131 // give only the distances in the debugging text
132 std::stringstream streamargs;
133 BOOST_FOREACH (argument_t arg, *distanceiter) {
134 streamargs << " " << arg.distance;
135 }
136 LOG(2, "DEBUG: frag.#" << index+1 << "'s error is |" << energy << " - " << result[0]
137 << "| = " << error << " for args " << streamargs.str() << ".");
138 }
139 }
140
141 return WorseFragmentMap;
142}
143
144const TrainingData::DistanceEnergyTable_t TrainingData::getDistanceEnergyTable() const
145{
146 TrainingData::DistanceEnergyTable_t table;
147
148 /// extract distance member variable from argument_t and first value from results_t
149 OutputVector_t::const_iterator ergiter = EnergyVector.begin();
150 for (InputVector_t::const_iterator iter = DistanceVector.begin();
151 iter != DistanceVector.end(); ++iter, ++ergiter) {
152 ASSERT( ergiter != EnergyVector.end(),
153 "TrainingData::getDistanceEnergyTable() - less output than input values.");
154 std::vector< double > values(iter->size(), 0.);
155 // transform all distances
156 const FunctionModel::arguments_t &args = *iter;
157 std::transform(
158 args.begin(), args.end(),
159 values.begin(),
160 boost::bind(&argument_t::distance, _1));
161
162 // get first energy value
163 values.push_back((*ergiter)[0]);
164
165 // push as table row
166 table.push_back(values);
167 }
168
169 return table;
170}
171
172const FunctionModel::results_t TrainingData::getTrainingOutputAverage() const
173{
174 if (EnergyVector.size() != 0) {
175 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
176 FunctionModel::results_t result(*outiter);
177 for (++outiter; outiter != EnergyVector.end(); ++outiter)
178 for (size_t index = 0; index < (*outiter).size(); ++index)
179 result[index] += (*outiter)[index];
180 LOG(2, "DEBUG: Sum of EnergyVector is " << result << ".");
181 const double factor = 1./EnergyVector.size();
182 std::transform(result.begin(), result.end(), result.begin(),
183 boost::lambda::_1 * factor);
184 LOG(2, "DEBUG: Average EnergyVector is " << result << ".");
185 return result;
186 }
187 return FunctionModel::results_t();
188}
189
190std::ostream &operator<<(std::ostream &out, const TrainingData &data)
191{
192 const TrainingData::InputVector_t &DistanceVector = data.getAllArguments();
193 const TrainingData::OutputVector_t &EnergyVector = data.getTrainingOutputs();
194 out << "(" << DistanceVector.size()
195 << "," << EnergyVector.size() << ") data pairs: " << std::endl;
196 FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
197 FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
198 for (; initer != DistanceVector.end(); ++initer, ++outiter) {
199 for (size_t index = 0; index < (*initer).size(); ++index)
200 out << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
201 << ") " << (*initer)[index].distance;
202 out << " with energy ";
203 out << (*outiter);
204 out << std::endl;
205 }
206 return out;
207}
Note: See TracBrowser for help on using the repository browser.