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