RDKit
Open-source cheminformatics and machine learning.
RandomSample.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer in the documentation and/or other materials provided
14 // with the distribution.
15 // * Neither the name of Novartis Institutes for BioMedical Research Inc.
16 // nor the names of its contributors may be used to endorse or promote
17 // products derived from this software without specific prior written
18 // permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //
32 
33 #ifndef RGROUP_RANDOM_SAMPLE_H
34 #define RGROUP_RANDOM_SAMPLE_H
35 
37 #include <boost/random.hpp>
38 #include <boost/random/uniform_int_distribution.hpp>
39 #include <sstream>
40 
41 namespace RDKit {
42 
43 //! This is a class for fully randomly sampling reagents.
44 // Note that this enumerator never halts.
45 /*!
46  basic usage:
47 
48  \verbatim
49  std::vector<MOL_SPTR_VECT> bbs;
50  bbs.push_back( bbs_for_reactants_1 );
51  bbs.push_back( bbs_for_reactants_2 );
52 
53  RandomSampleStrategy rgroups;
54  rgroups.initialize(rxn, bbs);
55  for(size_t i=0; i<num_samples && rgroups; ++i) {
56  MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
57  std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
58  ...
59  }
60  \endverbatim
61 
62  See EnumerationStrategyBase for more details and usage.
63 */
65  boost::uint64_t m_numPermutationsProcessed;
66  boost::minstd_rand m_rng;
67  std::vector<boost::random::uniform_int_distribution<> > m_distributions;
68 
69  public:
72  m_numPermutationsProcessed(),
73  m_rng(),
74  m_distributions() {
75  for (size_t i = 0; i < m_permutation.size(); ++i) {
76  m_distributions.push_back(
77  boost::random::uniform_int_distribution<>(0, m_permutation[i] - 1));
78  }
79  }
80 
82 
84  m_distributions.clear();
85  for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
86  m_distributions.push_back(boost::random::uniform_int_distribution<>(
87  0, m_permutationSizes[i] - 1));
88  }
89 
90  m_numPermutationsProcessed = 0;
91  }
92 
93  virtual const char *type() const { return "RandomSampleStrategy"; }
94 
95  //! The current permutation {r1, r2, ...}
96  virtual const EnumerationTypes::RGROUPS &next() {
97  for (size_t i = 0; i < m_permutation.size(); ++i) {
98  m_permutation[i] = m_distributions[i](m_rng);
99  }
100 
101  ++m_numPermutationsProcessed;
102 
103  return m_permutation;
104  }
105 
106  virtual boost::uint64_t getPermutationIdx() const {
107  return m_numPermutationsProcessed; }
108 
109  virtual operator bool() const { return true; }
110 
112  return new RandomSampleStrategy(*this);
113  }
114 
115  private:
116 #ifdef RDK_USE_BOOST_SERIALIZATION
117  friend class boost::serialization::access;
118 
119  template <class Archive>
120  void save(Archive &ar, const unsigned int /*version*/) const {
121  // invoke serialization of the base class
122  ar << boost::serialization::base_object<const EnumerationStrategyBase>(
123  *this);
124  ar << m_numPermutationsProcessed;
125 
126  std::stringstream random;
127  random << m_rng;
128  std::string s = random.str();
129  ar << s;
130  }
131 
132  template <class Archive>
133  void load(Archive &ar, const unsigned int /*version*/) {
134  // invoke serialization of the base class
135  ar >> boost::serialization::base_object<EnumerationStrategyBase>(*this);
136  ar >> m_numPermutationsProcessed;
137  std::string s;
138  ar >> s;
139  std::stringstream random(s);
140  random >> m_rng;
141 
142  // reset the uniform distributions
143  m_distributions.clear();
144  for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
145  m_distributions.push_back(boost::random::uniform_int_distribution<>(
146  0, m_permutationSizes[i] - 1));
147  }
148  }
149 
150  template <class Archive>
151  void serialize(Archive &ar, const unsigned int file_version) {
152  boost::serialization::split_member(ar, *this, file_version);
153  }
154 #endif
155 };
156 }
157 
158 #ifdef RDK_USE_BOOST_SERIALIZATION
159 BOOST_CLASS_VERSION(RDKit::RandomSampleStrategy, 1)
160 #endif
161 
162 #endif
virtual void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &)
Definition: RandomSample.h:83
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
virtual const char * type() const
Definition: RandomSample.h:93
EnumerationTypes::RGROUPS m_permutation
This is a class for fully randomly sampling reagents.
Definition: RandomSample.h:64
std::vector< MOL_SPTR_VECT > BBS
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
virtual const EnumerationTypes::RGROUPS & next()
The current permutation {r1, r2, ...}.
Definition: RandomSample.h:96
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
Definition: RandomSample.h:111
std::vector< boost::uint64_t > RGROUPS
EnumerationTypes::RGROUPS m_permutationSizes
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
Definition: RandomSample.h:106