BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FPTBondOrderStrategy.h
Go to the documentation of this file.
1 #ifndef BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H
2 #define BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H
3 
4 #ifndef BALL_COMMON_GLOBAL_H
5 # include <BALL/COMMON/global.h>
6 #endif
7 
8 #ifndef BALL_MATHS_COMMON_H
9 # include <BALL/MATHS/common.h>
10 #endif
11 
12 #ifndef BALL_KERNEL_ATOMCONTAINER_H
14 #endif
15 
16 #ifndef BALL_KERNEL_BOND_H
17 # include <BALL/KERNEL/bond.h>
18 #endif
19 
20 #ifndef BALL_DATATYPE_HASHMAP_H
21 # include <BALL/DATATYPE/hashMap.h>
22 #endif
23 
24 #ifndef BALL_DATATYPE_GRAPH_H
26 #endif
27 
28 #ifndef BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H
30 #endif
31 
32 #ifndef BALL_DATATYPE_GRAPH_TREEWIDTH_H
34 #endif
35 
36 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENTSTRATEGY_H
38 #endif
39 
40 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENT_H
42 #endif
43 
44 #include <algorithm>
45 #include <map>
46 #include <set>
47 #include <vector>
48 #include <stack>
49 #include <iterator>
50 #include <queue>
51 
52 #include <boost/shared_ptr.hpp>
53 #include <boost/ref.hpp>
54 
55 namespace BALL
56 {
57  //TODO: documentation is obsolete!
123  {
124  public:
127 
130 
132 
136  typedef float Penalty;
137 
142  typedef int Valence;
143 
148  typedef int BondOrder;
149 
152  static const Penalty infinite_penalty;
153  static const Valence max_valence;
154 
156  struct BALL_EXPORT Option
158  {
164  };
165 
168  {
174  };
175 
177 
179 
184  virtual ~FPTBondOrderStrategy();
185 
191  virtual void clear();
192 
198  virtual void init();
199 
200  virtual bool readOptions(const Options& options);
201  virtual void setDefaultOptions();
202 
208  virtual boost::shared_ptr<BondOrderAssignment> computeNextSolution();
209 
210  protected:
220  class DPConfig_
221  {
222  public:
226  DPConfig_();
227 
233 
237  DPConfig_(std::vector<Valence> const& v, std::vector<BondOrder> const& bo);
238 
242  DPConfig_& operator=(DPConfig_ const& copy);
243 
247  template<typename ValenceIterator, typename BondIterator>
248  DPConfig_(ValenceIterator vit, ValenceIterator vend, BondIterator boit, BondIterator boend)
249  : consumed_valences(vit, vend),
250  bond_assignments(boit, boend)
251  {
252  }
253 
257  bool operator < (DPConfig_ const& conf) const;
258 
262  bool operator > (DPConfig_ const& conf) const;
263 
267  bool operator <= (DPConfig_ const& conf) const;
268 
272  bool operator >= (DPConfig_ const& conf) const;
273 
277  bool operator == (DPConfig_ const& conf) const;
278 
289  int compare(DPConfig_ const& other) const;
290 
294  Size numberOfAtoms() const;
295 
299  Size numberOfBonds() const;
300 
305  std::vector<Valence> consumed_valences;
306 
314  std::vector<BondOrder> bond_assignments;
315 
316  };
317 
322  typedef std::pair<boost::reference_wrapper<DPConfig_>, Penalty> DPRow_;
323 
328  typedef std::pair<boost::reference_wrapper<DPConfig_ const>, Penalty> DPConstRow_;
329 
335  typedef std::pair<DPConfig_*, Penalty> DPPointerRow_;
336 
341  typedef std::map<DPConfig_, Penalty> DPMap_;
342 
350  class DPTable_
351  {
352  protected:
358 
359  public:
363  DPTable_();
364 
368  DPTable_(DPTable_ const& table);
369 
373  typedef DPMap_::iterator iterator;
374 
378  typedef DPMap_::const_iterator const_iterator;
379 
383  Penalty operator[](DPConfig_ const& config) const;
384 
389  bool insert(DPConfig_ const& config, Penalty penalty);
390 
394  Size size() const;
395 
399  Penalty bestPenalty() const;
400 
406  DPConstRow_ bestEntry() const;
407 
411  iterator begin();
412 
416  iterator end();
417 
421  const_iterator begin() const;
422 
426  const_iterator end() const;
427  };
428 
435  {
436  public:
441 
446 
450  AdditionalBagProperties_& operator=(AdditionalBagProperties_ const& copy);
451 
456 
460  std::vector<MolecularGraphTraits::EdgeType> bonds;
461 
466  };
467 
468  class DPBackTracking_;
470 
479  {
480  public:
481  friend class DPBackTracking_;
484 
492  FPTBondOrderAssignment_(FPTBondOrderStrategy& parent, boost::shared_ptr<TreeDecomposition>& ntd,
493  Penalty upper_bound = infinite_penalty);
494 
501 
507  Penalty compute();
508 
509  protected:
514 
519 
523  boost::shared_ptr<TreeDecomposition> ntd_;
524 
529  vector<AdditionalBagProperties_> properties_;
530 
539 
544 
549 
562  DPTable_* operator() (TreeDecompositionBag& bag,
563  std::vector<DPTable_*>::const_iterator begin, std::vector<DPTable_*>::const_iterator end);
564 
572  std::vector<MolecularGraphTraits::EdgeType> getBondsInBag(TreeDecompositionBag& bag);
573 
580  void computeLeafIntroduceBag(AdditionalBagProperties_& bag_properties);
581 
592  void computeIntroduceBag(TreeDecompositionBag& bag,
593  DPTable_& child, AdditionalBagProperties_& bag_properties);
594 
609  void computeForgetBag(TreeDecompositionBag& bag,
610  DPTable_& child, AdditionalBagProperties_& property);
611 
625  void computeJoinBag(TreeDecompositionBag& bag,
626  DPTable_& leftChild, DPTable_& rightChild, AdditionalBagProperties_& bag_properties);
627 
633  void computeRootBag(TreeDecompositionBag& bag, DPTable_& child, AdditionalBagProperties_& bag_properties);
634 
640  Penalty forgetInnerVertexIn(TreeDecompositionBag& bag, DPConstRow_ child_row, DPConfig_& entry,
641  std::vector<MolecularGraphTraits::EdgeType>& child_bonds, Size forgotten_index);
642 
643  };
644 
651  {
652  public:
656  ComputingData_();
657 
661  ~ComputingData_();
662 
666  vector<FPTBondOrderAssignment_*> bond_assignments;
667 
672 
676  boost::shared_ptr<TreeWidth<MolecularGraph> > tw;
677 
682  vector<Bond const *> bonds;
683  };
684 
699  {
700  public:
704  Assignment_();
705 
710  Assignment_(Size num_bonds);
711 
715  Assignment_(Assignment_ const& copy);
716 
721  Assignment_(std::vector<BondOrder> const& bonds, Penalty penalty);
722 
726  Assignment_& operator=(Assignment_ const& copy);
727 
732  BondOrder operator [](Size index) const;
733 
738  BondOrder& operator [](Size index);
739 
750  void combine(Assignment_ const& other);
751 
755  std::vector<BondOrder> const& getBondOrders() const;
756 
762  int compare(Assignment_ const& a) const;
763 
767  bool operator < (Assignment_ const& a) const;
768 
772  bool operator > (Assignment_ const& a) const;
773 
777  bool operator <= (Assignment_ const& a) const;
778 
782  bool operator >= (Assignment_ const& a) const;
783 
787  bool operator == (Assignment_ const& a) const;
788 
797  bool isValid(MolecularGraph& molecule, FPTBondOrderStrategy& parent);
798 
803 
804  protected:
808  std::vector<BondOrder> bonds_;
809 
810  };
811 
818  {
823  bool operator() (DPConfig_ const* leftp, DPConfig_ const* rightp) const;
824 
830  int compare(DPConfig_ const* leftp, DPConfig_ const* rightp) const;
831  };
832 
836  {
837  public:
839 
841  : graph_(graph)
842  { }
843 
844  bool operator() (Edge const& e1, Edge const& e2);
845 
846  protected:
848  };
849 
857  {
858  public:
863 
868 
873 
877  BackTrackingState_& operator=(BackTrackingState_ const& other);
878 
883  int compare(BackTrackingState_ const& other) const;
884 
888  bool operator < (BackTrackingState_ const&) const;
889 
893  bool operator > (BackTrackingState_ const&) const;
894 
898  bool operator <= (BackTrackingState_ const&) const;
899 
903  bool operator >= (BackTrackingState_ const&) const;
904 
908  bool operator == (BackTrackingState_ const&) const;
909 
915 
921 
927  std::stack<std::pair<DPConfig_, Size> > join_branches;
928 
934 
935 
936  };
937 
942  typedef std::pair<DPTable_::const_iterator, DPTable_::const_iterator> DPPairIt_;
943 
947  static bool compareJoinTablePairs_(DPPairIt_ const& left, DPPairIt_ const& right);
948 
952  static bool compareTablePointerEntries_(DPPointerRow_ const& left, DPPointerRow_ const& right);
953 
958  typedef std::multimap<DPConfig_ const*, Penalty, DPJoinMapComparator_> DPJoinMap_;
959 
980  {
981  public:
985 
996  DPBackTracking_(FPTBondOrderAssignment_& bond_assignment, Size max_number_of_solutions,
997  std::vector<MolecularGraphTraits::EdgeType> const& bonds, Penalty upperbound = infinite_penalty);
998 
1002  DPBackTracking_(DPBackTracking_ const& copy);
1003 
1007  ~DPBackTracking_();
1008 
1012  DPBackTracking_& operator= (DPBackTracking_ const& copy);
1013 
1019  Assignment_& getSolution();
1020 
1027  Assignment_ const& getSolution() const;
1028 
1033  bool hasMoreSolutions() const;
1034 
1039  void nextSolution();
1040 
1045  Penalty penaltyOfNextSolution() const;
1046 
1047  void clear();
1048 
1050  {
1051  bags_->push_back(node);
1052  }
1053 
1055  {
1056  }
1057 
1059  {
1060  }
1061 
1062  protected:
1063 
1068  {
1072  bool operator () (BackTrackingState_ const * left, BackTrackingState_ const * right) const;
1073  };
1074 
1081 
1086 
1091  std::multiset<BackTrackingState_*, StateComparator_> queue_;
1092 
1097 
1102  std::vector<MolecularGraphTraits::EdgeType> const* bonds_;
1103 
1107  boost::shared_ptr<std::vector<TreeDecompositionBag> > bags_;
1108 
1114 
1119 
1124 
1125  typedef vector<TreeDecompositionBag> BagVector;
1126 
1130  DPTable_& getTable(Size order);
1131 
1135  AdditionalBagProperties_& getProperties(Size order);
1136 
1142  void visitLeaf(BackTrackingState_& state);
1143 
1156  void visitJoin(BackTrackingState_& state, TreeDecompositionBag& bag,
1157  DPTable_& leftTable, DPTable_& rightTable);
1158 
1166  void visitForget(BackTrackingState_& state, TreeDecompositionBag& bag, DPTable_& table);
1167 
1175  void visitIntroduce(BackTrackingState_& state, TreeDecompositionBag& bag, DPTable_& table);
1176 
1182  Size bondIndexFor(MolecularGraphTraits::EdgeType bond) const;
1183 
1191  void remember(BackTrackingState_& state);
1192 
1197  bool isSolutionNeeded(Penalty penalty);
1198 
1207  void setStateAssignment(BackTrackingState_& state, TreeDecompositionBag& bag,
1208  DPConfig_& antecessor, MolecularGraphTraits::VertexType forgotten_vertex);
1209 
1217  void extendState(BackTrackingState_& state, DPConfig_ const& antecessor, Penalty additional_penalty);
1218 
1225  void branchState(BackTrackingState_& state, TreeDecompositionBag const& child, DPConfig_ const& antecessor);
1226 
1227  };
1228 
1241  {
1242  public:
1248  DPBackTrackingCombiner_(std::vector<FPTBondOrderAssignment_*>& bond_assignments,
1249  Size solution_number, Penalty upper_bound = infinite_penalty);
1250 
1255 
1260 
1261  void clear();
1262 
1266  DPBackTrackingCombiner_& operator = (DPBackTrackingCombiner_ const& copy);
1267 
1271  bool hasMoreSolutions() const;
1272 
1277  void nextSolution();
1278 
1282  Assignment_& getSolution();
1283 
1287  Assignment_ const& getSolution() const;
1288 
1293  Penalty penaltyOfNextSolution() const;
1294 
1299  std::vector<MolecularGraphTraits::EdgeType> sorted_edges;
1300 
1301  protected:
1305  std::vector<DPBackTracking_*> backtrackers_;
1306 
1312  std::priority_queue<Assignment_, std::vector<Assignment_>, std::greater<Assignment_> > priority_queue_;
1313 
1318  std::vector<std::vector<Assignment_> > component_solutions_;
1319 
1324 
1329 
1334 
1339 
1344  std::pair<Size, Penalty> getNextMinimumBackTracker_() const;
1345 
1351  void applyAssignment_(Size backtracker_index, Size solution_index);
1352 
1356  void initialize_();
1357 
1362  void combineEachSolution_(Size mindex);
1363 
1367  std::vector<DPBackTracking_*> deepCopyOfBacktrackers_() const;
1368 
1369  };
1370 
1371 
1373  void initPenaltyData_();
1374 
1376  Penalty getPenaltyFor_(MolecularGraphTraits::VertexType vertex, Valence valence) const;
1377 
1381  std::vector<int> const* penalties_;
1382 
1386  std::vector<Position> const * block_to_start_idx_;
1387 
1391  std::vector<Size> const * block_to_length_;
1392 
1396  std::vector<int> const * block_to_start_valence_;
1397 
1401  std::vector<std::vector<int> > const* atom_to_block_;
1402 
1407  boost::shared_ptr<ComputingData_> computing_data_;
1408 
1412  boost::shared_ptr<DPBackTrackingCombiner_> combiner_;
1413  };
1414 }
1415 #endif // BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H
boost::shared_ptr< DPBackTrackingCombiner_ > combiner_
void preorder(TreeDecompositionBag node, TreeDecomposition &)
vector< FPTBondOrderAssignment_ * > bond_assignments
Assignment of bond orders from topology information.
std::vector< std::vector< Assignment_ > > component_solutions_
DPConfig_(ValenceIterator vit, ValenceIterator vend, BondIterator boit, BondIterator boend)
TreeWidth< MolecularGraph >::TreeDecomposition TreeDecomposition
std::vector< MolecularGraphTraits::EdgeType > const * bonds_
std::stack< std::pair< DPConfig_, Size > > join_branches
TreeWidth< MolecularGraph >::TreeDecompositionBag TreeDecompositionBag
std::set< OriginalVertexType > TreeDecompositionContent
Definition: treeWidth.h:99
GRAPH::GraphTraits< MolecularGraph >::VertexType VertexType
TreeWidth< MolecularGraph >::TreeDecompositionBag TreeDecompositionBag
TreeWidth< MolecularGraph >::TreeDecomposition TreeDecomposition
std::multiset< BackTrackingState_ *, StateComparator_ > queue_
BALL_EXPORT AtomList atoms(const AtomContainer &fragment, const String &expression=String())
std::priority_queue< Assignment_, std::vector< Assignment_ >, std::greater< Assignment_ > > priority_queue_
BALL_EXPORT bool operator>(const String &s1, const String &s2)
TreeWidth< MolecularGraph >::TreeDecompositionContent TreeDecompositionContent
boost::shared_ptr< ComputingData_ > computing_data_
boost::shared_ptr< std::vector< TreeDecompositionBag > > bags_
void postorder(TreeDecompositionBag, TreeDecomposition &)
static const Valence max_valence
GRAPH::GraphTraits< MolecularGraph >::EdgeType Edge
static const Penalty infinite_penalty
GRAPH::GraphTraits< MolecularGraph >::EdgeType Edge
boost::graph_traits< Graph >::edge_descriptor EdgeType
std::pair< boost::reference_wrapper< DPConfig_ const >, Penalty > DPConstRow_
std::vector< MolecularGraphTraits::EdgeType > bonds
boost::graph_as_tree< TreeDecompositionGraph, TreeDecompositionParentMap > TreeDecomposition
Definition: treeWidth.h:112
std::multimap< DPConfig_ const *, Penalty, DPJoinMapComparator_ > DPJoinMap_
std::vector< MolecularGraphTraits::EdgeType > sorted_edges
std::pair< boost::reference_wrapper< DPConfig_ >, Penalty > DPRow_
boost::graph_traits< Graph >::vertex_descriptor VertexType
BALL_EXPORT bool operator>=(const String &s1, const String &s2)
BALL_EXPORT bool operator==(const String &s1, const String &s2)
BALL_EXPORT bool operator<=(const String &s1, const String &s2)
std::map< DPConfig_, Penalty > DPMap_
boost::shared_ptr< TreeWidth< MolecularGraph > > tw
std::vector< int > const * block_to_start_valence_
boost::graph_traits< TreeDecompositionGraph >::vertex_descriptor TreeDecompositionBag
Definition: treeWidth.h:107
std::pair< DPConfig_ *, Penalty > DPPointerRow_
BALL_EXPORT BondList bonds(const AtomContainer &fragment, bool selected_only=false)
std::vector< int > const * penalties_
std::vector< Size > const * block_to_length_
void inorder(TreeDecompositionBag, TreeDecomposition &)
std::pair< DPTable_::const_iterator, DPTable_::const_iterator > DPPairIt_
Base class for bond order assignment algorithms.
std::vector< std::vector< int > > const * atom_to_block_
#define BALL_EXPORT
Definition: COMMON/global.h:50
Index compare(const T1 &a, const T2 &b)
Definition: MATHS/common.h:314
std::vector< Position > const * block_to_start_idx_
BALL_EXPORT bool operator<(const String &s1, const String &s2)