RDKit
Open-source cheminformatics and machine learning.
EvenSamplePairs.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016, 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 #include <RDGeneral/export.h>
34 #ifndef RGROUP_EVEN_SAMPLE_H
35 #define RGROUP_EVEN_SAMPLE_H
36 
38 #ifdef RDK_USE_BOOST_SERIALIZATION
39 #include <boost/serialization/set.hpp>
40 #endif
41 #include <boost/cstdint.hpp>
42 
43 namespace RDKit {
44 //! EvenSamplePairsStrategy
45 /*! Randomly sample Pairs evenly from a collection of building blocks
46  This is a good strategy for choosing a relatively small selection
47  of building blocks from a larger set. As the amount of work needed
48  to retrieve the next evenly sample building block grows with the
49  number of samples, this method performs progressively worse as the
50  number of samples gets larger.
51 
52  See EnumeartionStrategyBase for more details.
53 */
54 
56  boost::uint64_t m_numPermutationsProcessed;
57 
58  std::vector<boost::int64_t> used_count;
59  std::vector<std::vector<boost::uint64_t> > var_used;
60  std::vector<std::vector<boost::uint64_t> > pair_used;
61  std::vector<std::vector<boost::uint64_t> > pair_counts;
62  std::set<boost::uint64_t> selected;
63 
64  boost::uint64_t seed; // last seed for permutation (starts at 0)
65  boost::uint64_t M, a, b; // random number stuff
66  boost::uint64_t nslack, min_nslack;
67  boost::uint64_t rejected_period, rejected_unique;
68  boost::uint64_t rejected_slack_condition, rejected_bb_sampling_condition;
69 
70  public:
73  m_numPermutationsProcessed(),
74  used_count(),
75  var_used(),
76  pair_used(),
77  pair_counts(),
78  selected(),
79  seed(),
80  M(),
81  a(),
82  b(),
83  nslack(),
84  min_nslack(),
85  rejected_period(),
86  rejected_unique(),
87  rejected_slack_condition(),
88  rejected_bb_sampling_condition() {}
89 
92  m_numPermutationsProcessed(rhs.m_numPermutationsProcessed),
93  used_count(rhs.used_count),
94  var_used(rhs.var_used),
95  pair_used(rhs.pair_used),
96  pair_counts(rhs.pair_counts),
97  selected(rhs.selected),
98  seed(rhs.seed),
99  M(rhs.M),
100  a(rhs.a),
101  b(rhs.b),
102  nslack(rhs.nslack),
103  min_nslack(rhs.min_nslack),
104  rejected_period(rhs.rejected_period),
105  rejected_unique(rhs.rejected_unique),
106  rejected_slack_condition(rhs.rejected_slack_condition),
107  rejected_bb_sampling_condition(rhs.rejected_bb_sampling_condition) {}
108 
109  virtual const char *type() const { return "EvenSamplePairsStrategy"; }
110 
111  //! This is a class for enumerating RGroups using Cartesian Products of
112  //! reagents.
113  /*!
114  basic usage:
115 
116  \verbatim
117  std::vector<MOL_SPTR_VECT> bbs;
118  bbs.push_back( bbs_for_reactants_1 );
119  bbs.push_back( bbs_for_reactants_2 );
120 
121  EvenSamplePairsStrategy rgroups;
122  rgroups.initialize(rxn, bbs);
123  for(boost::uint64_t i=0; i<num_samples && rgroups; ++i) {
124  MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
125  std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
126  ...
127  }
128  \endverbatim
129  */
131 
132  virtual void initializeStrategy(const ChemicalReaction &,
133  const EnumerationTypes::BBS &);
134 
135  //! The current permutation {r1, r2, ...}
136  virtual const EnumerationTypes::RGROUPS &next();
137 
138  virtual boost::uint64_t getPermutationIdx() const {
139  return m_numPermutationsProcessed;
140  }
141 
142  virtual operator bool() const { return true; }
143 
145  return new EvenSamplePairsStrategy(*this);
146  }
147 
148  std::string stats() const;
149 
150  private:
151  friend class boost::serialization::access;
152 
153  // decode a packed integer into an RGroup selection
154  const EnumerationTypes::RGROUPS &decode(boost::uint64_t seed) {
155  for (boost::int64_t j = m_permutationSizes.size() - 1; j >= 0; j--) {
156  m_permutation[j] = seed % m_permutationSizes[j];
157  seed /= m_permutationSizes[j];
158  }
159  return m_permutation;
160  }
161 
162  bool try_add(boost::uint64_t seed);
163 
164  public:
165 #ifdef RDK_USE_BOOST_SERIALIZATION
166  template <class Archive>
167  void serialize(Archive &ar, const unsigned int /*version*/) {
168  // invoke serialization of the base class
169  ar &boost::serialization::base_object<EnumerationStrategyBase>(*this);
170  ar &m_numPermutationsProcessed;
171  ar &used_count;
172  ar &var_used;
173  ar &pair_used;
174  ar &pair_counts;
175  ar &selected;
176 
177  ar &seed;
178 
179  ar &M;
180  ar &a;
181  ar &b;
182 
183  ar &nslack;
184  ar &min_nslack;
185  ar &rejected_period;
186  ar &rejected_unique;
187  ar &rejected_slack_condition;
188  ar &rejected_bb_sampling_condition;
189  }
190 #endif
191 };
192 }
193 
194 BOOST_CLASS_VERSION(RDKit::EvenSamplePairsStrategy, 1)
195 
196 #endif
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
EvenSamplePairsStrategy(const EvenSamplePairsStrategy &rhs)
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:118
EvenSamplePairsStrategy.
virtual const char * type() const
std::vector< MOL_SPTR_VECT > BBS
Std stuff.
Definition: Atom.h:30
std::vector< boost::uint64_t > RGROUPS
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:60
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state