Open Babel  3.0
conformersearch.h
Go to the documentation of this file.
1 /**********************************************************************
2 conformersearch.h - Conformer searching using genetic algorithm.
3 
4 Copyright (C) 2010 Tim Vandermeersch
5 Some portions Copyright (C) 2012 Materials Design, Inc.
6 Some portions Copyright (C) 2016 Torsten Sachse
7 
8 This file is part of the Open Babel project.
9 For more information, see <http://openbabel.org/>
10 
11 This program 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 version 2 of the License.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 ***********************************************************************/
20 
21 #ifndef OB_CONFORMERSEARCH_H
22 #define OB_CONFORMERSEARCH_H
23 
24 #include <openbabel/mol.h>
25 #include <openbabel/rotor.h>
26 #include <openbabel/rotamer.h>
27 
28 namespace OpenBabel {
29 
30  typedef std::vector<int> RotorKey;
31  typedef std::vector<RotorKey> RotorKeys;
32  typedef std::map<std::vector<int>,double> mapRotorEnergy;
33 
36 
38  //
39  // OBConformerFilter(s)
40  //
42 
55  class OBAPI OBConformerFilter
56  {
57  public:
64  virtual bool IsGood(const OBMol &mol, const RotorKey &key, double *coords) = 0;
65  virtual ~OBConformerFilter() = 0;
66  };
67 
81  {
82  public:
86  OBConformerFilters(const std::vector<OBConformerFilter*> &filters) : m_filters(filters)
87  {
88  }
93  bool IsGood(const OBMol &mol, const RotorKey &key, double *coords)
94  {
95  for (unsigned int i = 0; i < m_filters.size(); ++i)
96  if (!m_filters[i]->IsGood(mol, key, coords))
97  return false;
98  return true;
99  }
100  protected:
101  std::vector<OBConformerFilter*> m_filters;
102  };
103 
114  {
115  public:
117  OBStericConformerFilter (double cutoff, double vdw_factor = 0.5, bool check_hydrogens = true);
118  bool IsGood(const OBMol &mol, const RotorKey &key, double *coords);
119  private:
120  double m_cutoff;
121  double m_vdw_factor;
122  bool m_check_hydrogens;
123  };
124 
126  //
127  // OBConformerScore(s)
128  //
130 
144  class OBAPI OBConformerScore
145  {
146  public:
150  enum Preferred { HighScore, LowScore };
154  virtual Preferred GetPreferred() = 0;
158  enum Convergence { Highest, Lowest, Sum, Average };
162  virtual Convergence GetConvergence() = 0;
166  virtual double Score(OBMol &mol, unsigned int index, const RotorKeys &keys,
167  const std::vector<double*> &conformers) = 0;
168  virtual ~OBConformerScore() = 0;
169  };
170 
180  {
181  public:
182  Preferred GetPreferred() { return HighScore; }
183  Convergence GetConvergence() { return Average; }
184  double Score(OBMol &mol, unsigned int index, const RotorKeys &keys,
185  const std::vector<double*> &conformers);
186  };
187 
194  {
195  public:
197  energy_map.clear ();
198  energy_ncompute = 0;
199  energy_nrequest = 0;
200  }
201  long unsigned int GetNbEnergyCompute () {return energy_ncompute;}
202  long unsigned int GetNbEnergyRequest () {return energy_nrequest;}
203  Preferred GetPreferred() { return LowScore; }
204  Convergence GetConvergence() { return Lowest; }
205  double Score(OBMol &mol, unsigned int index, const RotorKeys &keys,
206  const std::vector<double*> &conformers);
207  private:
208  mapRotorEnergy energy_map;
209  long unsigned int energy_ncompute;
210  long unsigned int energy_nrequest;
211  };
212 
219  {
220  public:
222  energy_map.clear ();
223  energy_ncompute = 0;
224  energy_nrequest = 0;
225  }
226  long unsigned int GetNbEnergyCompute () {return energy_ncompute;}
227  long unsigned int GetNbEnergyRequest () {return energy_nrequest;}
228  Preferred GetPreferred() { return LowScore; }
229  Convergence GetConvergence() { return Lowest; }
230  double Score(OBMol &mol, unsigned int index, const RotorKeys &keys,
231  const std::vector<double*> &conformers);
232  private:
233  mapRotorEnergy energy_map;
234  long unsigned int energy_ncompute;
235  long unsigned int energy_nrequest;
236  };
237 
248  {
249  public:
250  Preferred GetPreferred() { return HighScore; }
251  Convergence GetConvergence() { return Average; }
252  double Score(OBMol &mol, unsigned int index, const RotorKeys &keys,
253  const std::vector<double*> &conformers);
254  };
255 
257  //
258  // OBConformerSearch
259  //
261 
268  class OBAPI OBConformerSearch
269  {
270  public:
286  bool Setup(const OBMol &mol, int numConformers = 30, int numChildren = 5,
287  int mutability = 5, int convergence = 25);
291  void SetNumConformers(int numConformers) { m_numConformers = numConformers; }
295  void SetNumChildren(int numChildren) { m_numChildren = numChildren; }
299  void SetMutability(int mutability) { m_mutability = mutability; }
304  void SetConvergence(int convergence) { m_convergence = convergence; }
308  void SetFixedBonds(const OBBitVec &fixedBonds) { m_fixedBonds = fixedBonds; }
309 
310 
320  {
321  delete m_filter;
322  m_filter = filter;
323  }
334  {
335  delete m_score;
336  m_score = score;
337  }
338 
342  void PrintRotors(bool printrotors) { m_printrotors = printrotors; }
343 
347  void Search();
348 
349  const RotorKeys& GetRotorKeys() const
350  {
351  return m_rotorKeys;
352  }
353 
354  void GetConformers(OBMol &mol);
355 
356  /* @brief Set an output stream for logging. If NULL pointer is provided, logging is disabled. */
357  void SetLogStream (std::ostream *sptr) {m_logstream = sptr;}
358 
359  /*************************************************/
360  /* Methods related to fitness sharing parameters */
361 
362  /* @brief Set the use of fitness sharing ON (default) or OFF*/
363  void SetSharing (bool value = true) {use_sharing = value;}
364 
365  /* @brief Get the targeted number of niches, for dynamic niche sharing */
366  int GetNbNiches () {return nb_niches;}
367 
368  /* @brief Set the targeted number of niches, for dynamic niche sharing */
369  void SetNbNiches (int value) {nb_niches = value;}
370 
371  /* @brief Get niches radius, for dynamic niche sharing.*/
372  double GetNicheRadius () {return niche_radius;}
373 
374  /* @brief Set niches radius, for dynamic niche sharing.*/
375  void SetNicheRadius (double value) {niche_radius = value;}
376 
377  /* @brief Get the alpha sharing parameter */
378  double GetAlphaSharing () {return alpha_share;}
379 
380  /* @brief Set the alpha sharing parameter */
381  void SetAlphaSharing (double value) {alpha_share = value;}
382 
383  /* @brief Get the sigma sharing parameter */
384  double GetSigmaSharing () {return sigma_share;}
385 
386  /* @brief Set the sigma sharing parameter */
387  void SetSigmaSharing (double value) {sigma_share = value;}
388 
389  /* @brief Get the (uniform) crossover probability */
390  double GetCrossoverProbability () {return p_crossover;}
391 
392  /* @brief Set the (uniform) crossover probability */
393  void SetCrossoverProbability (double value) {p_crossover = value;}
394 
395  /* @brief Get the niche mating probability, for dynamic niche sharing */
396  double GetNicheMating () {return niche_mating;}
397 
398  /* @brief Set the (uniform) crossover probability */
399  void SetNicheMating (double value) {niche_mating = value;}
400 
401  /* @brief Set the local optimization rate */
402  void SetLocalOptRate (int value) {local_opt_rate = value;}
403 
404  /* @brief Get the local optimization rate*/
405  int SetLocalOptRate() {return local_opt_rate;}
406 
407  private:
411  void NextGeneration();
415  double MakeSelection();
419  bool IsUniqueKey(const RotorKeys &keys, const RotorKey &key) const;
423  bool IsGood(const RotorKey &key);
424 
426  int key_distance (const RotorKey &key1, const RotorKey &key2);
428  int local_opt ();
430  int reproduce (RotorKey &key1, RotorKey &key2);
432  int score_population ();
434  int share_fitness ();
436  double sharing_generation ();
437 
438  unsigned int m_numConformers;
439  int m_numChildren;
440  int m_mutability;
441  int m_convergence;
442 
443  std::vector<double> vscores;
444  std::vector<double> vshared_fitnes;
445  std::vector<std::vector <int> > dynamic_niches;
446  std::vector<int> niche_map;
447 
448  void *d; // Opaque pointer - currently for storing OBRandom* which may be removed in future
449  bool use_sharing;
450  double alpha_share;
451  double sigma_share;
452  int nb_niches;
453  double niche_radius;
454  double p_crossover;
455  double niche_mating;
456  int local_opt_rate;
457  OBBitVec m_fixedBonds;
458  OBMol m_mol;
459  OBRotorList m_rotorList;
460  RotorKeys m_rotorKeys;
461  bool m_printrotors;
462 
463  OBConformerFilter *m_filter;
464  OBConformerScore *m_score;
465 
466  std::ostream *m_logstream;
467  };
468 
575 };
577 
578 #endif
579 
void SetNbNiches(int value)
Definition: conformersearch.h:369
Convergence GetConvergence()
Definition: conformersearch.h:204
An RMSD conformer scoring class, after a short minimization.
Definition: conformersearch.h:247
void SetNumConformers(int numConformers)
Definition: conformersearch.h:291
const RotorKeys & GetRotorKeys() const
Definition: conformersearch.h:349
long unsigned int GetNbEnergyCompute()
Definition: conformersearch.h:201
A lowest energy conformer scoring class.
Definition: conformersearch.h:193
OBConformerFilters(const std::vector< OBConformerFilter *> &filters)
Definition: conformersearch.h:86
OBMinimizingEnergyConformerScore()
Definition: conformersearch.h:221
Definition: conformersearch.h:158
Class for combining OBConformerFilter objects.
Definition: conformersearch.h:80
long unsigned int GetNbEnergyRequest()
Definition: conformersearch.h:227
std::vector< OBConformerFilter * > m_filters
Definition: conformersearch.h:101
long unsigned int GetNbEnergyRequest()
Definition: conformersearch.h:202
void SetCrossoverProbability(double value)
Definition: conformersearch.h:393
void SetSharing(bool value=true)
Definition: conformersearch.h:363
void SetNicheRadius(double value)
Definition: conformersearch.h:375
int GetNbNiches()
Definition: conformersearch.h:366
Convergence GetConvergence()
Definition: conformersearch.h:229
Rotate torsional according to rotor rules.
Given an OBMol, set up a list of possibly rotatable torsions,.
Definition: rotor.h:457
double GetSigmaSharing()
Definition: conformersearch.h:384
Molecule Class.
Definition: mol.h:118
bool IsGood(const OBMol &mol, const RotorKey &key, double *coords)
Definition: conformersearch.h:93
void PrintRotors(bool printrotors)
Definition: conformersearch.h:342
void SetLocalOptRate(int value)
Definition: conformersearch.h:402
void SetNumChildren(int numChildren)
Definition: conformersearch.h:295
Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue. (the main header for Open Babel) ...
void SetConvergence(int convergence)
Definition: conformersearch.h:304
int SetLocalOptRate()
Definition: conformersearch.h:405
Preferred GetPreferred()
Definition: conformersearch.h:228
void SetSigmaSharing(double value)
Definition: conformersearch.h:387
void SetMutability(int mutability)
Definition: conformersearch.h:299
std::vector< int > RotorKey
Definition: conformersearch.h:30
A speed-optimized vector of bits.
Definition: bitvec.h:57
Preferred GetPreferred()
Definition: conformersearch.h:250
std::map< std::vector< int >, double > mapRotorEnergy
Definition: conformersearch.h:32
A lowest energy conformer scoring class (after minimization)
Definition: conformersearch.h:218
A steric conformer filter class.
Definition: conformersearch.h:113
void SetLogStream(std::ostream *sptr)
Definition: conformersearch.h:357
Convergence
Definition: conformersearch.h:158
OBEnergyConformerScore()
Definition: conformersearch.h:196
std::vector< RotorKey > RotorKeys
Definition: conformersearch.h:31
void SetNicheMating(double value)
Definition: conformersearch.h:399
Conformer searching using genetic algorithm. See Conformer Searching.
Definition: conformersearch.h:268
Preferred
Definition: conformersearch.h:150
double GetNicheMating()
Definition: conformersearch.h:396
double GetNicheRadius()
Definition: conformersearch.h:372
Interface used by OBConformerSearch for filtering conformers.
Definition: conformersearch.h:55
Handle rotamer list data.
A RMSD conformer scoring class.
Definition: conformersearch.h:179
void SetAlphaSharing(double value)
Definition: conformersearch.h:381
double GetAlphaSharing()
Definition: conformersearch.h:378
void SetFilter(OBConformerFilter *filter)
Definition: conformersearch.h:319
double GetCrossoverProbability()
Definition: conformersearch.h:390
Preferred GetPreferred()
Definition: conformersearch.h:182
Interface used by OBConformerSearch for scoring conformers.
Definition: conformersearch.h:144
long unsigned int GetNbEnergyCompute()
Definition: conformersearch.h:226
void SetScore(OBConformerScore *score)
Definition: conformersearch.h:333
Preferred GetPreferred()
Definition: conformersearch.h:203
Convergence GetConvergence()
Definition: conformersearch.h:183
Convergence GetConvergence()
Definition: conformersearch.h:251
void SetFixedBonds(const OBBitVec &fixedBonds)
Definition: conformersearch.h:308
Global namespace for all Open Babel code.
Definition: alias.h:22