-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature.cc
34 lines (27 loc) · 1006 Bytes
/
creature.cc
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
#include "creature.h"
#include <memory>
#include <vector>
#include "utils.h"
int Creature::DnaSize() { return this->dna.size(); }
void Creature::Mutate(int i) {
// Uniform random mutation
// Haupt, Randy L., Sue Ellen Haupt, and Sue Ellen Autor Haupt. 2004.
// Practical Genetic Algorithms. Wiley.
this->dna[i].Set(Random());
this->hasGetCostCache = false;
}
void Creature::Mate(Creature& c1, Creature* child0, Creature* child1) {
// Randcliff blending method
// Haupt, Randy L., Sue Ellen Haupt, and Sue Ellen Autor Haupt. 2004.
// Practical Genetic Algorithms. Wiley.
double beta;
for (int i = 0; i < this->DnaSize(); i++) {
beta = Random();
child0->dna[i].Set(beta * this->dna[i].Get() +
(1 - beta) * c1.dna[i].Get());
child1->dna[i].Set((1 - beta) * this->dna[i].Get() +
beta * c1.dna[i].Get());
}
child0->hasGetCostCache = false;
child1->hasGetCostCache = false;
}