| 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 |  | 
|---|
| 40 | #include <iostream> | 
|---|
| 41 |  | 
|---|
| 42 | #include "CodePatterns/toString.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "Fragmentation/SetValues/Fragment.hpp" | 
|---|
| 45 | #include "FunctionApproximation/FunctionModel.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | void TrainingData::operator()(const range_t &range) { | 
|---|
| 48 | for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) { | 
|---|
| 49 | // get distance out of Fragment | 
|---|
| 50 | const Fragment &fragment = iter->second.first; | 
|---|
| 51 | FunctionModel::arguments_t args = extractor( | 
|---|
| 52 | fragment, | 
|---|
| 53 | DistanceVector.size() | 
|---|
| 54 | ); | 
|---|
| 55 | DistanceVector.push_back( args ); | 
|---|
| 56 | const double &energy = iter->second.second; | 
|---|
| 57 | EnergyVector.push_back( FunctionModel::results_t(1, energy) ); | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | const double TrainingData::getL2Error(const FunctionModel &model) const | 
|---|
| 62 | { | 
|---|
| 63 | double L2sum = 0.; | 
|---|
| 64 |  | 
|---|
| 65 | FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin(); | 
|---|
| 66 | FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin(); | 
|---|
| 67 | for (; initer != DistanceVector.end(); ++initer, ++outiter) { | 
|---|
| 68 | const FunctionModel::results_t result = model((*initer)); | 
|---|
| 69 | const double temp = fabs((*outiter)[0] - result[0]); | 
|---|
| 70 | L2sum += temp*temp; | 
|---|
| 71 | } | 
|---|
| 72 | return L2sum; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | const double TrainingData::getLMaxError(const FunctionModel &model) const | 
|---|
| 76 | { | 
|---|
| 77 | double Lmax = 0.; | 
|---|
| 78 | size_t maxindex = -1; | 
|---|
| 79 | FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin(); | 
|---|
| 80 | FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin(); | 
|---|
| 81 | for (; initer != DistanceVector.end(); ++initer, ++outiter) { | 
|---|
| 82 | const FunctionModel::results_t result = model((*initer)); | 
|---|
| 83 | const double temp = fabs((*outiter)[0] - result[0]); | 
|---|
| 84 | if (temp > Lmax) { | 
|---|
| 85 | Lmax = temp; | 
|---|
| 86 | maxindex = std::distance( | 
|---|
| 87 | const_cast<const FunctionApproximation::inputs_t &>(DistanceVector).begin(), | 
|---|
| 88 | initer | 
|---|
| 89 | ); | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 | return Lmax; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | std::ostream &operator<<(std::ostream &out, const TrainingData &data) | 
|---|
| 96 | { | 
|---|
| 97 | const TrainingData::InputVector_t &DistanceVector = data.getTrainingInputs(); | 
|---|
| 98 | const TrainingData::OutputVector_t &EnergyVector = data.getTrainingOutputs(); | 
|---|
| 99 | out << "(" << DistanceVector.size() | 
|---|
| 100 | << "," << EnergyVector.size() << ") data pairs: " << std::endl; | 
|---|
| 101 | FunctionApproximation::inputs_t::const_iterator initer = DistanceVector.begin(); | 
|---|
| 102 | FunctionApproximation::outputs_t::const_iterator outiter = EnergyVector.begin(); | 
|---|
| 103 | for (; initer != DistanceVector.end(); ++initer, ++outiter) { | 
|---|
| 104 | for (size_t index = 0; index < (*initer).size(); ++index) | 
|---|
| 105 | out << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second | 
|---|
| 106 | << ") " << (*initer)[index].distance; | 
|---|
| 107 | out << " with energy "; | 
|---|
| 108 | out << (*outiter); | 
|---|
| 109 | out << std::endl; | 
|---|
| 110 | } | 
|---|
| 111 | return out; | 
|---|
| 112 | } | 
|---|