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