RDKit
Open-source cheminformatics and machine learning.
RGroupGa.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2020 Gareth Jones, Glysade LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 
11 #ifndef RGROUPDECOMPGA_H_
12 #define RGROUPDECOMPGA_H_
13 
14 #include <vector>
15 #include <memory>
16 #include <map>
17 #include <chrono>
18 
19 #include "../../../External/GA/ga/StringChromosome.h"
20 #include "../../../External/GA/ga/GaBase.h"
21 #include "../../../External/GA/ga/GaOperation.h"
22 #include "../../../External/GA/ga/LinkedPopLinearSel.h"
23 #include "../../../External/GA/ga/IntegerStringChromosomePolicy.h"
24 #include "RGroupFingerprintScore.h"
25 #include "RGroupScore.h"
26 
27 namespace RDKit {
28 
29 using namespace GapeGa;
30 using namespace std;
31 
32 class RGroupDecompositionChromosome;
33 class RGroupGa;
34 struct RGroupDecompData;
35 
36 typedef LinkedPopLinearSel<RGroupDecompositionChromosome, RGroupGa>
38 
39 typedef enum {
40  RgroupMutate = 0x01,
41  Crossover = 0x02,
42  Create = 0x04,
44 
45 class RGroupDecompositionChromosome : public IntegerStringChromosome {
46  public:
48 
49  double getFitness() const { return fitness; }
50 
51  OperationName getOperationName() const { return operationName; }
52 
53  void setOperationName(OperationName operationName) {
54  this->operationName = operationName;
55  }
56 
57  std::string info() const;
58 
59  double score();
60 
61  double recalculateScore();
62 
63  bool isOk() { return true; }
64 
65  void decode();
66 
67  void copyGene(const StringChromosomeBase& other) override;
68 
70  return fingerprintVarianceScoreData;
71  }
72 
73  const vector<size_t>& getPermutation() const { return permutation; }
74 
75  const RGroupGa& getRGroupGA() const { return rGroupGa; }
76 
77  private:
79  delete;
81  const RGroupDecompositionChromosome& other) = delete;
82  double fitness;
83  FingerprintVarianceScoreData fingerprintVarianceScoreData;
84  OperationName operationName = Create;
85  vector<size_t> permutation;
86 
87  RGroupGa& rGroupGa;
88 };
89 
90 struct GaResult {
92 
93  GaResult(const double score, const vector<vector<size_t>>& permutations)
94  : rGroupScorer(RGroupScorer(permutations, score)) {}
95  GaResult(const GaResult& other) : rGroupScorer(other.rGroupScorer) {}
96 
97  GaResult() {}
98 
99  // Copy constructor required by MSVC for future<GaResult>
100  GaResult& operator=(const GaResult& other);
101 };
102 
104  public:
105  RGroupGa(const RGroupDecompData& rGroupData,
106  const chrono::steady_clock::time_point* const t0 = nullptr);
107 
108  IntegerStringChromosomePolicy& getChromosomePolicy() {
109  return chromosomePolicy;
110  }
111 
112  int chromosomeLength() const { return chromLength; }
113 
114  int numberDecompositions() const { return numberDecomps; }
115 
116  GaResult run(int runNumber = 1);
117 
118  vector<GaResult> runBatch();
119 
120  shared_ptr<RGroupDecompositionChromosome> createChromosome();
121 
122  const RGroupDecompData& getRGroupData() const { return rGroupData; }
123 
124  const vector<shared_ptr<GaOperation<RGroupDecompositionChromosome>>>
125  getOperations() const;
126 
127  unsigned int numberPermutations() const { return numPermutations; }
128 
129  private:
130  RGroupGa(const RGroupGa& other) = delete;
131  RGroupGa& operator=(const RGroupGa& other) = delete;
132  const RGroupDecompData& rGroupData;
133 
134  IntegerStringChromosomePolicy chromosomePolicy;
135  int numberOperations;
136  int numberOperationsWithoutImprovement;
137  int chromLength;
138  unsigned int numPermutations;
139  int numberDecomps;
140  const chrono::steady_clock::time_point* const t0;
141 
142  void createOperations();
143 
144  static void rGroupMutateOperation(
145  const std::vector<std::shared_ptr<RGroupDecompositionChromosome>>&
146  parents,
147  std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& children);
148  static void rGroupCrossoverOperation(
149  const std::vector<std::shared_ptr<RGroupDecompositionChromosome>>&
150  parents,
151  std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& children);
152 };
153 
156 
158  FingerprintVarianceScoreData& fingerprintVarianceScoreData);
159 
160 } // namespace RDKit
161 
162 #endif // RGROUPDECOMPGA_H_
RGroupDecompositionChromosome(RGroupGa &rGroupGa)
OperationName getOperationName() const
Definition: RGroupGa.h:51
void setOperationName(OperationName operationName)
Definition: RGroupGa.h:53
const vector< size_t > & getPermutation() const
Definition: RGroupGa.h:73
FingerprintVarianceScoreData & getFingerprintVarianceScoreData()
Definition: RGroupGa.h:69
void copyGene(const StringChromosomeBase &other) override
const RGroupGa & getRGroupGA() const
Definition: RGroupGa.h:75
IntegerStringChromosomePolicy & getChromosomePolicy()
Definition: RGroupGa.h:108
const RGroupDecompData & getRGroupData() const
Definition: RGroupGa.h:122
int chromosomeLength() const
Definition: RGroupGa.h:112
int numberDecompositions() const
Definition: RGroupGa.h:114
RGroupGa(const RGroupDecompData &rGroupData, const chrono::steady_clock::time_point *const t0=nullptr)
vector< GaResult > runBatch()
const vector< shared_ptr< GaOperation< RGroupDecompositionChromosome > > > getOperations() const
shared_ptr< RGroupDecompositionChromosome > createChromosome()
GaResult run(int runNumber=1)
unsigned int numberPermutations() const
Definition: RGroupGa.h:127
#define RDKIT_RGROUPDECOMPOSITION_EXPORT
Definition: export.h:385
Std stuff.
Definition: Abbreviations.h:18
void copyVarianceData(const FingerprintVarianceScoreData &fromData, FingerprintVarianceScoreData &toData)
OperationName
Definition: RGroupGa.h:39
@ Crossover
Definition: RGroupGa.h:41
@ Create
Definition: RGroupGa.h:42
@ RgroupMutate
Definition: RGroupGa.h:40
void clearVarianceData(FingerprintVarianceScoreData &fingerprintVarianceScoreData)
LinkedPopLinearSel< RGroupDecompositionChromosome, RGroupGa > RGroupGaPopulation
Definition: RGroupGa.h:34
RGroupScorer rGroupScorer
Definition: RGroupGa.h:91
GaResult & operator=(const GaResult &other)
GaResult(const GaResult &other)
Definition: RGroupGa.h:95
GaResult(const double score, const vector< vector< size_t >> &permutations)
Definition: RGroupGa.h:93