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 |
|
---|
58 | void 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 |
|
---|
79 | const 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 |
|
---|
93 | const 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 |
|
---|
113 | const TrainingData::L2ErrorConfigurationIndexMap_t
|
---|
114 | TrainingData::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 |
|
---|
148 | const 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 |
|
---|
176 | const 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 |
|
---|
194 | std::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 | }
|
---|