Files
rdkit/External/GA/ga/IntegerStringChromosomePolicy.cpp
Greg Landrum da6cd73168 Run clang-format across everything (#7849)
* run clang-format-18 across Code/*.cpp and Code/*.h

* run clang-format-18 across External
2024-09-26 13:39:02 +02:00

70 lines
1.7 KiB
C++

//
// 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 "IntegerStringChromosomePolicy.h"
namespace GapeGa {
using namespace GarethUtil;
IntegerStringChromosomePolicy::IntegerStringChromosomePolicy(RandomUtil &rng_,
int size_)
: rng(rng_), size{size_}, maxs{new int[size]}, allowNulls{new bool[size]} {
setMax(10);
setAllowNulls(false);
}
IntegerStringChromosomePolicy::~IntegerStringChromosomePolicy() {
delete[] maxs;
delete[] allowNulls;
}
int IntegerStringChromosomePolicy::mutate(int pos, int currentValue) const {
assert(pos >= 0 && pos < size);
int max = maxs[pos];
int min = allowNulls[pos] ? -1 : 0;
int val = rng.randomInt(min, max - 1);
if (val >= currentValue) {
val++;
}
return val;
}
int IntegerStringChromosomePolicy::initialize(int pos) const {
assert(pos >= 0 && pos < size);
int max = maxs[pos];
int min = allowNulls[pos] ? -1 : 0;
return rng.randomInt(min, max);
}
void IntegerStringChromosomePolicy::setMax(int max) {
for (int i = 0; i < size; i++) {
maxs[i] = max;
}
}
void IntegerStringChromosomePolicy::setMax(int pos, int max) {
assert(pos >= 0 && pos < size);
maxs[pos] = max;
}
void IntegerStringChromosomePolicy::setAllowNulls(bool allow) {
for (int i = 0; i < size; i++) {
allowNulls[i] = allow;
}
}
void IntegerStringChromosomePolicy::setAllowNulls(int pos, bool allow) {
assert(pos >= 0 && pos < size);
allowNulls[pos] = allow;
}
} // namespace GapeGa