BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
assignBondOrderProcessor.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_STRUCTURE_ASSIGNBONDORDERPROCESSOR_H
6 #define BALL_STRUCTURE_ASSIGNBONDORDERPROCESSOR_H
7 
8 #ifndef BALL_CONCEPT_PROCESSOR_H
9  #include <BALL/CONCEPT/processor.h>
10 #endif
11 
12 #ifndef BALL_KERNEL_ATOMCONTAINER_H
14 #endif
15 
16 #ifndef BALL_DATATYPE_HASHMAP_H
17  #include <BALL/DATATYPE/hashMap.h>
18 #endif
19 
20 #ifndef BALL_DATATYPE_HASHSET_H
21  #include <BALL/DATATYPE/hashSet.h>
22 #endif
23 
24 #ifndef BALL_KERNEL_BOND_H
25  #include <BALL/KERNEL/bond.h>
26 #endif
27 
28 #ifndef BALL_DATATYPE_OPTIONS_H
29 # include <BALL/DATATYPE/options.h>
30 #endif
31 
32 #ifndef BALL_COMMON_EXCEPTION_H
33 # include <BALL/COMMON/exception.h>
34 #endif
35 
36 #ifndef BALL_SYSTEM_TIMER_H
37 # include <BALL/SYSTEM/timer.h>
38 #endif
39 
40 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENTSTRATEGY_H
42 #endif
43 
44 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENT_H
46 #endif
47 
48 #ifndef BALL_STRUCTURE_BONDORDERS_PARTIALBONDORDERASSIGNMENT_H
50 #endif
51 
52 #include <map>
53 #include <vector>
54 
55 namespace BALL
56 {
89  : public UnaryProcessor<AtomContainer>
90  {
91  protected:
93  friend class BondOrderAssignment;
94 
95  class PQ_Entry_;
96  friend class PQ_Entry_;
97 
99 
102  friend class FPTBondOrderStrategy;
103  friend class ILPBondOrderStrategy;
105 
106  public:
107 
111  struct BALL_EXPORT Option
113  {
117  static const char* OVERWRITE_SINGLE_BOND_ORDERS;
118 
123  static const char* OVERWRITE_DOUBLE_BOND_ORDERS;
124 
129  static const char* OVERWRITE_TRIPLE_BOND_ORDERS;
130 
140  static const char* OVERWRITE_SELECTED_BONDS;
141 
146  static const char* ADD_HYDROGENS; //TODO
147 
152  static const char* COMPUTE_ALSO_CONNECTIVITY; //TODO
153 
158  static const char* CONNECTIVITY_CUTOFF; //TODO
159 
162  static const char* USE_FINE_PENALTY;
163 
166  static const char* KEKULIZE_RINGS;
167 
170  static const char* ALGORITHM;
171 
174  static const char* INIFile;
175 
178  static const char* MAX_BOND_ORDER;
179 
188  static const char* MAX_NUMBER_OF_SOLUTIONS;
189 
200  static const char* MAX_PENALTY;
201 
212 
218  static const char* BOND_LENGTH_WEIGHTING;
219 
224  static const char* APPLY_FIRST_SOLUTION;
225 
226  };
227 
230  {
231  static const bool OVERWRITE_SINGLE_BOND_ORDERS;
232  static const bool OVERWRITE_DOUBLE_BOND_ORDERS;
233  static const bool OVERWRITE_TRIPLE_BOND_ORDERS;
234  static const bool OVERWRITE_SELECTED_BONDS;
235  static const bool ADD_HYDROGENS;
236  static const bool COMPUTE_ALSO_CONNECTIVITY;
237  static const float CONNECTIVITY_CUTOFF;
238  static const bool USE_FINE_PENALTY;
239  static const bool KEKULIZE_RINGS;
240  static const String ALGORITHM;
241  static const String INIFile;
242  static const int MAX_BOND_ORDER;
243  static const int MAX_NUMBER_OF_SOLUTIONS;
244  static const int MAX_PENALTY;
246  static const float BOND_LENGTH_WEIGHTING;
247  static const bool APPLY_FIRST_SOLUTION;
248  };
249 
251  {
255  static const String A_STAR;
256 
267  static const String ILP;
268 
294  static const String FPT;
295 
296  static const String K_GREEDY;
297  static const String BRANCH_AND_BOUND;
298  };
299 
301 
305 
308 
309  // constructor with parameter filename //TODO
310  //AssignBondOrderProcessor(const String& file_name) throw(Exception::FileNotFound);
311 
313  virtual ~AssignBondOrderProcessor();
315 
319 
321  virtual bool start();
322 
328  void clear();
329 
343  virtual Processor::Result operator ()(AtomContainer& ac);
344 
346  virtual bool finish();
347 
349 
353 
363  {
364  if (i >= solutions_.size())
365  {
366  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
367  return 0;
368  }
369  int num_hydrogens = 0;
370 
371  HashMap<Atom*, int>::Iterator it = solutions_[i].number_of_virtual_hydrogens.begin();
372  for (; it != solutions_[i].number_of_virtual_hydrogens.end(); it++)
373  num_hydrogens += it->second;
374  return num_hydrogens;
375  }
376 
386  Size getNumberOfComputedSolutions() {return solutions_.size();}
387 
388 
392 
395  AtomContainer const* getAtomContainer() const {return ac_;}
396 
405  const System& getSolution(Position i) throw(Exception::IndexOverflow);
406 
415  {
416  if (i >= solutions_.size())
417  {
418  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
419 
421  }
422  else
423  {
424  return getTotalCharge_(solutions_[i]);
425  }
426  }
427 
434  {
435  if (i >= solutions_.size())
436  {
437  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
438 
440  }
441  else
442  return getTotalPenalty_(solutions_[i]);
443  }
444 
445  /* Returns the number of node expansions before solution i was found.
446  *
447  * param i index of the solution, whose number of node expansions should be returned.
448  * return int - number of node expansions before solution i was found.
449  */
451  {
452  if (i >= solutions_.size())
453  {
454  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
455 
456  return -1;
457  }
458  else
459  return getNumberOfNodeExpansions_(solutions_[i]);
460  }
461 
462  /* Returns the number of node expansions before solution i was found.
463  *
464  * param i index of the solution, whose queue size should be returned.
465  * return int - queue size when solution i was found.
466  */
468  {
469  if (i >= solutions_.size())
470  {
471  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
472 
473  return -1;
474  }
475  else
476  return getQueueSize_(solutions_[i]);
477  }
478 
491  bool apply(Position i);
492 
498  void resetBondOrders();
499 
507  bool computeNextSolution(bool apply_solution = true);
508 
511  void setDefaultOptions();
512 
515  bool hasValidOptions(){return readOptions_();}
516 
523  float evaluatePenalty(AtomContainer* ac);
525 
529  Options options;
531 
533 
534  protected:
535 
541  bool readOptions_();
542 
543 
548  bool readAtomPenalties_() throw(Exception::FileNotFound());
549 
558  bool preassignPenaltyClasses_();
559 
566  int getPenaltyClass_(Atom* atom);
567 
568 
584  bool precomputeBondLengthPenalties_();
585 
597  float computeVirtualHydrogens_(Atom* atom);
598 
601  //TODO: move to solution!
602  bool apply_(BondOrderAssignment& solution);
603 
606  void storeOriginalConfiguration_();
607 
608  /* Returns the queue's size at the moment the given solution was found.
609  *
610  * param sol solution, whose queue size should be returned.
611  * return int - queue size when the given solution was found.
612  */
613  int getQueueSize_(const BondOrderAssignment& sol){return sol.getQueueSize();}
614 
622  {
623  if (sol.valid)
624  {
625  return sol.total_charge;
626  }
627  else
628  {
629  return 0;
630  }
631  }
632 
640  {
641  return sol.coarsePenalty();
642  }
643 
644  /* Returns the number of node expansions before the given solution was found.
645  *
646  * param sol solution, whose number of node expansions should be returned.
647  * return int - number of node expansions before solution i was found.
648  */
650 
652  bool valid_;
653 
656 
657  // Map for storing the bonds fixed orders
658  // if a bond is free, the map returns 0
659  std::map<Bond*, short> bond_fixed_;
660 
661  // all free bonds in the atom container
662  std::vector<Bond*> free_bonds_;
663 
664  // Map for storing the bonds associated index (all bonds)
666 
667  // Vector for mapping from variable indices onto bonds (all bonds)
668  std::vector<Bond*> index_to_bond_;
669 
670 
671 
672  // ***************** datastructures for virtual hydrogen bonds ******************
673  //
674  // NOTE: a single virtual bond represents ALL possible hydrogen
675  // bonds for a given atom
676  //
677  // the atoms with upto n possible additional hydrogens
679  //
680  // the max number of virtual hydrogens per virtual bond index
682  //
683  // the number of virtual bonds
685  //
686  // the virtual bond index assigned to this atom!
689  //
690  //
691  // a virtual dummy bond
693 
694  // ******************* general datastructures *********************
695 
696  // the number of bonds given (free + fixed!)
698 
699  // num of free bonds without virtual bonds!
701 
702  // store for all atom-indices the atoms fixed valences
703  std::vector<Position> fixed_val_;
704 
705  // storing the solutions
706  vector<BondOrderAssignment> solutions_;
707 
708  // the original conformation before we computed anything
709  // this is a vector because we can have multiple molecules...
710  vector<BondOrderAssignment> starting_configuration_;
711 
712  // the inverse of the atom type penalty normalization factor
714 
715  // the inverse of the bond length penalty normalization factor
717 
718  // denotes the index of the last applied solution
719  // -1 if there was no valid solution applied
721 
722  // the AtomContainer, the processor is operating on
724 
725  // max bond order to consider
727 
728  // balance parameter between atom type and bond length penalty
729  float alpha_;
730 
731  // the max number of solutions to compute
733 
734  // the max penalty score
736 
737  // flag to indicate, whether also non-optimal solutions should be computed
739 
740  // flag for adding missing hydrogens
742 
743  // flag for computing also the bond connectivity
745 
746  // flag for using fine penalties derived from 3d information
748 
749  // //////// general stuff /////////
750 
751 
752  // The penalty administration datastructures.
753  // filled by readAtomPenalties_
754  // organized in imaginarey blocks of length
755  // block_to_length_[i], starting from
756  // block_to_start_idx_[i] associating
757  // block_to_start_valence_[i] to the start_idx
758  vector<int> penalties_;
759  vector<Position> block_to_start_idx_;
760  vector<Size> block_to_length_;
762  // stores the defining element and the SMART-string of each block
763  vector<std::pair<String, String> > block_definition_;
764 
765 
766  // Stores which atom belongs to which penalty block.
767  // The first vector element of each atom block denotes the penalty block
768  // assigned to the atom without any additional VIRTUAL Hydrogens,
769  // the second element with one additional Hydrogen and so on.
770  vector< vector<int> > atom_to_block_;
771 
772  // Stores the possible bond lengths penalties per order.
774 
776 
778  AssignBondOrderProcessor& operator = (const AssignBondOrderProcessor& abop);
779 
780  // The strategies this class can use
782  };
783 
784 } // namespace BALL
785 
786 
787 #endif // BALL_STRUCTURE_ASSIGNBONDORDERPROCESSOR_H
float coarsePenalty(float atom_type_penalty, float bond_length_penalty) const
Assignment of bond orders from topology information.
AtomContainer const * getAtomContainer() const
HashMap< Bond *, Index > bond_to_index_
vector< BondOrderAssignment > solutions_
float getTotalCharge_(const BondOrderAssignment &sol)
A solution to the bond order assignment problem.
std::vector< int > virtual_bond_index_to_number_of_virtual_hydrogens_
vector< BondOrderAssignment > starting_configuration_
Base::iterator Iterator
Definition: hashMap.h:97
BALL_EXPORT LogStream Log
StringHashMap< boost::shared_ptr< BondOrderAssignmentStrategy > > strategies_
T max(const T &a, const T &b)
Definition: MATHS/common.h:79
bool valid_
Processor is in a useable valid state.
LogStream & error(int n=0)
vector< std::pair< String, String > > block_definition_
HashMap< Atom *, int > atom_to_virtual_bond_index_
HashMap< Bond *, vector< float > > bond_lengths_penalties_
std::map< Bond *, short > bond_fixed_
float getTotalPenalty_(const BondOrderAssignment &sol)
int getNumberOfNodeExpansions_(const BondOrderAssignment &sol)
HashMap class based on the STL map (containing serveral convenience functions)
Definition: hashMap.h:73
bool evaluation_mode_
Processor is in an evaluation mode. Default is false.
Base class for bond order assignment algorithms.
#define BALL_EXPORT
Definition: COMMON/global.h:50
HashMap< Atom *, int > number_of_virtual_hydrogens_
A full or partial solution to the AStar-based bond order assignment problem.