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