-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdna.cpp
More file actions
146 lines (125 loc) · 3.92 KB
/
Copy pathdna.cpp
File metadata and controls
146 lines (125 loc) · 3.92 KB
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
#include "dna.h"
#include <cstdlib>
#define CROSSOVER_VAL 0
MutationTable default_mutation_table = {
0.011f, // add chunk
0.009f, // remove chunk
0.1f, // move chunk
0.01f, // reverse chunk
0.58f, // swap bit
0.29f, // random byte
0.00f, // add crossover
};
DNA::DNA(): data(0), mutation(0) {
}
DNA::DNA(QByteArray &d, float mut): data(&d), mutation(mut) {
}
void DNA::blend(const DNA &mum, const DNA &dad, int max_size, int min_size) {
const QByteArray &mumData = mum.getData();
const QByteArray &dadData = dad.getData();
const QByteArray *current, *other;
//int coin = qrand() % 2, s;
data->resize(qMax(mumData.size(), dadData.size()));
//int s;
//data->resize(s = qMax(mumData.size(),dadData.size()));
unsigned int combo = qrand() % 2;
current = (combo ? &mumData : &dadData);
other = (combo == 0 ? &mumData : &dadData);
int i = 0;
for(; i < current->size(); i++) {
if(i < other->size()) {
(*data)[i] = (current->at(i) & 0xf) + (other->at(i) & 0xf0);
// crossover - ensure it exists in both parents genes or we end up collecting all the crossovers and having too many
if(current->at(i) == CROSSOVER_VAL && other->at(i) == CROSSOVER_VAL) {
combo = (combo + 1) % 2;
current = (combo ? &mumData : &dadData);
other = (combo == 0 ? &mumData : &dadData);
}
} else
(*data)[i] = current->at(i);
}
data->resize(i);
mutate(max_size, min_size);
}
void DNA::set_from(const DNA &parent, int max_size, int min_size) {
*data = parent.getData();
mutate(max_size, min_size);
}
void DNA::mutate(int max_size, int min_size) {
// random mutation
if(qrand()/(float)RAND_MAX < mutation) {
// yep, we have a mutation - figure out the type according to the mutation table
float r = qrand()/(float)RAND_MAX;
if(r < default_mutation_table.add_chunk) {
add_chunk(max_size);
return;
}
r -= default_mutation_table.add_chunk;
if(r < default_mutation_table.remove_chunk) {
remove_chunk(min_size);
return;
}
r -= default_mutation_table.remove_chunk;
if(r < default_mutation_table.move_chunk) {
move_chunk();
return;
}
r -= default_mutation_table.move_chunk;
if(r < default_mutation_table.reverse_section) {
reverse_section();
return;
}
r -= default_mutation_table.reverse_section;
if(r < default_mutation_table.swap_bit) {
int pos = qrand() % data->size();
unsigned char mask = 1 << (qrand() % 8);
(*data)[pos] = ((*data)[pos] ^ mask);
return;
}
r -= default_mutation_table.swap_bit;
if(r < default_mutation_table.random_byte) {
int pos = qrand() % data->size();
(*data)[pos] = qrand() % 0xff;
return;
}
r -= default_mutation_table.random_byte;
if(r < default_mutation_table.add_crossover) {
int pos = qrand() % data->size();
(*data)[pos] = CROSSOVER_VAL; // crossover value
return;
}
}
}
void DNA::add_chunk(int max_size) {
if(data->size() >= max_size) return;
int len = qrand() % qMin(max_size - data->size(), data->size()),
pos2 = qrand() % (data->size() - len),
pos1 = qrand() % data->size();
if(len > 0) data->insert(pos1, data->mid(pos2, len));
}
void DNA::remove_chunk(int min_size) {
if(data->size() <= min_size) return;
int len = qrand() % (data->size() - min_size),
pos = qrand() % (data->size() - len);
if(len > 0) data->remove(pos, len);
}
void DNA::move_chunk() {
int len = qrand() % data->size(),
pos1 = qrand() % (data->size() - len),
pos2 = qrand() % (data->size() - len);
if(len > 0) {
QByteArray temp = data->mid(pos1, len);
data->remove(pos1, len);
data->insert(pos2, temp);
}
}
void DNA::reverse_section() {
int len = qrand() % (data->size()),
pos = qrand() % (data->size() - len);
if(len > 0) {
QByteArray temp = data->mid(pos, len);
for(int i = 0; i < temp.size(); i++) {
(*data)[pos + i] = temp.at(temp.size() - 1 - i);
}
}
}