| [8aa597] | 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.
 | 
|---|
| [8aa597] | 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 |  * Extractors.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 | 
 | 
|---|
| [2124aa] | 37 | #include <boost/graph/adjacency_list.hpp>
 | 
|---|
 | 38 | #include <boost/graph/breadth_first_search.hpp>
 | 
|---|
 | 39 | #include <boost/graph/subgraph.hpp>
 | 
|---|
 | 40 | 
 | 
|---|
| [8aa597] | 41 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 42 | 
 | 
|---|
| [2124aa] | 43 | #include <algorithm>
 | 
|---|
 | 44 | #include <iterator>
 | 
|---|
 | 45 | #include <set>
 | 
|---|
| [caa00e9] | 46 | #include <sstream>
 | 
|---|
| [8aa597] | 47 | #include <utility>
 | 
|---|
 | 48 | #include <vector>
 | 
|---|
 | 49 | #include <boost/assign.hpp>
 | 
|---|
| [2124aa] | 50 | #include <boost/bimap.hpp>
 | 
|---|
 | 51 | #include <boost/bimap/set_of.hpp>
 | 
|---|
 | 52 | #include <boost/bimap/multiset_of.hpp>
 | 
|---|
| [64bdfd] | 53 | #include <boost/bind.hpp>
 | 
|---|
| [caa00e9] | 54 | #include <boost/foreach.hpp>
 | 
|---|
| [8aa597] | 55 | 
 | 
|---|
| [301dbf] | 56 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [51e0e3] | 57 | #include "CodePatterns/IteratorAdaptors.hpp"
 | 
|---|
| [8aa597] | 58 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [51e0e3] | 59 | #include "CodePatterns/toString.hpp"
 | 
|---|
| [8aa597] | 60 | 
 | 
|---|
 | 61 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | #include "FunctionApproximation/Extractors.hpp"
 | 
|---|
 | 64 | #include "FunctionApproximation/FunctionArgument.hpp"
 | 
|---|
 | 65 | 
 | 
|---|
| [67044a] | 66 | #include "Fragmentation/Homology/HomologyGraph.hpp"
 | 
|---|
 | 67 | 
 | 
|---|
| [8aa597] | 68 | using namespace boost::assign;
 | 
|---|
 | 69 | 
 | 
|---|
| [49f163] | 70 | FunctionModel::arguments_t
 | 
|---|
 | 71 | Extractors::gatherAllSymmetricDistanceArguments(
 | 
|---|
| [691be4] | 72 |     const Fragment::positions_t& positions,
 | 
|---|
| [c7aac9] | 73 |     const Fragment::atomicnumbers_t& atomicnumbers,
 | 
|---|
| [228340] | 74 |     const FragmentationEdges::edges_t &edges,
 | 
|---|
| [49f163] | 75 |     const size_t globalid)
 | 
