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
|
//
// 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.
//
/**
* A template class for GA array based (e.g. bool, int) chromosomes.
*
* A ChromosomePolicy is required. It's contract includes a initialization,
* mutate and allowSwitch methods.
*/
#ifndef STRINGCHROMOSOME_H_
#define STRINGCHROMOSOME_H_
#include "../util/RandomUtil.h"
#include <cassert>
#include <string>
#include <sstream>
#include <typeinfo>
#include <memory>
namespace GapeGa {
using namespace GarethUtil;
template <typename T, typename ChromosomePolicy>
class StringChromosomeBase {
private:
RandomUtil &rng;
ChromosomePolicy &chromosomePolicy;
StringChromosomeBase(const StringChromosomeBase &other);
StringChromosomeBase &operator=(const StringChromosomeBase &other);
protected:
const int length;
std::unique_ptr<T[]> string;
public:
StringChromosomeBase(int length_, RandomUtil &rng_,
ChromosomePolicy &chromosomePolicy_)
: rng(rng_),
chromosomePolicy(chromosomePolicy_),
length(length_),
string(new T[length_]) {
;
}
virtual ~StringChromosomeBase() {}
void initialize();
bool equals(const StringChromosomeBase &other) const;
virtual void copyGene(const StringChromosomeBase &other);
void mutate(double pMutate = -1);
void twoPointCrossover(const StringChromosomeBase &parent2,
StringChromosomeBase &child1,
StringChromosomeBase &child2) const;
void onePointCrossover(const StringChromosomeBase &parent2,
StringChromosomeBase &child1,
StringChromosomeBase &child2) const;
std::string geneInfo() const;
const T getValue(int pos) const;
T *getString() const;
int getLength() const { return length; }
RandomUtil &getRng() const { return rng; }
ChromosomePolicy &getChromosomePolicy() const { return chromosomePolicy; }
};
/**
* Initializes the chromosome to new values chosen by the policy
*/
template <typename T, typename ChromosomePolicy>
void StringChromosomeBase<T, ChromosomePolicy>::initialize() {
for (int i = 0; i < length; i++) {
string[i] = chromosomePolicy.initialize(i);
}
}
/**
*
* @param other
* @return true if two chromosome strings are the same
*/
template <typename T, typename ChromosomePolicy>
bool StringChromosomeBase<T, ChromosomePolicy>::equals(
const StringChromosomeBase &other) const {
if (other.length != length) {
return false;
}
T *ptr = string.get();
T *otherPtr = other.string.get();
for (int i = 0; i < length; i++, ptr++, otherPtr++) {
if (*ptr != *otherPtr) {
return false;
}
}
return true;
}
/**
* copies the gene to another chromosome
*
* @param other
*/
template <typename T, typename ChromosomePolicy>
void StringChromosomeBase<T, ChromosomePolicy>::copyGene(
const StringChromosomeBase &other) {
assert(length == other.length);
assert(typeid(other.string) == typeid(string));
T *ptr = string.get(), *otherPtr = other.string.get();
for (int i = 0; i < length; i++, ptr++, otherPtr++) {
*ptr = *otherPtr;
}
}
/**
* Randomly mutates at least one position in the string
*/
template <typename T, typename ChromosomePolicy>
void StringChromosomeBase<T, ChromosomePolicy>::mutate(double pMutate) {
if (pMutate < 0.0) {
pMutate = 1.0 / (length);
}
bool mutated = false;
T *ptr = string.get();
for (int i = 0; i < length; i++, ptr++) {
if (rng.normalRand() < pMutate) {
T newVal = chromosomePolicy.mutate(i, *ptr);
*ptr = newVal;
mutated = true;
}
}
if (!mutated) {
mutate();
}
}
/**
* Performs two point (or circular) crossover
*
* @param parent2
* @param child1
* @param child2
*/
template <typename T, typename ChromosomePolicy>
void StringChromosomeBase<T, ChromosomePolicy>::twoPointCrossover(
const StringChromosomeBase &parent2, StringChromosomeBase &child1,
StringChromosomeBase &child2) const {
// choose cross point sites
int site1 = rng.randomInt(0, length);
int site2 = rng.randomInt(0, length - 1);
if (site2 >= site1) {
site2++;
} else {
int n = site1;
site1 = site2;
site2 = n;
}
bool switchFlag = false;
if (chromosomePolicy.isAllowSwitch()) {
switchFlag = rng.randomBoolean();
}
T *c1 = switchFlag ? child2.string.get() : child1.string.get();
T *c2 = switchFlag ? child1.string.get() : child2.string.get();
T *p1 = string.get();
T *p2 = parent2.string.get();
// create children before 1st cross point
int pos = 0;
for (; pos < site1; pos++, c1++, c2++, p1++, p2++) {
*c1 = *p1;
*c2 = *p2;
}
// children between cross points
for (; pos < site2; pos++, c1++, c2++, p1++, p2++) {
*c1 = *p2;
*c2 = *p1;
}
// children after 2nd cross point
for (; pos < length; pos++, c1++, c2++, p1++, p2++) {
*c1 = *p1;
*c2 = *p2;
}
}
/**
* Performs one point crossover
*
* @param parent2
* @param child1
* @param child2
*/
template <typename T, typename ChromosomePolicy>
void StringChromosomeBase<T, ChromosomePolicy>::onePointCrossover(
const StringChromosomeBase &parent2, StringChromosomeBase &child1,
StringChromosomeBase &child2) const {
// choose cross point site
int site = rng.randomInt(0, length - 1);
bool switchFlag = false;
if (chromosomePolicy.isAllowSwitch()) {
switchFlag = rng.randomBoolean();
}
T *c1 = switchFlag ? child2.string.get() : child1.string.get();
T *c2 = switchFlag ? child1.string.get() : child2.string.get();
T *p1 = string.get();
T *p2 = parent2.string.get();
// create children before cross point
int pos = 0;
for (; pos < site; pos++, c1++, c2++, p1++, p2++) {
*c1 = *p1;
*c2 = *p2;
}
// children after cross point
for (; pos < length; pos++, c1++, c2++, p1++, p2++) {
*c1 = *p2;
*c2 = *p1;
}
}
/**
*
* @return a string representation of the chromosome
*
*/
template <typename T, typename ChromosomePolicy>
std::string StringChromosomeBase<T, ChromosomePolicy>::geneInfo() const {
std::stringstream ss;
for (int i = 0; i < length; i++) {
if (i > 0) {
ss << ' ';
}
ss << string[i];
}
return ss.str();
}
/**
*
* @param pos position on the chromosome
* @return the value on the chromosome at the position
*/
template <typename T, typename ChromosomePolicy>
const T StringChromosomeBase<T, ChromosomePolicy>::getValue(int pos) const {
return string[pos];
}
template <typename T, typename ChromosomePolicy>
T *StringChromosomeBase<T, ChromosomePolicy>::getString() const {
return string.get();
}
} // namespace GapeGa
#endif /* STRINGCHROMOSOME_H_ */
|