| [68172a] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
 | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | /*
 | 
|---|
 | 25 |  * TrainingData.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: 15.10.2012
 | 
|---|
 | 28 |  *      Author: heber
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "TrainingData.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [dd8094] | 40 | #include <algorithm>
 | 
|---|
| [04cc7e] | 41 | #include <boost/bind.hpp>
 | 
|---|
| [dd8094] | 42 | #include <boost/lambda/lambda.hpp>
 | 
|---|
| [68172a] | 43 | #include <iostream>
 | 
|---|
 | 44 | 
 | 
|---|
| [04cc7e] | 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [dd8094] | 46 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [68172a] | 47 | #include "CodePatterns/toString.hpp"
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | #include "Fragmentation/SetValues/Fragment.hpp"
 | 
|---|
 | 50 | #include "FunctionApproximation/FunctionModel.hpp"
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | void TrainingData::operator()(const range_t &range) {
 | 
|---|
 | 53 |   for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
 | 
|---|
 | 54 |     // get distance out of Fragment
 | 
|---|
 | 55 |     const Fragment &fragment = iter->second.first;
 | 
|---|
 | 56 |     FunctionModel::arguments_t args = extractor(
 | 
|---|
 | 57 |           fragment,
 | 
|---|
 | 58 |           DistanceVector.size()
 | 
|---|
 | 59 |         );
 | 
|---|
 | 60 |     DistanceVector.push_back( args );
 | 
|---|
 | 61 |     const double &energy = iter->second.second;
 | 
|---|
 | 62 |     EnergyVector.push_back( FunctionModel::results_t(1, energy) );
 | 
|---|
 | 63 |   }
 | 
|---|
 | 64 | }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | const double TrainingData::getL2Error(const FunctionModel &model) const
 | 
|---|
 | 67 | {
 | 
|---|
 | 68 |   double L2sum = 0.;
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |   FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
 | 
|---|
 | 71 |   FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
 | 
|---|
 | 72 |   for (; initer != DistanceVector.end(); ++initer, ++outiter) {
 | 
|---|
 | 73 |     const FunctionModel::results_t result = model((*initer));
 | 
|---|
 | 74 |     const double temp = fabs((*outiter)[0] - result[0]);
 | 
|---|
 | 75 |     L2sum += temp*temp;
 | 
|---|
 | 76 |   }
 | 
|---|
 | 77 |   return L2sum;
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | const double TrainingData::getLMaxError(const FunctionModel &model) const
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   double Lmax = 0.;
 | 
|---|
 | 83 |   size_t maxindex = -1;
 | 
|---|
 | 84 |   FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
 | 
|---|
 | 85 |   FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
 | 
|---|
 | 86 |   for (; initer != DistanceVector.end(); ++initer, ++outiter) {
 | 
|---|
 | 87 |     const FunctionModel::results_t result = model((*initer));
 | 
|---|
 | 88 |     const double temp = fabs((*outiter)[0] - result[0]);
 | 
|---|
 | 89 |     if (temp > Lmax) {
 | 
|---|
 | 90 |       Lmax = temp;
 | 
|---|
 | 91 |       maxindex = std::distance(
 | 
|---|
 | 92 |           const_cast<const FunctionApproximation::inputs_t &>(DistanceVector).begin(),
 | 
|---|
 | 93 |           initer
 | 
|---|
 | 94 |           );
 | 
|---|
 | 95 |     }
 | 
|---|
 | 96 |   }
 | 
|---|
 | 97 |   return Lmax;
 | 
|---|
 | 98 | }
 | 
|---|
 | 99 | 
 | 
|---|
| [04cc7e] | 100 | const TrainingData::DistanceEnergyTable_t TrainingData::getDistanceEnergyTable() const
 | 
|---|
 | 101 | {
 | 
|---|
 | 102 |   TrainingData::DistanceEnergyTable_t table;
 | 
|---|
 | 103 |   const InputVector_t &DistanceVector = getTrainingInputs();
 | 
|---|
 | 104 |   const OutputVector_t &EnergyVector = getTrainingOutputs();
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |   /// extract distance member variable from argument_t and first value from results_t
 | 
|---|
 | 107 |   OutputVector_t::const_iterator ergiter = EnergyVector.begin();
 | 
|---|
 | 108 |   for (InputVector_t::const_iterator iter = DistanceVector.begin();
 | 
|---|
 | 109 |       iter != DistanceVector.end(); ++iter, ++ergiter) {
 | 
|---|
 | 110 |     ASSERT( ergiter != EnergyVector.end(),
 | 
|---|
 | 111 |         "TrainingData::getDistanceEnergyTable() - less output than input values.");
 | 
|---|
 | 112 |     std::vector< double > values(iter->size(), 0.);
 | 
|---|
 | 113 |     // transform all distances
 | 
|---|
 | 114 |     const FunctionModel::arguments_t &args = *iter;
 | 
|---|
 | 115 |     std::transform(
 | 
|---|
 | 116 |         args.begin(), args.end(),
 | 
|---|
 | 117 |         values.begin(),
 | 
|---|
 | 118 |         boost::bind(&argument_t::distance, _1));
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |     // get first energy value
 | 
|---|
 | 121 |     values.push_back((*ergiter)[0]);
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 |     // push as table row
 | 
|---|
 | 124 |     table.push_back(values);
 | 
|---|
 | 125 |   }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |   return table;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
| [dd8094] | 130 | const FunctionModel::results_t TrainingData::getTrainingOutputAverage() const
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |   if (EnergyVector.size() != 0) {
 | 
|---|
 | 133 |     FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
 | 
|---|
 | 134 |     FunctionModel::results_t result(*outiter);
 | 
|---|
 | 135 |     for (++outiter; outiter != EnergyVector.end(); ++outiter)
 | 
|---|
 | 136 |       for (size_t index = 0; index < (*outiter).size(); ++index)
 | 
|---|
 | 137 |         result[index] += (*outiter)[index];
 | 
|---|
 | 138 |     LOG(2, "DEBUG: Sum of EnergyVector is " << result << ".");
 | 
|---|
 | 139 |     const double factor = 1./EnergyVector.size();
 | 
|---|
 | 140 |     std::transform(result.begin(), result.end(), result.begin(),
 | 
|---|
 | 141 |         boost::lambda::_1 * factor);
 | 
|---|
 | 142 |     LOG(2, "DEBUG: Average EnergyVector is " << result << ".");
 | 
|---|
 | 143 |     return result;
 | 
|---|
 | 144 |   }
 | 
|---|
 | 145 |   return FunctionModel::results_t();
 | 
|---|
 | 146 | }
 | 
|---|
 | 147 | 
 | 
|---|
| [68172a] | 148 | std::ostream &operator<<(std::ostream &out, const TrainingData &data)
 | 
|---|
 | 149 | {
 | 
|---|
 | 150 |   const TrainingData::InputVector_t &DistanceVector = data.getTrainingInputs();
 | 
|---|
 | 151 |   const TrainingData::OutputVector_t &EnergyVector = data.getTrainingOutputs();
 | 
|---|
 | 152 |   out << "(" << DistanceVector.size()
 | 
|---|
 | 153 |       << "," << EnergyVector.size() << ") data pairs: " << std::endl;
 | 
|---|
 | 154 |   FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin();
 | 
|---|
 | 155 |   FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin();
 | 
|---|
 | 156 |   for (; initer != DistanceVector.end(); ++initer, ++outiter) {
 | 
|---|
 | 157 |     for (size_t index = 0; index < (*initer).size(); ++index)
 | 
|---|
 | 158 |        out << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
 | 
|---|
 | 159 |           << ") " << (*initer)[index].distance;
 | 
|---|
 | 160 |     out << " with energy ";
 | 
|---|
 | 161 |     out << (*outiter);
 | 
|---|
 | 162 |     out << std::endl;
 | 
|---|
 | 163 |   }
 | 
|---|
 | 164 |   return out;
 | 
|---|
 | 165 | }
 | 
|---|