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 <cstdint>
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  : public EnumerationStrategyBase {
57  boost::uint64_t m_numPermutationsProcessed;
58 
59  std::vector<boost::int64_t> used_count;
60  std::vector<std::vector<boost::uint64_t>> var_used;
61  std::vector<std::vector<boost::uint64_t>> pair_used;
62  std::vector<std::vector<boost::uint64_t>> pair_counts;
63  std::set<boost::uint64_t> selected;
64 
65  boost::uint64_t seed; // last seed for permutation (starts at 0)
66  boost::uint64_t M, a, b; // random number stuff
67  boost::uint64_t nslack, min_nslack;
68  boost::uint64_t rejected_period, rejected_unique;
69  boost::uint64_t rejected_slack_condition, rejected_bb_sampling_condition;
70 
71  public:
74  m_numPermutationsProcessed(),
75  used_count(),
76  var_used(),
77  pair_used(),
78  pair_counts(),
79  selected(),
80  seed(),
81  M(),
82  a(),
83  b(),
84  nslack(),
85  min_nslack(),
86  rejected_period(),
87  rejected_unique(),
88  rejected_slack_condition(),
89  rejected_bb_sampling_condition() {}
90 
93  m_numPermutationsProcessed(rhs.m_numPermutationsProcessed),
94  used_count(rhs.used_count),
95  var_used(rhs.var_used),
96  pair_used(rhs.pair_used),
97  pair_counts(rhs.pair_counts),
98  selected(rhs.selected),
99  seed(rhs.seed),
100  M(rhs.M),
101  a(rhs.a),
102  b(rhs.b),
103  nslack(rhs.nslack),
104  min_nslack(rhs.min_nslack),
105  rejected_period(rhs.rejected_period),
106  rejected_unique(rhs.rejected_unique),
107  rejected_slack_condition(rhs.rejected_slack_condition),
108  rejected_bb_sampling_condition(rhs.rejected_bb_sampling_condition) {}
109 
110  virtual const char *type() const { return "EvenSamplePairsStrategy"; }
111 
112  //! This is a class for enumerating RGroups using Cartesian Products of
113  //! reagents.
114  /*!
115  basic usage:
116 
117  \verbatim
118  std::vector<MOL_SPTR_VECT> bbs;
119  bbs.push_back( bbs_for_reactants_1 );
120  bbs.push_back( bbs_for_reactants_2 );
121 
122  EvenSamplePairsStrategy rgroups;
123  rgroups.initialize(rxn, bbs);
124  for(boost::uint64_t i=0; i<num_samples && rgroups; ++i) {
125  MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
126  std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
127  ...
128  }
129  \endverbatim
130  */
132 
133  virtual void initializeStrategy(const ChemicalReaction &,
134  const EnumerationTypes::BBS &);
135 
136  //! The current permutation {r1, r2, ...}
137  virtual const EnumerationTypes::RGROUPS &next();
138 
139  virtual boost::uint64_t getPermutationIdx() const {
140  return m_numPermutationsProcessed;
141  }
142 
143  virtual operator bool() const { return true; }
144 
146  return new EvenSamplePairsStrategy(*this);
147  }
148 
149  std::string stats() const;
150 
151  private:
152  friend class boost::serialization::access;
153 
154  // decode a packed integer into an RGroup selection
155  const EnumerationTypes::RGROUPS &decode(boost::uint64_t seed) {
156  for (boost::int64_t j = m_permutationSizes.size() - 1; j >= 0; j--) {
157  m_permutation[j] = seed % m_permutationSizes[j];
158  seed /= m_permutationSizes[j];
159  }
160  return m_permutation;
161  }
162 
163  bool try_add(boost::uint64_t seed);
164 
165  public:
166 #ifdef RDK_USE_BOOST_SERIALIZATION
167  template <class Archive>
168  void serialize(Archive &ar, const unsigned int /*version*/) {
169  // invoke serialization of the base class
170  ar &boost::serialization::base_object<EnumerationStrategyBase>(*this);
171  ar &m_numPermutationsProcessed;
172  ar &used_count;
173  ar &var_used;
174  ar &pair_used;
175  ar &pair_counts;
176  ar &selected;
177 
178  ar &seed;
179 
180  ar &M;
181  ar &a;
182  ar &b;
183 
184  ar &nslack;
185  ar &min_nslack;
186  ar &rejected_period;
187  ar &rejected_unique;
188  ar &rejected_slack_condition;
189  ar &rejected_bb_sampling_condition;
190  }
191 #endif
192 };
193 } // namespace RDKit
194 
195 BOOST_CLASS_VERSION(RDKit::EvenSamplePairsStrategy, 1)
196 
197 #endif
EnumerationStrategyBase.h
RDKit::EvenSamplePairsStrategy::EvenSamplePairsStrategy
EvenSamplePairsStrategy()
Definition: EvenSamplePairs.h:72
RDKit::EvenSamplePairsStrategy::EvenSamplePairsStrategy
EvenSamplePairsStrategy(const EvenSamplePairsStrategy &rhs)
Definition: EvenSamplePairs.h:91
RDKit::EvenSamplePairsStrategy::getPermutationIdx
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
Definition: EvenSamplePairs.h:139
RDKit::EvenSamplePairsStrategy::type
virtual const char * type() const
Definition: EvenSamplePairs.h:110
RDKit::EnumerationTypes::BBS
std::vector< MOL_SPTR_VECT > BBS
Definition: EnumerateTypes.h:42
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:119
RDKit::EnumerationStrategyBase
Definition: EnumerationStrategyBase.h:120
RDKit::EnumerationTypes::RGROUPS
std::vector< boost::uint64_t > RGROUPS
Definition: EnumerateTypes.h:56
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::EvenSamplePairsStrategy::copy
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
Definition: EvenSamplePairs.h:145
RDKIT_CHEMREACTIONS_EXPORT
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:60
RDKit::EvenSamplePairsStrategy
EvenSamplePairsStrategy.
Definition: EvenSamplePairs.h:55
RDKit::EnumerationStrategyBase::initialize
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
Definition: EnumerationStrategyBase.h:141
export.h