BALL  1.4.1
 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_LIMITS_H
33 # include <BALL/COMMON/limits.h>
34 #endif
35 
36 #ifndef BALL_COMMON_EXCEPTION_H
37 # include <BALL/COMMON/exception.h>
38 #endif
39 
40 #ifndef BALL_SYSTEM_TIMER_H
41 # include <BALL/SYSTEM/timer.h>
42 #endif
43 
44 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENTSTRATEGY_H
46 #endif
47 
48 #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENT_H
50 #endif
51 
52 #ifndef BALL_STRUCTURE_BONDORDERS_PARTIALBONDORDERASSIGNMENT_H
54 #endif
55 
56 #include <map>
57 #include <vector>
58 
59 namespace BALL
60 {
93  : public UnaryProcessor<AtomContainer>
94  {
95  protected:
97  friend class BondOrderAssignment;
98 
99  class PQ_Entry_;
100  friend class PQ_Entry_;
101 
103 
106  friend class FPTBondOrderStrategy;
107  friend class ILPBondOrderStrategy;
109 
110  public:
111 
115 
117  {
121  static const char* OVERWRITE_SINGLE_BOND_ORDERS;
122 
127  static const char* OVERWRITE_DOUBLE_BOND_ORDERS;
128 
133  static const char* OVERWRITE_TRIPLE_BOND_ORDERS;
134 
144  static const char* OVERWRITE_SELECTED_BONDS;
145 
150  static const char* ADD_HYDROGENS; //TODO
151 
156  static const char* COMPUTE_ALSO_CONNECTIVITY; //TODO
157 
162  static const char* CONNECTIVITY_CUTOFF; //TODO
163 
166  static const char* USE_FINE_PENALTY;
167 
170  static const char* KEKULIZE_RINGS;
171 
174  static const char* ALGORITHM;
175 
178  static const char* INIFile;
179 
182  static const char* MAX_BOND_ORDER;
183 
192  static const char* MAX_NUMBER_OF_SOLUTIONS;
193 
204  static const char* MAX_PENALTY;
205 
216 
222  static const char* BOND_LENGTH_WEIGHTING;
223 
228  static const char* APPLY_FIRST_SOLUTION;
229 
230  };
231 
234  {
235  static const bool OVERWRITE_SINGLE_BOND_ORDERS;
236  static const bool OVERWRITE_DOUBLE_BOND_ORDERS;
237  static const bool OVERWRITE_TRIPLE_BOND_ORDERS;
238  static const bool OVERWRITE_SELECTED_BONDS;
239  static const bool ADD_HYDROGENS;
240  static const bool COMPUTE_ALSO_CONNECTIVITY;
241  static const float CONNECTIVITY_CUTOFF;
242  static const bool USE_FINE_PENALTY;
243  static const bool KEKULIZE_RINGS;
244  static const String ALGORITHM;
245  static const String INIFile;
246  static const int MAX_BOND_ORDER;
247  static const int MAX_NUMBER_OF_SOLUTIONS;
248  static const int MAX_PENALTY;
250  static const float BOND_LENGTH_WEIGHTING;
251  static const bool APPLY_FIRST_SOLUTION;
252  };
253 
255  {
259  static const String A_STAR;
260 
271  static const String ILP;
272 
298  static const String FPT;
299 
300  static const String K_GREEDY;
301  static const String BRANCH_AND_BOUND;
302  };
303 
305 
309 
312 
313  // constructor with parameter filename //TODO
314  //AssignBondOrderProcessor(const String& file_name) throw(Exception::FileNotFound);
315 
317  virtual ~AssignBondOrderProcessor();
319 
323 
325  virtual bool start();
326 
332  void clear();
333 
347  virtual Processor::Result operator ()(AtomContainer& ac);
348 
350  virtual bool finish();
351 
353 
357 
366  Size getNumberOfAddedHydrogens(Position i)
367  {
368  if (i >= solutions_.size())
369  {
370  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
371  return 0;
372  }
373  int num_hydrogens = 0;
374 
375  HashMap<Atom*, int>::Iterator it = solutions_[i].number_of_virtual_hydrogens.begin();
376  for (; it != solutions_[i].number_of_virtual_hydrogens.end(); it++)
377  num_hydrogens += it->second;
378  return num_hydrogens;
379  }
380 
390  Size getNumberOfComputedSolutions() {return solutions_.size();}
391 
392 
396 
399  AtomContainer const* getAtomContainer() const {return ac_;}
400 
409  const System& getSolution(Position i) throw(Exception::IndexOverflow);
410 
418  float getTotalCharge(Position i)
419  {
420  if (i >= solutions_.size())
421  {
422  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
423 
424  return Limits<float>::max();
425  }
426  else
427  {
428  return getTotalCharge_(solutions_[i]);
429  }
430  }
431 
437  float getTotalPenalty(Position i=0)
438  {
439  if (i >= solutions_.size())
440  {
441  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
442 
443  return Limits<float>::max();
444  }
445  else
446  return getTotalPenalty_(solutions_[i]);
447  }
448 
449  /* Returns the number of node expansions before solution i was found.
450  *
451  * param i index of the solution, whose number of node expansions should be returned.
452  * return int - number of node expansions before solution i was found.
453  */
454  int getNumberOfNodeExpansions(Position i)
455  {
456  if (i >= solutions_.size())
457  {
458  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
459 
460  return -1;
461  }
462  else
463  return getNumberOfNodeExpansions_(solutions_[i]);
464  }
465 
466  /* Returns the number of node expansions before solution i was found.
467  *
468  * param i index of the solution, whose queue size should be returned.
469  * return int - queue size when solution i was found.
470  */
471  int getQueueSize(Position i)
472  {
473  if (i >= solutions_.size())
474  {
475  Log.error() << "AssignBondOrderProcessor: No solution with index " << i << std::endl;
476 
477  return -1;
478  }
479  else
480  return getQueueSize_(solutions_[i]);
481  }
482 
495  bool apply(Position i);
496 
502  void resetBondOrders();
503 
511  bool computeNextSolution(bool apply_solution = true);
512 
515  void setDefaultOptions();
516 
519  bool hasValidOptions(){return readOptions_();}
520 
527  float evaluatePenalty(AtomContainer* ac);
529 
533 
535 
537 
538  protected:
539 
545  bool readOptions_();
546 
547 
552  bool readAtomPenalties_() throw(Exception::FileNotFound());
553 
562  bool preassignPenaltyClasses_();
563 
570  int getPenaltyClass_(Atom* atom);
571 
572 
588  bool precomputeBondLengthPenalties_();
589 
601  float computeVirtualHydrogens_(Atom* atom);
602 
605  //TODO: move to solution!
606  bool apply_(BondOrderAssignment& solution);
607 
610  void storeOriginalConfiguration_();
611 
612  /* Returns the queue's size at the moment the given solution was found.
613  *
614  * param sol solution, whose queue size should be returned.
615  * return int - queue size when the given solution was found.
616  */
617  int getQueueSize_(const BondOrderAssignment& sol){return sol.getQueueSize();}
618 
625  float getTotalCharge_(const BondOrderAssignment& sol)
626  {
627  if (sol.valid)
628  {
629  return sol.total_charge;
630  }
631  else
632  {
633  return 0;
634  }
635  }
636 
643  float getTotalPenalty_(const BondOrderAssignment& sol)
644  {
645  return sol.coarsePenalty();
646  }
647 
648  /* Returns the number of node expansions before the given solution was found.
649  *
650  * param sol solution, whose number of node expansions should be returned.
651  * return int - number of node expansions before solution i was found.
652  */
654 
656  bool valid_;
657 
660 
661  // Map for storing the bonds fixed orders
662  // if a bond is free, the map returns 0
663  std::map<Bond*, short> bond_fixed_;
664 
665  // all free bonds in the atom container
666  std::vector<Bond*> free_bonds_;
667 
668  // Map for storing the bonds associated index (all bonds)
670 
671  // Vector for mapping from variable indices onto bonds (all bonds)
672  std::vector<Bond*> index_to_bond_;
673 
674 
675 
676  // ***************** datastructures for virtual hydrogen bonds ******************
677  //
678  // NOTE: a single virtual bond represents ALL possible hydrogen
679  // bonds for a given atom
680  //
681  // the atoms with upto n possible additional hydrogens
683  //
684  // the max number of virtual hydrogens per virtual bond index
686  //
687  // the number of virtual bonds
689  //
690  // the virtual bond index assigned to this atom!
693  //
694  //
695  // a virtual dummy bond
697 
698  // ******************* general datastructures *********************
699 
700  // the number of bonds given (free + fixed!)
702 
703  // num of free bonds without virtual bonds!
705 
706  // store for all atom-indices the atoms fixed valences
707  std::vector<Position> fixed_val_;
708 
709  // storing the solutions
710  vector<BondOrderAssignment> solutions_;
711 
712  // the original conformation before we computed anything
713  // this is a vector because we can have multiple molecules...
714  vector<BondOrderAssignment> starting_configuration_;
715 
716  // the inverse of the atom type penalty normalization factor
718 
719  // the inverse of the bond length penalty normalization factor
721 
722  // denotes the index of the last applied solution
723  // -1 if there was no valid solution applied
725 
726  // the AtomContainer, the processor is operating on
728 
729  // max bond order to consider
731 
732  // balance parameter between atom type and bond length penalty
733  float alpha_;
734 
735  // the max number of solutions to compute
737 
738  // the max penalty score
740 
741  // flag to indicate, whether also non-optimal solutions should be computed
743 
744  // flag for adding missing hydrogens
746 
747  // flag for computing also the bond connectivity
749 
750  // flag for using fine penalties derived from 3d information
752 
753  // //////// general stuff /////////
754 
755 
756  // The penalty administration datastructures.
757  // filled by readAtomPenalties_
758  // organized in imaginarey blocks of length
759  // block_to_length_[i], starting from
760  // block_to_start_idx_[i] associating
761  // block_to_start_valence_[i] to the start_idx
762  vector<int> penalties_;
763  vector<Position> block_to_start_idx_;
764  vector<Size> block_to_length_;
766  // stores the defining element and the SMART-string of each block
767  vector<std::pair<String, String> > block_definition_;
768 
769 
770  // Stores which atom belongs to which penalty block.
771  // The first vector element of each atom block denotes the penalty block
772  // assigned to the atom without any additional VIRTUAL Hydrogens,
773  // the second element with one additional Hydrogen and so on.
774  vector< vector<int> > atom_to_block_;
775 
776  // Stores the possible bond lengths penalties per order.
778 
780 
782  AssignBondOrderProcessor& operator = (const AssignBondOrderProcessor& abop);
783 
784  // The strategies this class can use
786  };
787 
788 } // namespace BALL
789 
790 
791 #endif // BALL_STRUCTURE_ASSIGNBONDORDERPROCESSOR_H