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