-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossover.cpp
57 lines (45 loc) · 1.06 KB
/
Crossover.cpp
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
/*
* Crossover.cpp
*
* Created on: Feb 23, 2012
* Author: Toby
*/
#include "Crossover.h"
#include "BuildList.h"
//#include "F.h"
//#include <cstdlib>
//#include <iostream>
Crossover::Crossover(OF *oF) {
this->oF = oF;
}
Crossover::~Crossover() {}
BuildList* Crossover::createChild(BuildList* b1, BuildList* b2) {
BuildList *c = singlePoint(b1, b2);
BuildList *c2 = singlePoint(b1, b2);
if (c->getFitness() > c2->getFitness()) {
delete c2;
c->lengthenEntityList();
return c;
}
else {
delete c;
c2->lengthenEntityList();
return c2;
}
}
BuildList* Crossover::singlePoint(BuildList* b, BuildList* b2) {
unsigned long num = b->getEventNum();
if (num == 0) {
num++;
}
unsigned long crossPoint = oF->nextInt(1, num);
BuildList *child = new BuildList(oF);
for (unsigned long i = 0; i < crossPoint; i++) {
child->add(b->get(i));
}
for (unsigned long i = crossPoint; i < b->size(); i++) {
child->add(b2->get(i));
}
child->evaluateBuild();
return child;
}