|---|
 | 76 | {
 | 
|---|
 | 77 |   FunctionModel::arguments_t result;
 | 
|---|
 | 78 | 
 | 
|---|
| [2124aa] | 79 |   // place edges in map
 | 
|---|
 | 80 |   typedef std::set< std::pair<size_t, size_t> > sorted_edges_t;
 | 
|---|
 | 81 |   sorted_edges_t sorted_edges;
 | 
|---|
 | 82 |   for (FragmentationEdges::edges_t::const_iterator edgeiter = edges.begin();
 | 
|---|
 | 83 |       edgeiter != edges.end(); ++edgeiter) {
 | 
|---|
 | 84 |     std::pair<sorted_edges_t::iterator, bool> inserter =
 | 
|---|
 | 85 |         sorted_edges.insert(
 | 
|---|
 | 86 |         (edgeiter->first < edgeiter->second) ?
 | 
|---|
 | 87 |             std::make_pair(edgeiter->first, edgeiter->second) :
 | 
|---|
 | 88 |             std::make_pair(edgeiter->second, edgeiter->first));
 | 
|---|
 | 89 |     ASSERT(inserter.second,
 | 
|---|
 | 90 |         "Extractors::gatherAllSymmetricDistanceArguments() - edge "
 | 
|---|
 | 91 |         +toString(*inserter.first)+" is already present");
 | 
|---|
 | 92 |   }
 | 
|---|
 | 93 | 
 | 
|---|
| [49f163] | 94 |   // go through current configuration and gather all other distances
 | 
|---|
 | 95 |   Fragment::positions_t::const_iterator firstpositer = positions.begin();
 | 
|---|
 | 96 |   for (;firstpositer != positions.end(); ++firstpositer) {
 | 
|---|
 | 97 |     Fragment::positions_t::const_iterator secondpositer = firstpositer;
 | 
|---|
 | 98 |     for (; secondpositer != positions.end(); ++secondpositer) {
 | 
|---|
 | 99 |       if (firstpositer == secondpositer)
 | 
|---|
 | 100 |         continue;
 | 
|---|
 | 101 |       argument_t arg;
 | 
|---|
 | 102 |       const Vector firsttemp((*firstpositer)[0],(*firstpositer)[1],(*firstpositer)[2]);
 | 
|---|
 | 103 |       const Vector secondtemp((*secondpositer)[0],(*secondpositer)[1],(*secondpositer)[2]);
 | 
|---|
| [8aa597] | 104 |       arg.distance = firsttemp.distance(secondtemp);
 | 
|---|
| [691be4] | 105 |       arg.types = std::make_pair(
 | 
|---|
| [c7aac9] | 106 |           (int)atomicnumbers[ std::distance(positions.begin(), firstpositer) ],
 | 
|---|
 | 107 |           (int)atomicnumbers[ std::distance(positions.begin(), secondpositer) ]
 | 
|---|
| [691be4] | 108 |           );
 | 
|---|
| [8aa597] | 109 |       arg.indices = std::make_pair(
 | 
|---|
 | 110 |           std::distance(
 | 
|---|
 | 111 |               positions.begin(), firstpositer),
 | 
|---|
 | 112 |           std::distance(
 | 
|---|
 | 113 |               positions.begin(), secondpositer)
 | 
|---|
 | 114 |           );
 | 
|---|
 | 115 |       arg.globalid = globalid;
 | 
|---|
| [2124aa] | 116 |       arg.bonded = sorted_edges.find(arg.indices) != sorted_edges.end();
 | 
|---|
| [31a2be] | 117 |       LOG(3, "DEBUG: Created argument " << arg << ".");
 | 
|---|
| [8aa597] | 118 |       result.push_back(arg);
 | 
|---|
 | 119 |     }
 | 
|---|
 | 120 |   }
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |   return result;
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
| [bc6705] | 125 | Extractors::elementcounts_t 
 | 
|---|
 | 126 | Extractors::_detail::getElementCounts(
 | 
|---|
| [c5e75f3] | 127 |     const Fragment::atomicnumbers_t elements
 | 
|---|
| [bc6705] | 128 |   )
 | 
|---|
 | 129 | {
 | 
|---|
 | 130 |   elementcounts_t elementcounts;
 | 
|---|
| [c5e75f3] | 131 |   for (Fragment::atomicnumbers_t::const_iterator elementiter = elements.begin();
 | 
|---|
| [bc6705] | 132 |       elementiter != elements.end(); ++elementiter) {
 | 
|---|
 | 133 |     // insert new element
 | 
|---|
 | 134 |     std::pair< elementcounts_t::iterator, bool> inserter =
 | 
|---|
 | 135 |         elementcounts.insert( std::make_pair( *elementiter, 1) );
 | 
|---|
 | 136 |     // if already present, just increase its count
 | 
|---|
 | 137 |     if (!inserter.second)
 | 
|---|
 | 138 |       ++(inserter.first->second);
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   return elementcounts;
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
| [51e0e3] | 143 | struct ParticleTypesComparator {
 | 
|---|
 | 144 |   bool operator()(const argument_t::types_t &a, const argument_t::types_t &b)
 | 
|---|
 | 145 |   {
 | 
|---|
 | 146 |     if (a.first < a.second) {
 | 
|---|
 | 147 |       if (b.first < b.second) {
 | 
|---|
 | 148 |         if (a.first < b.first)
 | 
|---|
 | 149 |           return true;
 | 
|---|
 | 150 |         else if (a.first > b.first)
 | 
|---|
 | 151 |           return false;
 | 
|---|
 | 152 |         else
 | 
|---|
 | 153 |           return (a.second < b.second);
 | 
|---|
 | 154 |       } else {
 | 
|---|
 | 155 |         if (a.first < b.second)
 | 
|---|
 | 156 |           return true;
 | 
|---|
 | 157 |         else if (a.first > b.second)
 | 
|---|
 | 158 |           return false;
 | 
|---|
 | 159 |         else
 | 
|---|
 | 160 |           return (a.second < b.first);
 | 
|---|
 | 161 |       }
 | 
|---|
 | 162 |     } else {
 | 
|---|
 | 163 |       if (b.first < b.second) {
 | 
|---|
 | 164 |         if (a.second < b.first)
 | 
|---|
 | 165 |           return true;
 | 
|---|
 | 166 |         else if (a.second > b.first)
 | 
|---|
 | 167 |           return false;
 | 
|---|
 | 168 |         else
 | 
|---|
 | 169 |           return (a.first < b.second);
 | 
|---|
 | 170 |       } else {
 | 
|---|
 | 171 |         if (a.second < b.second)
 | 
|---|
 | 172 |           return true;
 | 
|---|
 | 173 |         else if (a.second > b.second)
 | 
|---|
 | 174 |           return false;
 | 
|---|
 | 175 |         else
 | 
|---|
 | 176 |           return (a.first < b.first);
 | 
|---|
 | 177 |       }
 | 
|---|
 | 178 |     }
 | 
|---|
 | 179 |   }
 | 
|---|
 | 180 | };
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 | std::ostream& operator<<(std::ostream &out, const argument_t::types_t &a)
 | 
|---|
 | 183 | {
 | 
|---|
 | 184 |   out << "[" << a.first << "," << a.second << "]";
 | 
|---|
 | 185 |   return out;
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
| [2124aa] | 188 | typedef size_t level_t;
 | 
|---|
 | 189 | typedef size_t node_t;
 | 
|---|
 | 190 | typedef std::multimap< level_t, node_t > nodes_per_level_t;
 | 
|---|
 | 191 | typedef std::set<node_t> nodes_t;
 | 
|---|
 | 192 | typedef std::set<nodes_t> set_of_nodes_t;
 | 
|---|
 | 193 | typedef boost::property_map < boost::adjacency_list <>, boost::vertex_index_t >::type index_map_t;
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 | typedef boost::bimap<
 | 
|---|
 | 196 |     boost::bimaps::set_of< size_t >,
 | 
|---|
 | 197 |     boost::bimaps::multiset_of< Extractors::ParticleType_t >
 | 
|---|
 | 198 | > type_index_lookup_t;
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 | typedef std::set<node_t> set_type;
 | 
|---|
 | 201 | typedef std::set<set_type> powerset_type;
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 | typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
 | 
|---|
 | 204 |     boost::no_property, boost::no_property > UndirectedGraph;
 | 
|---|
 | 205 | typedef boost::subgraph< UndirectedGraph > UndirectedSubgraph;
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 | typedef std::map< node_t, std::pair<Extractors::ParticleType_t, size_t> > node_FragmentNode_map_t;
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | typedef std::map< argument_t::indices_t, size_t> argument_placement_map_t;
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 | typedef std::map<size_t, size_t> argindex_to_nodeindex_t;
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 | void insertIntoNodeFragmentMap(
 | 
|---|
 | 214 |     node_FragmentNode_map_t &_node_FragmentNode_map,
 | 
|---|
 | 215 |     const size_t &_index,
 | 
|---|
 | 216 |     const Extractors::ParticleType_t &_type)
 | 
|---|
 | 217 | {
 | 
|---|
 | 218 |   const node_FragmentNode_map_t::iterator mapiter = _node_FragmentNode_map.find(_index);
 | 
|---|
 | 219 |   // check if already present
 | 
|---|
 | 220 |   if (mapiter != _node_FragmentNode_map.end()) {
 | 
|---|
 | 221 |     // assert same type and increment number of edges
 | 
|---|
 | 222 |     ASSERT( mapiter->second.first == _type,
 | 
|---|
 | 223 |         "insertIntoNodeFragmentMap() - different types "+toString(mapiter->second.first)+
 | 
|---|
 | 224 |         " and "+toString(_type)+" for node "+toString(_index));
 | 
|---|
 | 225 |     ++(mapiter->second.second);
 | 
|---|
 | 226 |   } else {
 | 
|---|
 | 227 |     // place new entry with a single present edge
 | 
|---|
 | 228 |     _node_FragmentNode_map.insert( std::make_pair(_index, std::make_pair(_type, 1) ));
 | 
|---|
 | 229 |   }
 | 
|---|
 | 230 | }
 | 
|---|
 | 231 | 
 | 
|---|
 | 232 | static node_FragmentNode_map_t fillNodeFragmentMap(
 | 
|---|
 | 233 |     FunctionModel::arguments_t &argumentbunch)
 | 
|---|
 | 234 | {
 | 
|---|
 | 235 |   node_FragmentNode_map_t node_FragmentNode_map;
 | 
|---|
 | 236 |   // place each node index with type and number of edges into map
 | 
|---|
 | 237 |   for (FunctionModel::arguments_t::const_iterator argiter = argumentbunch.begin();
 | 
|---|
 | 238 |       argiter != argumentbunch.end(); ++argiter) {
 | 
|---|
 | 239 |     const argument_t &arg = *argiter;
 | 
|---|
 | 240 |     // only consider the distances that represent a bond edge
 | 
|---|
 | 241 |     if (arg.bonded) {
 | 
|---|
 | 242 |       insertIntoNodeFragmentMap(node_FragmentNode_map, arg.indices.first, arg.types.first);
 | 
|---|
 | 243 |       insertIntoNodeFragmentMap(node_FragmentNode_map, arg.indices.second, arg.types.second);
 | 
|---|
 | 244 |     }
 | 
|---|
 | 245 |   }
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |   return node_FragmentNode_map;
 | 
|---|
 | 248 | }
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 | static argument_placement_map_t fillArgumentsPlacementMap(const size_t num_args)
 | 
|---|
 | 251 | {
 | 
|---|
 | 252 |   argument_placement_map_t argument_placement_map;
 | 
|---|
 | 253 |   size_t placement = 0;
 | 
|---|
 | 254 |   for (size_t i = 0;i<num_args;++i)
 | 
|---|
 | 255 |     for (size_t j = i+1;j<num_args;++j)
 | 
|---|
 | 256 |       argument_placement_map.insert( std::make_pair( argument_t::indices_t(i,j), placement++) );
 | 
|---|
 | 257 |   return argument_placement_map;
 | 
|---|
 | 258 | }
 | 
|---|
 | 259 | 
 | 
|---|
 | 260 | static argument_t::indices_t translateIndices(
 | 
|---|
 | 261 |     const argindex_to_nodeindex_t &_argindex_to_nodeindex,
 | 
|---|
 | 262 |     const argument_t::indices_t &_indices
 | 
|---|
 | 263 |     )
 | 
|---|
 | 264 | {
 | 
|---|
 | 265 |   const argindex_to_nodeindex_t::const_iterator firstiter =
 | 
|---|
 | 266 |       _argindex_to_nodeindex.find(_indices.first);
 | 
|---|
 | 267 |   ASSERT( firstiter != _argindex_to_nodeindex.end(),
 | 
|---|
 | 268 |       "translateIndices() - could not find #"+toString(_indices.first)+
 | 
|---|
 | 269 |       " in translation map.");
 | 
|---|
 | 270 |   const argindex_to_nodeindex_t::const_iterator seconditer =
 | 
|---|
 | 271 |       _argindex_to_nodeindex.find(_indices.second);
 | 
|---|
 | 272 |   ASSERT(seconditer != _argindex_to_nodeindex.end(),
 | 
|---|
 | 273 |       "translateIndices() - could not find #"+toString(_indices.second)+
 | 
|---|
 | 274 |       " in translation map.");
 | 
|---|
 | 275 |   // mind increasing order of indices in pair
 | 
|---|
 | 276 |   if (firstiter->second < seconditer->second)
 | 
|---|
 | 277 |     return argument_t::indices_t(firstiter->second, seconditer->second);
 | 
|---|
 | 278 |   else
 | 
|---|
 | 279 |     return argument_t::indices_t(seconditer->second, firstiter->second);
 | 
|---|
 | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 | /** Power set generator
 | 
|---|
 | 283 |  *
 | 
|---|
 | 284 |  * taken from https://rosettacode.org/wiki/Power_set#Non-recursive_version
 | 
|---|
 | 285 |  *
 | 
|---|
 | 286 |  */
 | 
|---|
 | 287 | static powerset_type powerset(set_type const& set)
 | 
|---|
 | 288 | {
 | 
|---|
 | 289 |   typedef set_type::const_iterator set_iter;
 | 
|---|
 | 290 |   typedef std::vector<set_iter> vec;
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 |   struct local
 | 
|---|
 | 293 |   {
 | 
|---|
 | 294 |     static int dereference(set_iter v) { return *v; }
 | 
|---|
 | 295 |   };
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 |   powerset_type result;
 | 
|---|
 | 298 | 
 | 
|---|
 | 299 |   vec elements;
 | 
|---|
 | 300 |   do {
 | 
|---|
 | 301 |     set_type tmp;
 | 
|---|
 | 302 |     std::transform(elements.begin(), elements.end(),
 | 
|---|
 | 303 |                    std::inserter(tmp, tmp.end()),
 | 
|---|
 | 304 |                    local::dereference);
 | 
|---|
 | 305 |     result.insert(tmp);
 | 
|---|
 | 306 |     if (!elements.empty() && ++elements.back() == set.end()) {
 | 
|---|
 | 307 |       elements.pop_back();
 | 
|---|
 | 308 |     } else {
 | 
|---|
 | 309 |       set_iter iter;
 | 
|---|
 | 310 |       if (elements.empty()) {
 | 
|---|
 | 311 |         iter = set.begin();
 | 
|---|
 | 312 |       } else {
 | 
|---|
 | 313 |         iter = elements.back();
 | 
|---|
 | 314 |         ++iter;
 | 
|---|
 | 315 |       }
 | 
|---|
 | 316 |       for (; iter != set.end(); ++iter) {
 | 
|---|
 | 317 |         elements.push_back(iter);
 | 
|---|
 | 318 |       }
 | 
|---|
 | 319 |     }
 | 
|---|
 | 320 |   } while (!elements.empty());
 | 
|---|
 | 321 | 
 | 
|---|
 | 322 |   return result;
 | 
|---|
 | 323 | }
 | 
|---|
 | 324 | 
 | 
|---|
 | 325 | /** Recursive function to generate all induced, connected subgraphs given a
 | 
|---|
 | 326 |  * graph.
 | 
|---|
 | 327 |  *
 | 
|---|
 | 328 |  * \param N number of still left to pick
 | 
|---|
 | 329 |  * \param depth level in \a set_of_nodes
 | 
|---|
 | 330 |  * \param nodes current set of nodes that are picked already
 | 
|---|
 | 331 |  * \param set_of_nodes resulting set of generated subgraphs' nodes
 | 
|---|
 | 332 |  * \param nodes_per_level level-wise frontier of connected nodes around a root node
 | 
|---|
 | 333 |  * \param graph graph containing the adjacency
 | 
|---|
 | 334 |  * \param index_map with indices per \a graph' vertex
 | 
|---|
 | 335 |  */
 | 
|---|
 | 336 | static void generateAllInducedConnectedSubgraphs(
 | 
|---|
 | 337 |     const size_t N,
 | 
|---|
 | 338 |     const level_t level,
 | 
|---|
 | 339 |     const nodes_t &nodes,
 | 
|---|
 | 340 |     set_of_nodes_t &set_of_nodes,
 | 
|---|
 | 341 |     const nodes_per_level_t &nodes_per_level,
 | 
|---|
 | 342 |     const UndirectedGraph &graph,
 | 
|---|
 | 343 |     const std::vector<size_t> &_distance,
 | 
|---|
 | 344 |     const index_map_t &index_map)
 | 
|---|
 | 345 | {
 | 
|---|
 | 346 |   ASSERT( nodes_per_level.find(level) != nodes_per_level.end(),
 | 
|---|
 | 347 |       "generateAllInducedConnectedSubgraphs() - we are deeper than the graph.");
 | 
|---|
 | 348 |   ASSERT( N < nodes_per_level.size(),
 | 
|---|
 | 349 |       "generateAllInducedConnectedSubgraphs() - we are looking for subgraphs larger than the graph.");
 | 
|---|
 | 350 |   if (N > 0) {
 | 
|---|
 | 351 |     LOG(3, "DEBUG: At level " << level << " current nodes is " << nodes << ", need to find " << N << " more.");
 | 
|---|
 | 352 |     // get next level's set and constrain to nodes connected to this set
 | 
|---|
 | 353 |     nodes_t validnodes;
 | 
|---|
 | 354 |     std::pair< nodes_per_level_t::const_iterator, nodes_per_level_t::const_iterator> range =
 | 
|---|
 | 355 |         nodes_per_level.equal_range(level);
 | 
|---|
 | 356 |     for (nodes_per_level_t::const_iterator rangeiter = range.first;
 | 
|---|
 | 357 |         rangeiter != range.second; ++rangeiter) {
 | 
|---|
 | 358 |       LOG(4, "DEBUG: Checking edges further away from node #" << rangeiter->second);
 | 
|---|
 | 359 |       // get all edges connected to this node further away
 | 
|---|
 | 360 |       UndirectedGraph::in_edge_iterator i, end;
 | 
|---|
 | 361 |       boost::tie(i, end) = boost::in_edges(boost::vertex(rangeiter->second, graph), graph);
 | 
|---|
 | 362 |       for (;i != end; ++i) {
 | 
|---|
 | 363 |         // check each edge whether it's in nodes
 | 
|---|
 | 364 |         const node_t sourceindex = boost::get(index_map, boost::source(*i, graph));
 | 
|---|
 | 365 |         const node_t targetindex = boost::get(index_map, boost::target(*i, graph));
 | 
|---|
 | 366 |         const size_t &source_distance = _distance[sourceindex];
 | 
|---|
 | 367 |         const size_t &target_distance = _distance[targetindex];
 | 
|---|
 | 368 |         // edge is going deeper into graph
 | 
|---|
 | 369 |         if (((source_distance == level) && (target_distance == (level+1)))
 | 
|---|
 | 370 |             || ((source_distance == (level+1)) && (target_distance == level))) {
 | 
|---|
 | 371 |           LOG(5, "DEBUG: Candidate edge on level " << level << " is from " << sourceindex
 | 
|---|
 | 372 |               << " to " << targetindex << ".");
 | 
|---|
 | 373 |           // pick right index and check for not present in list yet
 | 
|---|
 | 374 |           if (sourceindex == rangeiter->second) {
 | 
|---|
 | 375 |             if (nodes.count(targetindex) == 0) {
 | 
|---|
 | 376 |               validnodes.insert(targetindex);
 | 
|---|
 | 377 |               LOG(4, "DEBUG: Inserting node #" << targetindex << " into valid nodes.");
 | 
|---|
 | 378 |             }
 | 
|---|
 | 379 |           } else if (targetindex == rangeiter->second) {
 | 
|---|
 | 380 |             if (nodes.count(sourceindex) == 0) {
 | 
|---|
 | 381 |               validnodes.insert(sourceindex);
 | 
|---|
 | 382 |               LOG(4, "DEBUG: Inserting node #" << sourceindex << " into valid nodes.");
 | 
|---|
 | 383 |             }
 | 
|---|
 | 384 |           } else {
 | 
|---|
 | 385 |             ASSERT(0,
 | 
|---|
 | 386 |                 "generateAllInducedConnectedSubgraphs() - neither source #"+toString(sourceindex)+
 | 
|---|
 | 387 |                 " nor target #"+toString(targetindex)+" is equal to #"+toString(rangeiter->second));
 | 
|---|
 | 388 |           }
 | 
|---|
 | 389 |         }
 | 
|---|
 | 390 |       }
 | 
|---|
 | 391 |     }
 | 
|---|
 | 392 |     // skip this if we cannot go deeper into the graph from here
 | 
|---|
 | 393 |     if (validnodes.empty()) {
 | 
|---|
 | 394 |       LOG(3, "DEBUG: We did not find any more nodes to step on from " << nodes << ".");
 | 
|---|
 | 395 |       return;
 | 
|---|
 | 396 |     }
 | 
|---|
 | 397 | 
 | 
|---|
 | 398 |     // go through power set
 | 
|---|
 | 399 |     const powerset_type test_powerset = powerset(validnodes);
 | 
|---|
 | 400 |     for (powerset_type::const_iterator iter = test_powerset.begin();
 | 
|---|
 | 401 |          iter != test_powerset.end();
 | 
|---|
 | 402 |          ++iter) {
 | 
|---|
 | 403 |       // count set bits (#elements in *iter), must be between 1 and N
 | 
|---|
 | 404 |       const size_t num_set_bits = iter->size();
 | 
|---|
 | 405 |       if ((num_set_bits > 0) && (num_set_bits <= N)) {
 | 
|---|
 | 406 |         // add set to nodes
 | 
|---|
 | 407 |         LOG(3, "DEBUG: With present " << nodes << " the current set is " << *iter << " of "
 | 
|---|
 | 408 |             << validnodes << ".");
 | 
|---|
 | 409 | 
 | 
|---|
 | 410 |         // copy the nodes before insertion
 | 
|---|
 | 411 |         nodes_t filled_nodes(nodes.begin(), nodes.end());
 | 
|---|
 | 412 |         filled_nodes.insert(iter->begin(), iter->end());
 | 
|---|
 | 413 | 
 | 
|---|
 | 414 |         // and recurse
 | 
|---|
 | 415 |         generateAllInducedConnectedSubgraphs(
 | 
|---|
 | 416 |             N-num_set_bits, level+1, filled_nodes, set_of_nodes, nodes_per_level, graph, _distance, index_map);
 | 
|---|
 | 417 |       }
 | 
|---|
 | 418 |     }
 | 
|---|
 | 419 |   } else {
 | 
|---|
 | 420 |     // N==0: we have a winner
 | 
|---|
 | 421 |     std::pair<set_of_nodes_t::iterator, bool> inserter =
 | 
|---|
 | 422 |         set_of_nodes.insert( nodes );
 | 
|---|
 | 423 |     if (!inserter.second)
 | 
|---|
 | 424 |       LOG(2, "DEBUG: subgraph " << nodes << " is already contained in set_of_nodes.");
 | 
|---|
 | 425 |     else
 | 
|---|
 | 426 |       LOG(2, "DEBUG: subgraph " << nodes << " inserted into set_of_nodes.");
 | 
|---|
 | 427 |   }
 | 
|---|
 | 428 | }
 | 
|---|
 | 429 | 
 | 
|---|
 | 430 | static Extractors::ParticleType_t getParticleTypeToNode(
 | 
|---|
 | 431 |     const type_index_lookup_t &type_index_lookup,
 | 
|---|
 | 432 |     const size_t nodeindex)
 | 
|---|
 | 433 | {
 | 
|---|
 | 434 |   const type_index_lookup_t::left_const_iterator typeiter = type_index_lookup.left.find(nodeindex);
 | 
|---|
 | 435 |   ASSERT( typeiter != type_index_lookup.left.end(),
 | 
|---|
 | 436 |       "getParticleTypeToNode() - could not find type to node #"+toString(nodeindex));
 | 
|---|
 | 437 |   return typeiter->second;
 | 
|---|
 | 438 | }
 | 
|---|
 | 439 | 
 | 
|---|
 | 440 | static HomologyGraph createHomologyGraphFromNodes(
 | 
|---|
 | 441 |     const nodes_t &nodes,
 | 
|---|
 | 442 |     const type_index_lookup_t &type_index_lookup,
 | 
|---|
 | 443 |     const UndirectedGraph &graph,
 | 
|---|
 | 444 |     const index_map_t &index_map
 | 
|---|
 | 445 |     )
 | 
|---|
 | 446 | {
 | 
|---|
 | 447 |   HomologyGraph::nodes_t graph_nodes;
 | 
|---|
 | 448 |   HomologyGraph::edges_t graph_edges;
 | 
|---|
 | 449 |   {
 | 
|---|
 | 450 |     typedef std::set< std::pair<node_t, node_t> > graph_edges_t;
 | 
|---|
 | 451 |     graph_edges_t edge_set;
 | 
|---|
 | 452 |     std::pair<HomologyGraph::nodes_t::iterator, bool > inserter;
 | 
|---|
 | 453 |     for (nodes_t::const_iterator nodeiter = nodes.begin();
 | 
|---|
 | 454 |         nodeiter != nodes.end(); ++nodeiter) {
 | 
|---|
 | 455 |       const Extractors::ParticleType_t &nodetype = getParticleTypeToNode(type_index_lookup, *nodeiter);
 | 
|---|
 | 456 | 
 | 
|---|
 | 457 |       // count edges in constrained set for this particular node
 | 
|---|
 | 458 |       size_t num_edges = 0;
 | 
|---|
 | 459 |       UndirectedGraph::in_edge_iterator i, end;
 | 
|---|
 | 460 |       for (boost::tie(i, end) = boost::in_edges(boost::vertex(*nodeiter, graph), graph);
 | 
|---|
 | 461 |           i != end; ++i) {
 | 
|---|
 | 462 |         const node_t sourceindex = boost::get(index_map, boost::source(*i, graph));
 | 
|---|
 | 463 |         const node_t targetindex = boost::get(index_map, boost::target(*i, graph));
 | 
|---|
 | 464 |         // check each edge whether it's in nodes
 | 
|---|
 | 465 |         if ((nodes.count(sourceindex) != 0) && (nodes.count(targetindex) != 0)) {
 | 
|---|
 | 466 |           // increase edge count of node
 | 
|---|
 | 467 |           ++num_edges;
 | 
|---|
 | 468 |           // we first store edges in a set to ensure their uniqueness (as we encounter
 | 
|---|
 | 469 |           // each edge two times and we don't know if source and target will be
 | 
|---|
 | 470 |           // different the second time encountered)
 | 
|---|
 | 471 |           if (sourceindex < targetindex)
 | 
|---|
 | 472 |             edge_set.insert( std::make_pair(sourceindex, targetindex) );
 | 
|---|
 | 473 |           else
 | 
|---|
 | 474 |             edge_set.insert( std::make_pair(targetindex, sourceindex) );
 | 
|---|
 | 475 |         }
 | 
|---|
 | 476 |       }
 | 
|---|
 | 477 |       LOG(4, "DEBUG: Node #" << *nodeiter << " has " << num_edges << " edges.");
 | 
|---|
 | 478 | 
 | 
|---|
 | 479 |       // add FragmentNode
 | 
|---|
 | 480 |       inserter = graph_nodes.insert( std::make_pair(FragmentNode(nodetype, num_edges), 1) );
 | 
|---|
 | 481 |       if (!inserter.second)
 | 
|---|
 | 482 |         ++(inserter.first->second);
 | 
|---|
 | 483 |     }
 | 
|---|
 | 484 | 
 | 
|---|
 | 485 |     // add edges
 | 
|---|
 | 486 |     for (graph_edges_t::const_iterator edgeiter = edge_set.begin();
 | 
|---|
 | 487 |         edgeiter != edge_set.end(); ++edgeiter) {
 | 
|---|
 | 488 |       const Extractors::ParticleType_t sourcetype =
 | 
|---|
 | 489 |           getParticleTypeToNode(type_index_lookup, edgeiter->first);
 | 
|---|
 | 490 |       const Extractors::ParticleType_t targettype =
 | 
|---|
 | 491 |           getParticleTypeToNode(type_index_lookup, edgeiter->second);
 | 
|---|
 | 492 |       // FragmentEdge takes care of proper sorting
 | 
|---|
 | 493 |       FragmentEdge edge(sourcetype, targettype);
 | 
|---|
 | 494 |       LOG(4, "DEBUG: Adding fragment edge " << edge);
 | 
|---|
 | 495 |       std::pair<HomologyGraph::edges_t::iterator, bool > inserter;
 | 
|---|
 | 496 |       inserter = graph_edges.insert( std::make_pair( edge, 1) );
 | 
|---|
 | 497 |       if (!inserter.second)
 | 
|---|
 | 498 |         ++(inserter.first->second);
 | 
|---|
 | 499 |     }
 | 
|---|
 | 500 |   }
 | 
|---|
 | 501 | 
 | 
|---|
 | 502 |   return HomologyGraph(graph_nodes, graph_edges);
 | 
|---|
 | 503 | }
 | 
|---|
 | 504 | 
 | 
|---|
 | 505 | /**
 | 
|---|
 | 506 |  * I have no idea why this is so complicated with BGL ...
 | 
|---|
 | 507 |  *
 | 
|---|
 | 508 |  * This is taken from the book "The Boost Graph Library: User Guide and Reference Manual, Portable Documents",
 | 
|---|
 | 509 |  * chapter "Basic Graph Algorithms", example on calculating the bacon number.
 | 
|---|
 | 510 |  */
 | 
|---|
 | 511 | template <typename DistanceMap>
 | 
|---|
 | 512 | class distance_recorder : public boost::default_bfs_visitor
 | 
|---|
 | 513 | {
 | 
|---|
 | 514 | public:
 | 
|---|
 | 515 |   distance_recorder(DistanceMap dist) : d(dist) {}
 | 
|---|
 | 516 | 
 | 
|---|
 | 517 |   template <typename Edge, typename Graph>
 | 
|---|
 | 518 |   void tree_edge(Edge e, const Graph &g) const {
 | 
|---|
 | 519 |     typename boost::graph_traits<Graph>::vertex_descriptor u = source(e,g), v = target(e,g);
 | 
|---|
 | 520 |     d[v] = d[u] + 1;
 | 
|---|
 | 521 |   }
 | 
|---|
 | 522 | 
 | 
|---|
 | 523 | private:
 | 
|---|
 | 524 |   DistanceMap d;
 | 
|---|
 | 525 | };
 | 
|---|
 | 526 | 
 | 
|---|
 | 527 | template <typename DistanceMap>
 | 
|---|
 | 528 | distance_recorder<DistanceMap> record_distance(DistanceMap d)
 | 
|---|
 | 529 | {
 | 
|---|
 | 530 |   return distance_recorder<DistanceMap>(d);
 | 
|---|
 | 531 | }
 | 
|---|
 | 532 | 
 | 
|---|
| [e1fe7e] | 533 | FunctionModel::list_of_arguments_t Extractors::reorderArgumentsByParticleTypes(
 | 
|---|
 | 534 |     const FunctionModel::list_of_arguments_t &listargs,
 | 
|---|
| [e60558] | 535 |     const HomologyGraph &_graph,
 | 
|---|
| [67044a] | 536 |     const ParticleTypes_t &_types,
 | 
|---|
 | 537 |     const HomologyGraph &_bindingmodel
 | 
|---|
| [df350c] | 538 |     )
 | 
|---|
| [51e0e3] | 539 | {
 | 
|---|
| [e1fe7e] | 540 |   FunctionModel::list_of_arguments_t returnargs;
 | 
|---|
| [2124aa] | 541 |   for (FunctionModel::list_of_arguments_t::const_iterator listiter = listargs.begin();
 | 
|---|
 | 542 |       listiter != listargs.end(); ++listiter) {
 | 
|---|
 | 543 |     const FunctionModel::arguments_t &args = *listiter;
 | 
|---|
 | 544 | 
 | 
|---|
 | 545 |     // deal with the case when there are no distances (ConstantPotential)
 | 
|---|
 | 546 |     if (_bindingmodel.getNodes().size() < 2) {
 | 
|---|
 | 547 |       LOG(3, "DEBUG: Potential requires only zero or no particle types, needs no distances.");
 | 
|---|
 | 548 |       continue;
 | 
|---|
 | 549 |     }
 | 
|---|
 | 550 |     if (_bindingmodel.getEdges().empty()) {
 | 
|---|
 | 551 |       LOG(3, "DEBUG: Potential represents non-bonded interactions, gets all distances.");
 | 
|---|
 | 552 |       returnargs.push_back(args);
 | 
|---|
 | 553 |       continue;
 | 
|---|
 | 554 |     }
 | 
|---|
 | 555 | 
 | 
|---|
 | 556 |     /// 0. place all nodes in a lookup map for their type
 | 
|---|
 | 557 |     type_index_lookup_t type_index_lookup;
 | 
|---|
| [e1fe7e] | 558 |     for(FunctionModel::arguments_t::const_iterator iter = args.begin();
 | 
|---|
 | 559 |         iter != args.end(); ++iter) {
 | 
|---|
| [2124aa] | 560 |       if (type_index_lookup.left.find(iter->indices.first) == type_index_lookup.left.end())
 | 
|---|
 | 561 |         type_index_lookup.left.insert( std::make_pair(iter->indices.first, iter->types.first) );
 | 
|---|
 | 562 |       else {
 | 
|---|
 | 563 |         ASSERT(type_index_lookup.left.at(iter->indices.first) == iter->types.first,
 | 
|---|
 | 564 |             "Extractors::reorderArgumentsByParticleTypes() - entry " +toString(iter->indices.first)+
 | 
|---|
 | 565 |             " is already present with different type "+toString(iter->types.first)
 | 
|---|
 | 566 |             +" than "+toString(type_index_lookup.left.at(iter->indices.first)));
 | 
|---|
 | 567 |       }
 | 
|---|
 | 568 |       if (type_index_lookup.left.find(iter->indices.second) == type_index_lookup.left.end())
 | 
|---|
 | 569 |         type_index_lookup.left.insert( std::make_pair(iter->indices.second, iter->types.second) );
 | 
|---|
 | 570 |       else {
 | 
|---|
 | 571 |         ASSERT(type_index_lookup.left.at(iter->indices.second) == iter->types.second,
 | 
|---|
 | 572 |             "Extractors::reorderArgumentsByParticleTypes() - entry " +toString(iter->indices.second)+
 | 
|---|
 | 573 |             " is already present with different type "+toString(iter->types.second)
 | 
|---|
 | 574 |             +" than "+toString(type_index_lookup.left.at(iter->indices.second)));
 | 
|---|
 | 575 |       }
 | 
|---|
| [e1fe7e] | 576 |     }
 | 
|---|
| [2124aa] | 577 |     if (DoLog(3)) {
 | 
|---|
 | 578 |       std::stringstream output;
 | 
|---|
 | 579 |       for (type_index_lookup_t::left_const_iterator indexiter = type_index_lookup.left.begin();
 | 
|---|
 | 580 |           indexiter != type_index_lookup.left.end(); ++indexiter) {
 | 
|---|
 | 581 |         output << " [" << indexiter->first << "," << indexiter->second << "]";
 | 
|---|
| [e1fe7e] | 582 |       }
 | 
|---|
| [2124aa] | 583 |       LOG(3, "DEBUG: index to type map:" << output.str());
 | 
|---|
| [51e0e3] | 584 |     }
 | 
|---|
| [2124aa] | 585 |     if (DoLog(3)) {
 | 
|---|
 | 586 |       std::stringstream output;
 | 
|---|
 | 587 |       for (type_index_lookup_t::right_const_iterator typeiter = type_index_lookup.right.begin();
 | 
|---|
 | 588 |           typeiter != type_index_lookup.right.end(); ++typeiter) {
 | 
|---|
 | 589 |         output << " [" << typeiter->first << "," << typeiter->second << "]";
 | 
|---|
 | 590 |       }
 | 
|---|
 | 591 |       LOG(3, "DEBUG: type to index map " << output.str());
 | 
|---|
 | 592 |     }
 | 
|---|
 | 593 | 
 | 
|---|
 | 594 |     /// 1. construct boost::graph from arguments_t (iter)
 | 
|---|
 | 595 |     const size_t N = type_index_lookup.left.size();
 | 
|---|
 | 596 |     UndirectedGraph fragmentgraph(N);
 | 
|---|
 | 597 |     for(FunctionModel::arguments_t::const_iterator iter = args.begin();
 | 
|---|
 | 598 |         iter != args.end(); ++iter) {
 | 
|---|
 | 599 |       if (iter->bonded)
 | 
|---|
 | 600 |         boost::add_edge(iter->indices.first, iter->indices.second, fragmentgraph);
 | 
|---|
 | 601 |     }
 | 
|---|
 | 602 |     index_map_t index_map = boost::get(boost::vertex_index, fragmentgraph);
 | 
|---|
 | 603 |     LOG(2, "DEBUG: We have " << boost::num_vertices(fragmentgraph) << " nodes in the fragment graph.");
 | 
|---|
 | 604 | 
 | 
|---|
 | 605 |     /// 2. grab first node of the binding model (gives a type)
 | 
|---|
 | 606 |     const FragmentNode &firstnode = _bindingmodel.getNodes().begin()->first;
 | 
|---|
 | 607 |     const size_t &firsttimes = _bindingmodel.getNodes().begin()->second;
 | 
|---|
 | 608 |     const size_t &firsttype = firstnode.getAtomicNumber(); // TODO: convert to ParticleNumber_t ?
 | 
|---|
 | 609 | 
 | 
|---|
 | 610 |     /// 3. grab all candidate nodes contained in arguments_t
 | 
|---|
 | 611 |     std::pair<
 | 
|---|
 | 612 |         type_index_lookup_t::right_const_iterator,
 | 
|---|
 | 613 |         type_index_lookup_t::right_const_iterator> range = type_index_lookup.right.equal_range(firsttype);
 | 
|---|
 | 614 | #ifndef NDEBUG
 | 
|---|
 | 615 |     const size_t checktimes = std::distance(range.first, range.second);
 | 
|---|
 | 616 |     ASSERT( firsttimes == checktimes,
 | 
|---|
 | 617 |         "Extractors::reorderArgumentsByParticleTypes() - unequal amounts of same first type "
 | 
|---|
 | 618 |         +toString(firsttype)+" in HomologyGraph and in type lookup "+toString(checktimes));
 | 
|---|
 | 619 | #endif
 | 
|---|
 | 620 | 
 | 
|---|
 | 621 |     /// 4. go over all candidate nodes (gives index)
 | 
|---|
 | 622 |     set_of_nodes_t set_of_nodes;
 | 
|---|
 | 623 |     for (type_index_lookup_t::right_const_iterator rangeiter = range.first;
 | 
|---|
 | 624 |         rangeiter != range.second; ++rangeiter) {
 | 
|---|
 | 625 |       const size_t &rootindex = rangeiter->second;
 | 
|---|
 | 626 |       LOG(2, "DEBUG: Current root index is " << rootindex);
 | 
|---|
 | 627 | 
 | 
|---|
 | 628 |       /// 5a. from node in graph (with this index) perform BFS till n-1 (#nodes in binding model)
 | 
|---|
 | 629 |       std::vector<size_t> distances(boost::num_vertices(fragmentgraph));
 | 
|---|
 | 630 |       boost::breadth_first_search(
 | 
|---|
 | 631 |           fragmentgraph,
 | 
|---|
 | 632 |           boost::vertex(rootindex, fragmentgraph),
 | 
|---|
 | 633 |           boost::visitor(record_distance(&distances[0])));
 | 
|---|
 | 634 |       LOG(3, "DEBUG: BFS discovered the following distances " << distances);
 | 
|---|
 | 635 | 
 | 
|---|
 | 636 |       /// 5b. and store all node in map with distance to root as key
 | 
|---|
 | 637 |       nodes_per_level_t nodes_per_level;
 | 
|---|
 | 638 |       for (size_t i=0;i<distances.size();++i) {
 | 
|---|
 | 639 |         nodes_per_level.insert( std::make_pair(level_t(distances[i]), node_t(i)) );
 | 
|---|
 | 640 |       }
 | 
|---|
 | 641 |       LOG(3, "DEBUG: Nodes per level are " << nodes_per_level);
 | 
|---|
 | 642 | 
 | 
|---|
 | 643 |       /// 6. construct all possible induced connected subgraphs with this map (see fragmentation)
 | 
|---|
 | 644 |       nodes_t nodes;
 | 
|---|
 | 645 |       // rootindex is always contained
 | 
|---|
 | 646 |       nodes.insert( rootindex );
 | 
|---|
 | 647 |       level_t level = 0;
 | 
|---|
 | 648 | 
 | 
|---|
 | 649 |       generateAllInducedConnectedSubgraphs(
 | 
|---|
 | 650 |           N-1, level, nodes, set_of_nodes, nodes_per_level, fragmentgraph, distances, index_map);
 | 
|---|
 | 651 |       LOG(2, "DEBUG: We have found " << set_of_nodes.size() << " unique induced, connected subgraphs.");
 | 
|---|
 | 652 |     }
 | 
|---|
 | 653 | 
 | 
|---|
 | 654 |     /// 8. go through each induced, connected subgraph
 | 
|---|
 | 655 |     for (set_of_nodes_t::const_iterator nodesiter = set_of_nodes.begin();
 | 
|---|
 | 656 |         nodesiter != set_of_nodes.end(); ++nodesiter) {
 | 
|---|
 | 657 |       /// 9. convert its nodes into a HomologyGraph using the subgraph and arguments_t (types for index)
 | 
|---|
 | 658 |       const nodes_t &nodes = *nodesiter;
 | 
|---|
 | 659 |       const HomologyGraph nodes_graph =
 | 
|---|
 | 660 |           createHomologyGraphFromNodes(nodes, type_index_lookup, fragmentgraph, index_map);
 | 
|---|
 | 661 | 
 | 
|---|
 | 662 |       /// 10. compare each converted HomologyGraph with _bindingmodel: Accept or Reject
 | 
|---|
 | 663 |       if (nodes_graph == _bindingmodel) {
 | 
|---|
 | 664 |         LOG(2, "ACCEPT: " << nodes_graph << " is identical to " << _bindingmodel);
 | 
|---|
 | 665 |         /// 11. for each accepted keyset, pick _all_ symmetric distances from arguments_t
 | 
|---|
 | 666 |         FunctionModel::arguments_t argumentbunch;
 | 
|---|
 | 667 |         argumentbunch.reserve(args.size());
 | 
|---|
 | 668 |         for(FunctionModel::arguments_t::const_iterator iter = args.begin();
 | 
|---|
 | 669 |             iter != args.end(); ++iter) {
 | 
|---|
 | 670 |           if ((nodes.count(iter->indices.first) != 0) && (nodes.count(iter->indices.second) != 0)) {
 | 
|---|
 | 671 |             argumentbunch.push_back(*iter);
 | 
|---|
| [e1fe7e] | 672 |           }
 | 
|---|
| [2124aa] | 673 |         }
 | 
|---|
 | 674 |         const size_t num_symmetric_distances = nodes.size()*(nodes.size()-1)/2;
 | 
|---|
 | 675 |         ASSERT( argumentbunch.size() == num_symmetric_distances,
 | 
|---|
 | 676 |             "Extractors::reorderArgumentsByParticleTypes() - only "+toString(argumentbunch.size())+
 | 
|---|
 | 677 |             " found instead of "+toString(num_symmetric_distances));
 | 
|---|
 | 678 |         LOG(3, "DEBUG: Final bunch of arguments is " << argumentbunch << ".");
 | 
|---|
 | 679 | 
 | 
|---|
 | 680 |         /**
 | 
|---|
 | 681 |          * We still need to bring the arguments in the correct order for the potential.
 | 
|---|
 | 682 |          *
 | 
|---|
 | 683 |          * The potential gives the desired order via the nodes in the HomologyGraph! Their
 | 
|---|
 | 684 |          * sequence matches with the user-defined particle types and because of the properties
 | 
|---|
 | 685 |          * of the homology graph (FragmentNode stores type and number of edges) it also
 | 
|---|
 | 686 |          * matches with the desired bonding graph.
 | 
|---|
 | 687 |          */
 | 
|---|
 | 688 | 
 | 
|---|
 | 689 |         /// So, we need to store for each node its type and the number of connected
 | 
|---|
 | 690 |         /// edges, which we extract from the argumentbunch.
 | 
|---|
 | 691 |         node_FragmentNode_map_t node_FragmentNode_map = fillNodeFragmentMap(argumentbunch);
 | 
|---|
 | 692 | 
 | 
|---|
 | 693 |         /// Then, we step through the nodes of the bindingmodel
 | 
|---|
 | 694 |         const HomologyGraph::nodes_t bindingmodel_nodes = _bindingmodel.getNodes();
 | 
|---|
 | 695 |         argindex_to_nodeindex_t argindex_to_nodeindex;
 | 
|---|
 | 696 |         size_t nodeindex = 0;
 | 
|---|
 | 697 |         for (HomologyGraph::nodes_t::const_iterator nodeiter = bindingmodel_nodes.begin();
 | 
|---|
 | 698 |             nodeiter != bindingmodel_nodes.end(); ++nodeiter) {
 | 
|---|
 | 699 |           const FragmentNode &node = nodeiter->first;
 | 
|---|
 | 700 |           LOG(3, "DEBUG: Binding model's node #" << node << ".");
 | 
|---|
 | 701 |           // might have to pick several
 | 
|---|
 | 702 |           for (size_t pick_no = 0; pick_no < nodeiter->second; ++pick_no) {
 | 
|---|
 | 703 |             /// ... and pick for each the first (and unique) from these stored nodes.
 | 
|---|
 | 704 |             node_FragmentNode_map_t::iterator mapiter = node_FragmentNode_map.begin();
 | 
|---|
 | 705 |             for (;mapiter != node_FragmentNode_map.end(); ++mapiter) {
 | 
|---|
 | 706 |               if ((mapiter->second.first == node.getAtomicNumber())
 | 
|---|
 | 707 |                   && (mapiter->second.second == node.getConnectedEdges())) {
 | 
|---|
 | 708 |                 LOG(3, "DEBUG: #" << mapiter->first << " with type " << mapiter->second.first
 | 
|---|
 | 709 |                     << " and " << mapiter->second.second << " connected edges matches as choice #"
 | 
|---|
 | 710 |                     << pick_no << ".");
 | 
|---|
| [e1fe7e] | 711 |                 break;
 | 
|---|
 | 712 |               }
 | 
|---|
| [51e0e3] | 713 |             }
 | 
|---|
| [2124aa] | 714 |             ASSERT( mapiter != node_FragmentNode_map.end(),
 | 
|---|
 | 715 |                 "Extractors::reorderArgumentsByParticleTypes() - could not find a suitable node for #"+
 | 
|---|
 | 716 |                 toString(mapiter->first)+" with type "+toString(mapiter->second.first)+" and "+
 | 
|---|
 | 717 |                 toString(mapiter->second.second)+" connected edges");
 | 
|---|
 | 718 |             std::pair<argindex_to_nodeindex_t::iterator, bool> inserter =
 | 
|---|
 | 719 |                 argindex_to_nodeindex.insert( std::make_pair(mapiter->first, nodeindex++) );
 | 
|---|
 | 720 |             ASSERT( inserter.second,
 | 
|---|
 | 721 |                 "Extractors::reorderArgumentsByParticleTypes() - node #"+toString(mapiter->first)+
 | 
|---|
 | 722 |                 " is already present?");
 | 
|---|
 | 723 |             // remove to ensure uniqueness of choice
 | 
|---|
 | 724 |             node_FragmentNode_map.erase(mapiter);
 | 
|---|
| [51e0e3] | 725 |           }
 | 
|---|
 | 726 |         }
 | 
|---|
| [2124aa] | 727 |         /// This gives then the desired mapping from indices in the arguments to
 | 
|---|
 | 728 |         /// the index in the order of the HomologyGraph's nodes
 | 
|---|
 | 729 | 
 | 
|---|
 | 730 |         /// Finally, we only need to bring the arguments in the typical order:
 | 
|---|
 | 731 |         /// 01 02 03 04 ... 0n, 12 13 14 ... 1n, 23 24 25 ... 2n, ...,  (n-1)n
 | 
|---|
 | 732 |         const size_t num_args = argindex_to_nodeindex.size();
 | 
|---|
 | 733 |         argument_placement_map_t argument_placement_map = fillArgumentsPlacementMap(num_args);
 | 
|---|
 | 734 |         LOG(4, "DEBUG: Placement map is " << argument_placement_map);
 | 
|---|
 | 735 |         ASSERT( argument_placement_map.size() == argumentbunch.size(),
 | 
|---|
 | 736 |             "Extractors::reorderArgumentsByParticleTypes() - placement map has size "+
 | 
|---|
 | 737 |             toString(argument_placement_map.size())+" and we expected "+toString(argumentbunch.size()));
 | 
|---|
 | 738 | 
 | 
|---|
 | 739 |         // and finally resort the arguments with the known placement map
 | 
|---|
 | 740 |         FunctionModel::arguments_t sortedargs(argumentbunch);
 | 
|---|
 | 741 |         for (FunctionModel::arguments_t::const_iterator argiter = argumentbunch.begin();
 | 
|---|
 | 742 |             argiter != argumentbunch.end(); ++argiter) {
 | 
|---|
 | 743 |           const argument_t &arg = *argiter;
 | 
|---|
 | 744 |           const argument_placement_map_t::const_iterator indexiter =
 | 
|---|
 | 745 |               argument_placement_map.find( translateIndices(argindex_to_nodeindex, arg.indices) );
 | 
|---|
 | 746 |           ASSERT( indexiter != argument_placement_map.end(),
 | 
|---|
 | 747 |               "Extractors::reorderArgumentsByParticleTypes() - could not find place for edge "+
 | 
|---|
 | 748 |               toString(arg.indices));
 | 
|---|
 | 749 |           sortedargs[indexiter->second] = arg;
 | 
|---|
 | 750 |           LOG(3, "DEBUG: Placed argument " << arg << " at #" << indexiter->second
 | 
|---|
 | 751 |               << " as translated indices are " << translateIndices(argindex_to_nodeindex, arg.indices));
 | 
|---|
| [e1fe7e] | 752 |         }
 | 
|---|
| [2124aa] | 753 |         LOG(2, "DEBUG: Sorted arguments are " << sortedargs << ".");
 | 
|---|
 | 754 | 
 | 
|---|
 | 755 |         returnargs.push_back(sortedargs);
 | 
|---|
 | 756 |       } else {
 | 
|---|
 | 757 |         LOG(2, "REJECT: " << nodes_graph << " is not identical to " << _bindingmodel);
 | 
|---|
| [51e0e3] | 758 |       }
 | 
|---|
 | 759 |     }
 | 
|---|
 | 760 |   }
 | 
|---|
 | 761 | 
 | 
|---|
 | 762 |   return returnargs;
 | 
|---|
 | 763 | }
 | 
|---|
 | 764 | 
 | 
|---|
| [e1fe7e] | 765 | FunctionModel::list_of_arguments_t Extractors::filterArgumentsByParticleTypes(
 | 
|---|
| [51e0e3] | 766 |     const FunctionModel::arguments_t &args,
 | 
|---|
| [e60558] | 767 |     const HomologyGraph &_graph,
 | 
|---|
| [67044a] | 768 |     const ParticleTypes_t &_types,
 | 
|---|
 | 769 |     const HomologyGraph &_bindingmodel
 | 
|---|
| [51e0e3] | 770 |     )
 | 
|---|
| [df350c] | 771 | {
 | 
|---|
 | 772 |   typedef std::list< argument_t > ListArguments_t;
 | 
|---|
 | 773 |   ListArguments_t availableList(args.begin(), args.end());
 | 
|---|
| [31a2be] | 774 |   LOG(2, "DEBUG: Initial list of args is " << args << ".");
 | 
|---|
 | 775 | 
 | 
|---|
| [df350c] | 776 | 
 | 
|---|
 | 777 |   // TODO: fill a lookup map such that we don't have O(M^3) scaling, if M is number
 | 
|---|
 | 778 |   // of types (and we always must have M(M-1)/2 args) but O(M^2 log(M)). However, as
 | 
|---|
 | 779 |   // M is very small (<=3), this is not necessary fruitful now.
 | 
|---|
 | 780 | //  typedef ParticleTypes_t firsttype;
 | 
|---|
 | 781 | //  typedef ParticleTypes_t secondtype;
 | 
|---|
 | 782 | //  typedef std::map< firsttype, std::map< secondtype, boost::ref(args) > > ArgsLookup_t;
 | 
|---|
 | 783 | //  ArgsLookup_t ArgsLookup;
 | 
|---|
 | 784 | 
 | 
|---|
 | 785 |   // basically, we have two choose any two pairs out of types but only those
 | 
|---|
| [51e0e3] | 786 |   // where the first is less than the latter. Hence, we start the second
 | 
|---|
| [df350c] | 787 |   // iterator at the current position of the first one and skip the equal case.
 | 
|---|
| [e1fe7e] | 788 |   FunctionModel::arguments_t allargs;
 | 
|---|
 | 789 |   allargs.reserve(args.size());
 | 
|---|
| [df350c] | 790 |   for (ParticleTypes_t::const_iterator firstiter = _types.begin();
 | 
|---|
 | 791 |       firstiter != _types.end();
 | 
|---|
 | 792 |       ++firstiter) {
 | 
|---|
 | 793 |     for (ParticleTypes_t::const_iterator seconditer = firstiter;
 | 
|---|
 | 794 |         seconditer != _types.end();
 | 
|---|
 | 795 |         ++seconditer) {
 | 
|---|
 | 796 |       if (seconditer == firstiter)
 | 
|---|
 | 797 |         continue;
 | 
|---|
| [e1fe7e] | 798 |       LOG(3, "DEBUG: Looking for (" << *firstiter << "," << *seconditer << ") in all args.");
 | 
|---|
| [df350c] | 799 | 
 | 
|---|
 | 800 |       // search the right one in _args (we might allow switching places of
 | 
|---|
 | 801 |       // firstiter and seconditer, as distance is symmetric).
 | 
|---|
 | 802 |       ListArguments_t::iterator iter = availableList.begin();
 | 
|---|
| [31a2be] | 803 |       while (iter != availableList.end()) {
 | 
|---|
| [e1fe7e] | 804 |         LOG(4, "DEBUG: Current args is " << *iter << ".");
 | 
|---|
| [df350c] | 805 |         if ((iter->types.first == *firstiter)
 | 
|---|
 | 806 |               && (iter->types.second == *seconditer)) {
 | 
|---|
| [e1fe7e] | 807 |           allargs.push_back( *iter );
 | 
|---|
| [31a2be] | 808 |           iter = availableList.erase(iter);
 | 
|---|
 | 809 |           LOG(4, "DEBUG: Accepted argument.");
 | 
|---|
 | 810 |         } else if ((iter->types.first == *seconditer)
 | 
|---|
| [df350c] | 811 |               && (iter->types.second == *firstiter)) {
 | 
|---|
| [e1fe7e] | 812 |           allargs.push_back( *iter );
 | 
|---|
| [51e0e3] | 813 |           iter = availableList.erase(iter);
 | 
|---|
 | 814 |           LOG(4, "DEBUG: Accepted (flipped) argument.");
 | 
|---|
 | 815 |         } else {
 | 
|---|
 | 816 |           ++iter;
 | 
|---|
 | 817 |           LOG(4, "DEBUG: Rejected argument.");
 | 
|---|
| [df350c] | 818 |         }
 | 
|---|
 | 819 |       }
 | 
|---|
 | 820 |     }
 | 
|---|
 | 821 |   }
 | 
|---|
| [e1fe7e] | 822 |   LOG(2, "DEBUG: Final list of args is " << allargs << ".");
 | 
|---|
 | 823 | 
 | 
|---|
 | 824 |   // first, we bring together tuples of distances that belong together
 | 
|---|
 | 825 |   FunctionModel::list_of_arguments_t singlelist_allargs;
 | 
|---|
 | 826 |   singlelist_allargs.push_back(allargs);
 | 
|---|
| [2124aa] | 827 |   FunctionModel::list_of_arguments_t returnargs =
 | 
|---|
| [e60558] | 828 |       reorderArgumentsByParticleTypes(singlelist_allargs, _graph, _types, _bindingmodel);
 | 
|---|
| [2124aa] | 829 | //  ASSERT( sortedargs.size() == (size_t)1,
 | 
|---|
 | 830 | //      "Extractors::filterArgumentsByParticleTypes() - reordering did not generate a single list.");
 | 
|---|
 | 831 | //  // then we split up the tuples of arguments and place each into single list
 | 
|---|
 | 832 | //  FunctionModel::list_of_arguments_t returnargs;
 | 
|---|
 | 833 | //  FunctionModel::arguments_t::const_iterator argiter = sortedargs.begin()->begin();
 | 
|---|
 | 834 | //  const size_t num_types = _types.size();
 | 
|---|
 | 835 | //  const size_t args_per_tuple = num_types * (num_types-1) / 2;
 | 
|---|
 | 836 | //  while (argiter != sortedargs.begin()->end()) {
 | 
|---|
 | 837 | //    FunctionModel::arguments_t currenttuple(args_per_tuple);
 | 
|---|
 | 838 | //    const FunctionModel::arguments_t::const_iterator startiter = argiter;
 | 
|---|
 | 839 | //    std::advance(argiter, args_per_tuple);
 | 
|---|
 | 840 | //#ifndef NDEBUG
 | 
|---|
 | 841 | //    FunctionModel::arguments_t::const_iterator endoutiter =
 | 
|---|
 | 842 | //#endif
 | 
|---|
 | 843 | //        std::copy(startiter, argiter, currenttuple.begin());
 | 
|---|
 | 844 | //    ASSERT( endoutiter == currenttuple.end(),
 | 
|---|
 | 845 | //        "Extractors::filterArgumentsByParticleTypes() - currenttuple not initialized to right size.");
 | 
|---|
 | 846 | //    returnargs.push_back(currenttuple);
 | 
|---|
 | 847 | //  }
 | 
|---|
| [e1fe7e] | 848 | 
 | 
|---|
 | 849 |   LOG(2, "DEBUG: We have generated " << returnargs.size() << " tuples of distances.");
 | 
|---|
| [df350c] | 850 | 
 | 
|---|
 | 851 |   return returnargs;
 | 
|---|
 | 852 | }
 | 
|---|
| [9897ee9] | 853 | 
 | 
|---|
 | 854 | 
 | 
|---|
 | 855 | FunctionModel::arguments_t Extractors::combineArguments(
 | 
|---|
 | 856 |     const FunctionModel::arguments_t &firstargs,
 | 
|---|
 | 857 |     const FunctionModel::arguments_t &secondargs)
 | 
|---|
 | 858 | {
 | 
|---|
| [cf4905] | 859 |   FunctionModel::arguments_t args = concatenateArguments(firstargs, secondargs);
 | 
|---|
| [64bdfd] | 860 |   std::sort(args.begin(), args.end(),
 | 
|---|
 | 861 |       boost::bind(&argument_t::operator<, _1, _2));
 | 
|---|
 | 862 |   FunctionModel::arguments_t::iterator iter =
 | 
|---|
 | 863 |       std::unique(args.begin(), args.end(), 
 | 
|---|
 | 864 |           boost::bind(&argument_t::operator==, _1, _2));
 | 
|---|
 | 865 |   args.erase(iter, args.end());
 | 
|---|
| [9897ee9] | 866 |   return args;
 | 
|---|
 | 867 | }
 | 
|---|
| [64bdfd] | 868 | 
 | 
|---|
| [cf4905] | 869 | FunctionModel::arguments_t Extractors::concatenateArguments(
 | 
|---|
 | 870 |     const FunctionModel::arguments_t &firstargs,
 | 
|---|
 | 871 |     const FunctionModel::arguments_t &secondargs)
 | 
|---|
 | 872 | {
 | 
|---|
 | 873 |   FunctionModel::arguments_t args(firstargs);
 | 
|---|
 | 874 |   args.insert(args.end(), secondargs.begin(), secondargs.end());
 | 
|---|
 | 875 |   return args;
 | 
|---|
 | 876 | }
 | 
|---|
 | 877 | 
 | 
|---|
| [e1fe7e] | 878 | FunctionModel::list_of_arguments_t Extractors::concatenateListOfArguments(
 | 
|---|
 | 879 |     const FunctionModel::list_of_arguments_t &firstlistargs,
 | 
|---|
 | 880 |     const FunctionModel::list_of_arguments_t &secondlistargs)
 | 
|---|
 | 881 | {
 | 
|---|
 | 882 |   FunctionModel::list_of_arguments_t listargs(firstlistargs);
 | 
|---|
 | 883 |   listargs.insert(listargs.end(), secondlistargs.begin(), secondlistargs.end());
 | 
|---|
 | 884 |   return listargs;
 | 
|---|
 | 885 | }
 | 
|---|