1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
//
// Copyright (C) 2020 Gareth Jones, Glysade LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <ctime>
#include <limits>
#ifdef RDK_BUILD_THREADSAFE_SSS
#include <future>
#endif
#include <assert.h>
#include "RGroupGa.h"
#include "RGroupDecompData.h"
#include "RGroupDecomp.h"
#include "RGroupFingerprintScore.h"
#include "../../../External/GA/util/Util.h"
// #define DEBUG
namespace RDKit {
RGroupDecompositionChromosome::RGroupDecompositionChromosome(RGroupGa& rGroupGa)
: IntegerStringChromosome(rGroupGa.chromosomeLength(), rGroupGa.getRng(),
rGroupGa.getChromosomePolicy()),
rGroupGa(rGroupGa) {
permutation.reserve(rGroupGa.numberDecompositions());
}
std::string RGroupDecompositionChromosome::info() const {
auto format = boost::format("Fit %7.3f : ") % fitness;
return format.str() + geneInfo();
}
double RGroupDecompositionChromosome::score() {
auto& rGroupData = rGroupGa.getRGroupData();
RGroupScore scoreMethod =
static_cast<RGroupScore>(rGroupData.params.scoreMethod);
if (operationName != RgroupMutate) {
decode();
}
if (scoreMethod == FingerprintVariance &&
fingerprintVarianceScoreData.labelsToVarianceData.size() > 0 &&
operationName == RgroupMutate) {
fitness = fingerprintVarianceScoreData.fingerprintVarianceGroupScore();
// Uncomment the following line to check that the
// fingerprintVarianceGroupScore is giving the correct result. Don't do
// this in production as it will be extremely slow.
// assert(fitness == recalculateScore());
} else {
fitness = rGroupData.score(permutation, &fingerprintVarianceScoreData);
}
return fitness;
}
double RGroupDecompositionChromosome::recalculateScore() {
auto& rGroupData = rGroupGa.getRGroupData();
return rGroupData.score(permutation);
}
void RGroupDecompositionChromosome::decode() {
auto values = getString();
permutation.clear();
const auto& matches = rGroupGa.getRGroupData().matches;
auto pos = 0;
for (const auto& m : matches) {
if (m.size() == 1) {
permutation.push_back(0);
} else {
permutation.push_back(values[pos]);
pos++;
}
}
}
void RGroupDecompositionChromosome::copyGene(
const StringChromosomeBase<int, IntegerStringChromosomePolicy>& other) {
StringChromosomeBase<int, IntegerStringChromosomePolicy>::copyGene(other);
const auto& parent = static_cast<const RGroupDecompositionChromosome&>(other);
copyVarianceData(parent.fingerprintVarianceScoreData,
fingerprintVarianceScoreData);
}
GaResult& GaResult::operator=(const GaResult& other) {
if (&other == this) {
return *this;
}
rGroupScorer = other.rGroupScorer;
return *this;
}
RGroupGa::RGroupGa(const RGroupDecompData& rGroupData,
const chrono::steady_clock::time_point* const t0)
: rGroupData(rGroupData),
chromosomePolicy(getRng(), rGroupData.matches.size()),
t0(t0) {
setSelectionPressure(1.0001);
const auto& matches = rGroupData.matches;
numPermutations = 1L;
auto pos = 0;
for (auto m : matches) {
if (m.size() == 1) {
continue;
}
chromosomePolicy.setMax(pos, m.size());
unsigned long count = numPermutations * m.size();
numPermutations = std::min(count, static_cast<unsigned long>(numeric_limits<unsigned int>::max()));
pos++;
}
chromLength = pos;
numberDecomps = matches.size();
// TODO refine these settings
auto popsize = 100 + chromLength / 10;
if (popsize > 200) {
popsize = 200;
}
const auto& params = rGroupData.params;
if (params.gaPopulationSize > 0) {
popsize = params.gaPopulationSize;
}
// For now run the GA a long time and exit early if no improvement in the
// score is seen
numberOperations = 1000000;
if (params.gaMaximumOperations > 0) {
numberOperations = params.gaMaximumOperations;
}
numberOperationsWithoutImprovement = 7500;
if (params.gaNumberOperationsWithoutImprovement > 0) {
numberOperationsWithoutImprovement =
params.gaNumberOperationsWithoutImprovement;
}
setPopsize(popsize);
uint32_t rngSeed;
if (params.gaRandomSeed >= 0) {
rngSeed = params.gaRandomSeed;
getRng().seed(rngSeed);
} else if (params.gaRandomSeed == -2) {
random_device rd;
auto seed = rd();
rngSeed = seed;
getRng().seed(rngSeed);
} else {
rngSeed = mt19937::default_seed;
}
BOOST_LOG(rdInfoLog) << "GA RNG seed " << rngSeed << endl;
}
void RGroupGa::rGroupMutateOperation(
const std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& parents,
std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& children) {
auto parent = parents[0];
auto child = children[0];
child->copyGene(*parent);
child->mutate();
child->setOperationName(RgroupMutate);
child->decode();
auto& fingerprintVarianceScoreData = child->getFingerprintVarianceScoreData();
if (fingerprintVarianceScoreData.labelsToVarianceData.size() == 0) {
return;
}
#ifdef DEBUG
std::cerr << "RGroup mutate start" << std::endl;
#endif
#ifdef DEBUG
std::cerr << "Starting child score" << std::endl;
fingerprintVarianceGroupScore(fingerprintVarianceScoreData);
#endif
auto& parentPermutation = parent->getPermutation();
auto& childPermutation = child->getPermutation();
const auto& rgroupData = parent->getRGroupGA().getRGroupData();
const auto& matches = rgroupData.matches;
const auto& labels = rgroupData.labels;
for (auto pos = 0U; pos < parentPermutation.size(); pos++) {
int parentValue = parentPermutation.at(pos);
int childValue = childPermutation.at(pos);
if (parentValue != childValue) {
fingerprintVarianceScoreData.removeVarianceData(pos, parentValue, matches,
labels);
#ifdef DEBUG
std::cerr << "After removing parent" << std::endl;
fingerprintVarianceGroupScore(fingerprintVarianceScoreData);
#endif
fingerprintVarianceScoreData.addVarianceData(pos, childValue, matches,
labels);
#ifdef DEBUG
std::cerr << "After adding child" << std::endl;
fingerprintVarianceGroupScore(fingerprintVarianceScoreData);
#endif
}
}
#ifdef DEBUG
std::cerr << "Final recalculating" << std::endl;
child->recalculateScore();
std::cerr << "RGroup mutate done" << std::endl;
#endif
}
void RGroupGa::rGroupCrossoverOperation(
const std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& parents,
std::vector<std::shared_ptr<RGroupDecompositionChromosome>>& children) {
auto parent1 = parents[0];
auto child1 = children[0];
auto parent2 = parents[1];
auto child2 = children[1];
child1->setOperationName(Crossover);
child2->setOperationName(Crossover);
clearVarianceData(child1->getFingerprintVarianceScoreData());
clearVarianceData(child2->getFingerprintVarianceScoreData());
parent1->twoPointCrossover(*parent2, *child1, *child2);
}
const vector<shared_ptr<GaOperation<RGroupDecompositionChromosome>>>
RGroupGa::getOperations() const {
// bias to mutation as that operator is so efficient
auto mutationOperation =
make_shared<GaOperation<RGroupDecompositionChromosome>>(
1, 1, 75.0, &rGroupMutateOperation);
auto crossoverOperation =
make_shared<GaOperation<RGroupDecompositionChromosome>>(
2, 2, 25.0, &rGroupCrossoverOperation);
vector<shared_ptr<GaOperation<RGroupDecompositionChromosome>>> operations;
operations.reserve(2);
operations.push_back(mutationOperation);
operations.push_back(crossoverOperation);
return operations;
}
std::string timeInfo(const std::clock_t start) {
auto now = std::clock();
auto seconds = (now - start) / (double)CLOCKS_PER_SEC;
auto format = boost::format("Time %7.2f") % seconds;
return format.str();
}
GaResult RGroupGa::run(int runNumber) {
auto startTime = clock();
RGroupGaPopulation population{*this};
auto format =
boost::format(
"Running GA run %2d number operations %5d population size %5d "
"number operations without improvement %5d "
"chromosome length %5d %s\n") %
runNumber % numberOperations % getPopsize() %
numberOperationsWithoutImprovement % chromLength % timeInfo(startTime);
BOOST_LOG(rdInfoLog) << format.str();
population.create();
double bestScore = population.getBestScore();
BOOST_LOG(rdInfoLog) << population.info() << endl;
int nOps = 0;
int lastImprovementOp = 0;
while (nOps < numberOperations) {
population.iterate();
nOps++;
if (nOps % 1000 == 0) {
BOOST_LOG(rdInfoLog) << "Run " << runNumber << " " << population.info()
<< " " << timeInfo(startTime) << endl;
}
if (population.getBestScore() > bestScore) {
bestScore = population.getBestScore();
lastImprovementOp = nOps;
auto format = boost::format("Run %2d OP %5d Fit %7.3f %s\n") % runNumber %
nOps % bestScore % timeInfo(startTime);
BOOST_LOG(rdInfoLog) << format.str();
}
if (nOps - lastImprovementOp > numberOperationsWithoutImprovement) {
BOOST_LOG(rdInfoLog) << "Run " << runNumber << " Op " << nOps
<< " No improvement since " << lastImprovementOp
<< " finishing.." << endl;
break;
}
if (t0 && checkForTimeout(*t0, rGroupData.params.timeout)) {
break;
}
}
const shared_ptr<RGroupDecompositionChromosome> best = population.getBest();
auto ties = population.getTiedBest();
vector<vector<size_t>> permutations;
permutations.reserve(ties.size());
std::transform(ties.cbegin(), ties.cend(), back_inserter(permutations),
[](const shared_ptr<RGroupDecompositionChromosome> c) {
return c->getPermutation();
});
BOOST_LOG(rdInfoLog) << "Run " << runNumber << " Execution "
<< timeInfo(startTime) << std::endl;
GaResult result{best->getFitness(), permutations};
return result;
}
vector<GaResult> RGroupGa::runBatch() {
int numberRuns = rGroupData.params.gaNumberRuns;
bool gaParallelRuns = rGroupData.params.gaParallelRuns;
#ifndef RDK_BUILD_THREADSAFE_SSS
if (gaParallelRuns) {
gaParallelRuns = false;
BOOST_LOG(rdWarningLog)
<< "This RDKit build does not enable GA parallel runs" << std::endl;
}
#endif
vector<GaResult> results;
results.reserve(numberRuns);
if (gaParallelRuns) {
#ifdef RDK_BUILD_THREADSAFE_SSS
vector<future<GaResult>> tasks;
tasks.reserve(numberRuns);
for (int n = 0; n < numberRuns; n++) {
auto future = async(launch::async, &RDKit::RGroupGa::run, this, n + 1);
tasks.push_back(move(future));
}
std::transform(tasks.begin(), tasks.end(), back_inserter(results),
[](future<GaResult>& f) { return f.get(); });
#endif
} else {
for (int n = 0; n < numberRuns; n++) {
auto result = run(n + 1);
results.push_back(result);
}
}
return results;
}
shared_ptr<RGroupDecompositionChromosome> RGroupGa::createChromosome() {
return make_shared<RGroupDecompositionChromosome>(*this);
}
void copyVarianceData(const FingerprintVarianceScoreData& fromData,
FingerprintVarianceScoreData& toData) {
auto& from = fromData.labelsToVarianceData;
auto& to = toData.labelsToVarianceData;
for (auto it = from.cbegin(); it != from.end(); ++it) {
auto df = to.find(it->first);
if (df == to.end()) {
to.emplace(it->first, make_shared<VarianceDataForLabel>(*it->second));
} else {
auto& fromData = it->second;
auto& toData = df->second;
toData->numberFingerprints = fromData->numberFingerprints;
toData->bitCounts.assign(fromData->bitCounts.cbegin(),
fromData->bitCounts.cend());
}
}
toData.numberOfMolecules = fromData.numberOfMolecules;
toData.numberOfMissingUserRGroups = fromData.numberOfMissingUserRGroups;
}
void clearVarianceData(
FingerprintVarianceScoreData& fingerprintVarianceScoreData) {
auto& data = fingerprintVarianceScoreData.labelsToVarianceData;
for (auto it = data.begin(); it != data.end(); ++it) {
auto d = it->second;
d->numberFingerprints = 0;
d->bitCounts.assign(d->bitCounts.size(), 0.0);
}
fingerprintVarianceScoreData.numberOfMissingUserRGroups = 0;
fingerprintVarianceScoreData.numberOfMolecules = 0;
}
} // namespace RDKit
|