[68172a] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[68172a] | 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 |
|
---|
[dd8094] | 41 | #include <algorithm>
|
---|
[04cc7e] | 42 | #include <boost/bind.hpp>
|
---|
[f4496d] | 43 | #include <boost/foreach.hpp>
|
---|
[dd8094] | 44 | #include <boost/lambda/lambda.hpp>
|
---|
[68172a] | 45 | #include <iostream>
|
---|
[f4496d] | 46 | #include <sstream>
|
---|
[68172a] | 47 |
|
---|
[04cc7e] | 48 | #include "CodePatterns/Assert.hpp"
|
---|
[dd8094] | 49 | #include "CodePatterns/Log.hpp"
|
---|
[68172a] | 50 | #include "CodePatterns/toString.hpp"
|
---|
| 51 |
|
---|
[228340] | 52 | #include "Fragmentation/EdgesPerFragment.hpp"
|
---|
[fbf143] | 53 | #include "Fragmentation/Summation/SetValues/Fragment.hpp"
|
---|
[f4496d] | 54 | #include "FunctionApproximation/FunctionArgument.hpp"
|
---|
[68172a] | 55 | #include "FunctionApproximation/FunctionModel.hpp"
|
---|
[af2c7ec] | 56 | #include "FunctionApproximation/Extractors.hpp"
|
---|
[68172a] | 57 |
|
---|
| 58 | void TrainingData::operator()(const range_t &range) {
|
---|
| 59 | for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
|
---|
[e60558] | 60 | const HomologyGraph &graph = iter->first;
|
---|
[bf1d1b] | 61 | const Fragment &fragment = iter->second.fragment;
|
---|
[228340] | 62 | const FragmentationEdges::edges_t &edges = iter->second.edges;
|
---|
[af2c7ec] | 63 | FunctionModel::arguments_t all_args = Extractors::gatherAllSymmetricDistances(
|
---|
| 64 | fragment.getPositions(),
|
---|
[c7aac9] | 65 | fragment.getAtomicNumbers(),
|
---|
[228340] | 66 | edges,
|
---|
[af2c7ec] | 67 | DistanceVector.size()
|
---|
[68172a] | 68 | );
|
---|
[af2c7ec] | 69 | DistanceVector.push_back( all_args );
|
---|
[bf1d1b] | 70 | const double &energy = iter->second.energy;
|
---|
[68172a] | 71 | EnergyVector.push_back( FunctionModel::results_t(1, energy) );
|
---|
[af2c7ec] | 72 | // filter distances out of list of all arguments
|
---|
[e60558] | 73 | FunctionModel::list_of_arguments_t args = filter(graph, all_args);
|
---|
[af2c7ec] | 74 | LOG(3, "DEBUG: Filtered arguments are " << args << ".");
|
---|
| 75 | ArgumentVector.push_back( args );
|
---|
[68172a] | 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | const double TrainingData::getL2Error(const FunctionModel &model) const
|
---|
| 80 | {
|
---|
| 81 | double L2sum = 0.;
|
---|
| 82 |
|
---|
[e1fe7e] | 83 | FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
|
---|
| 84 | OutputVector_t::const_iterator outiter = EnergyVector.begin();
|
---|
[af2c7ec] | 85 | for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
|
---|
[68172a] | 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.;
|
---|
[af2c7ec] | 96 | // size_t maxindex = -1;
|
---|
[e1fe7e] | 97 | FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
|
---|
| 98 | OutputVector_t::const_iterator outiter = EnergyVector.begin();
|
---|
[af2c7ec] | 99 | for (; initer != ArgumentVector.end(); ++initer, ++outiter) {
|
---|
[68172a] | 100 | const FunctionModel::results_t result = model((*initer));
|
---|
| 101 | const double temp = fabs((*outiter)[0] - result[0]);
|
---|
| 102 | if (temp > Lmax) {
|
---|
| 103 | Lmax = temp;
|
---|
[af2c7ec] | 104 | // maxindex = std::distance(
|
---|
| 105 | // const_cast<const FunctionApproximation::inputs_t &>(ArgumentVector).begin(),
|
---|
| 106 | // initer
|
---|
| 107 | // );
|
---|
[68172a] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | return Lmax;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[f4496d] | 113 | const TrainingData::L2ErrorConfigurationIndexMap_t
|
---|
| 114 | TrainingData::getWorstFragmentMap(
|
---|
| 115 | const FunctionModel &model,
|
---|
| 116 | const range_t &range) const
|
---|
| 117 | {
|
---|
[e1fe7e] | 118 | L2ErrorConfigurationIndexMap_t WorseFragmentMap;
|
---|
[f4496d] | 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;
|
---|
[e1fe7e] | 121 | InputVector_t::const_iterator distanceiter = DistanceVector.begin();
|
---|
| 122 | FilteredInputVector_t::const_iterator initer = ArgumentVector.begin();
|
---|
[f4496d] | 123 | OutputVector_t::const_iterator outiter = EnergyVector.begin();
|
---|
[e1fe7e] | 124 | for (; initer != ArgumentVector.end(); ++initer, ++outiter, ++distanceiter) {
|
---|
[f4496d] | 125 | // calculate value from potential
|
---|
[e1fe7e] | 126 | const FunctionModel::list_of_arguments_t &args = *initer;
|
---|
[f4496d] | 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;
|
---|
[e1fe7e] | 137 | BOOST_FOREACH (argument_t arg, *distanceiter) {
|
---|
[f4496d] | 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 |
|
---|
[04cc7e] | 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();
|
---|
[e1fe7e] | 154 | for (InputVector_t::const_iterator iter = DistanceVector.begin();
|
---|
| 155 | iter != DistanceVector.end(); ++iter, ++ergiter) {
|
---|
[04cc7e] | 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 |
|
---|
[dd8094] | 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 |
|
---|
[68172a] | 194 | std::ostream &operator<<(std::ostream &out, const TrainingData &data)
|
---|
| 195 | {
|
---|
[af2c7ec] | 196 | const TrainingData::InputVector_t &DistanceVector = data.getAllArguments();
|
---|
[68172a] | 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 | }
|
---|