-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitFlipIndividual.cs
339 lines (269 loc) · 10.7 KB
/
BitFlipIndividual.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BitFlipIndividual : Individual
{
public int multiplier = 10;
private int chromosomeSize;
private int[] chromosome1;
private bool[] chromosome2;
public BitFlipIndividual (int size, int mult) : base (size)
{
multiplier = mult;
chromosomeSize = (int)(size / multiplier);
chromosome1 = new int[chromosomeSize];
chromosome2 = new bool[chromosomeSize];
}
public override void Initialize ()
{
for (int i = 0; i < chromosomeSize; i++) {
chromosome1 [i] = Random.Range (-1, 2);
chromosome2 [i] = (Random.Range (0, 2) == 1);
}
}
/// <summary>
/// Mutate the specified probability.
/// </summary>
/// <param name="probability">Probability from 0.0 to 1.0</param>
public override void Mutate (float probability)
{
//Debug.Log ("MutationType in bitflip = " + mutationType);
//TODO:check how to accept enum values as parameter
//mutationType
//0 -> random
//1 -> swap for int and bitflip for bool
//2 -> swap for everything
//3 -> full bitflip
if (mutationType == 0) {
//Debug.Log ("IN RANDOM MUTATION");
for (int i = 0; i < chromosomeSize; i++) {
if (Random.Range (0f, 1f) < probability) {
chromosome1 [i] = Random.Range (-1, 2);
chromosome2 [i] = (Random.Range (0, 2) == 1);
}
}
} else if (mutationType == 1) {
mixedMutation (probability);
} else if (mutationType == 2) { //swap for int and bool
fullSwapMutation (probability);
} else if (mutationType == 3) {//full bitflip(-like) mutation
fullBitFlipMutation (probability);
}
}
private void mixedMutation (float probability)
{
//Debug.Log ("IN ORDER CHANGING MUTATION");
//order changing mutation
/*
* Theory/thoughs behind this:
* 1.check if mutation is to be applied
* 2.calculate random 'pointers' within the size of the chromosome
* 3.if they are the same or point to the same value, change until they don't point to the same value anymore
* 4.swap values at the pointers
*
*/
if (Random.Range (0f, 1f) <= probability) {
int firstMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int secondMutationSwapElement = firstMutationSwapElement;
//while (secondMutationSwapElement == firstMutationSwapElement)
// secondMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int chromosomeCounter = 0;
while (chromosome1 [firstMutationSwapElement] == chromosome1 [secondMutationSwapElement]) {
secondMutationSwapElement = Random.Range (0, chromosomeSize - 1);
// if it points to the same value in chromosome1, change that value to something random
if (chromosomeCounter == chromosomeSize - 1) {
while (chromosome1 [firstMutationSwapElement] == chromosome1 [secondMutationSwapElement])
chromosome1 [secondMutationSwapElement] = (int)Random.Range (-1, 1);
}
/*
if (chromosomeCounter == chromosomeSize - 1 && chromosome1 [firstMutationSwapElement] == chromosome1 [secondMutationSwapElement]) {
chromosome1 [secondMutationSwapElement] = (int)Random.Range (-1, 1);
} else if (chromosomeCounter == chromosomeSize - 1) {
int temp = chromosome1 [firstMutationSwapElement];
chromosome1 [firstMutationSwapElement] = chromosome1 [secondMutationSwapElement];
chromosome1 [secondMutationSwapElement] = temp;
}
*/
chromosomeCounter++;
}
if (chromosome1 [firstMutationSwapElement] != chromosome1 [secondMutationSwapElement]) {
int temp = chromosome1 [secondMutationSwapElement];
chromosome1 [secondMutationSwapElement] = chromosome1 [firstMutationSwapElement];
chromosome1 [firstMutationSwapElement] = temp;
}
}
//bit flip mutation
for (int i = 0; i < chromosomeSize; i++) {
if (Random.Range (0f, 1f) <= probability) {
//flips values of chromosome2
chromosome2 [i] = chromosome2 [i] ? chromosome2 [i] = false : chromosome2 [i] = true; // switch value of chromosome2[i]
}
}
}
private void fullSwapMutation (float probability)
{
int firstIntMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int secondIntMutationSwapElement = firstIntMutationSwapElement;
while (secondIntMutationSwapElement == firstIntMutationSwapElement)
secondIntMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int firstBoolMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int secondBoolMutationSwapElement = firstBoolMutationSwapElement;
while (secondBoolMutationSwapElement == firstBoolMutationSwapElement)
secondBoolMutationSwapElement = Random.Range (0, chromosomeSize - 1);
int tempInt = chromosome1 [firstIntMutationSwapElement];
bool tempBool = chromosome2 [firstBoolMutationSwapElement];
chromosome1 [firstIntMutationSwapElement] = chromosome1 [secondIntMutationSwapElement];
chromosome1 [secondIntMutationSwapElement] = tempInt;
chromosome2 [firstBoolMutationSwapElement] = chromosome2 [secondBoolMutationSwapElement];
chromosome2 [secondBoolMutationSwapElement] = tempBool;
/*
int chromosomeCounter = 0;
while (chromosome1 [firstIntMutationSwapElement] == chromosome1 [secondIntMutationSwapElement]) {
secondIntMutationSwapElement = Random.Range (0, chromosomeSize - 1);
// if it points to the same value in chromosome1, change that value to something random
if (chromosomeCounter == chromosomeSize - 1) {
while (chromosome1 [firstIntMutationSwapElement] == chromosome1 [secondIntMutationSwapElement])
chromosome1 [secondIntMutationSwapElement] = (int)Random.Range (-1, 1);
}
/*
if (chromosomeCounter == chromosomeSize - 1 && chromosome1 [firstMutationSwapElement] == chromosome1 [secondMutationSwapElement]) {
chromosome1 [secondMutationSwapElement] = (int)Random.Range (-1, 1);
} else if (chromosomeCounter == chromosomeSize - 1) {
int temp = chromosome1 [firstMutationSwapElement];
chromosome1 [firstMutationSwapElement] = chromosome1 [secondMutationSwapElement];
chromosome1 [secondMutationSwapElement] = temp;
}
*/
/*
chromosomeCounter++;
}
chromosomeCounter = 0;
while (chromosome2 [firstBoolMutationSwapElement] == chromosome2 [secondBoolMutationSwapElement]) {
secondBoolMutationSwapElement = Random.Range (0, chromosomeSize - 1);
if (chromosomeCounter == chromosomeSize - 1) {
while (chromosome2 [firstBoolMutationSwapElement] == chromosome2 [secondBoolMutationSwapElement])
chromosome2 [secondBoolMutationSwapElement] = (Random.Range (0, 2) == 1);
}
chromosomeCounter++;
}
*/
}
private void fullBitFlipMutation (float probability)
{
//Debug.Log ("Full bitflip mutation");
for (int i = 0; i < chromosomeSize; i++) {
if (Random.Range (0f, 1f) <= probability) {
int temp = chromosome1 [i];
while (chromosome1 [i] == temp) {
chromosome1 [i] = Random.Range (-1, 2);
}
}
if (Random.Range (0f, 1f) <= probability) {
chromosome2 [i] = chromosome2 [i] == true ? chromosome2 [i] = false : chromosome2 [i] = true; // switch value of chromosome2[i]
}
}
}
//1 point
public override void Crossover (Individual partner, float probability)
{
switch (crossoverType) {
case 0:
onePointCrossover (partner, probability);
break;
case 1:
n_Crossover (partner, probability);
break;
}
}
private void onePointCrossover (Individual partner, float probability)
{
BitFlipIndividual bitFlipPartner = (BitFlipIndividual)partner;
//go through chromosomes and alternate the according chromosome from parent1 and parent2
int[] newChromosome1 = new int[chromosomeSize];
int[] newPartnerChromosome1 = new int[chromosomeSize];
bool[] newChromosome2 = new bool[chromosomeSize];
bool[] newPartnerChromosome2 = new bool[chromosomeSize];
if (Random.Range (0f, 1f) <= probability) {
int cutoffPoint = Random.Range (0, chromosomeSize);
for (int i = 0; i < cutoffPoint; i++) {
newChromosome1 [i] = chromosome1 [i];
newChromosome2 [i] = chromosome2 [i];
newPartnerChromosome1 [i] = bitFlipPartner.chromosome1 [i];
newPartnerChromosome2 [i] = bitFlipPartner.chromosome2 [i];
}
for (int i = cutoffPoint; i < chromosomeSize; i++) {
newChromosome1 [i] = bitFlipPartner.chromosome1 [i];
newChromosome2 [i] = bitFlipPartner.chromosome2 [i];
newPartnerChromosome1 [i] = chromosome1 [i];
newPartnerChromosome2 [i] = chromosome2 [i];
}
}
chromosome1 = newChromosome1;
chromosome2 = newChromosome2;
bitFlipPartner.chromosome1 = newPartnerChromosome1;
bitFlipPartner.chromosome2 = newPartnerChromosome2;
}
//N_Point Crossover
//Basic theory:
//1: random probability of happening the crossover between 0f and 1f
//Loop through all chromossome pairs
//Pick a random position to cut (n_cuts) ---------> should be dynamically changed in Unity, not in the code
//Create 2 new chromossomes with the two parts cut (Clone?)
private void n_Crossover (Individual partner, float probability)
{
BitFlipIndividual bitFlipPartner = (BitFlipIndividual)partner;
//Debug.Log (n_cuts + " cuts");
if (UnityEngine.Random.Range (0f, 1f) > probability) {
return;
}
int crossoverPoint = Mathf.FloorToInt (chromosomeSize / (n_cuts + 1));
for (int i = crossoverPoint; i < chromosomeSize; i += 2 * crossoverPoint) {
for (int j = i; j < chromosomeSize && j < i + crossoverPoint; j++) {
int temp1 = chromosome1 [j];
bool temp2 = chromosome2 [j];
chromosome1 [j] = bitFlipPartner.chromosome1 [j];
chromosome2 [j] = bitFlipPartner.chromosome2 [j];
bitFlipPartner.chromosome1 [j] = temp1;
bitFlipPartner.chromosome2 [j] = temp2;
}
}
}
public override void Translate ()
{
for (int i = 0; i < chromosomeSize; i++) {
for (int j = 0; j < multiplier; j++) {
horizontalMoves [i * multiplier + j] = chromosome1 [i];
shots [i * multiplier + j] = chromosome2 [i];
}
}
}
public override Individual Clone ()
{
BitFlipIndividual new_ind = new BitFlipIndividual (totalSize, multiplier);
chromosome1.CopyTo (new_ind.chromosome1, 0);
chromosome2.CopyTo (new_ind.chromosome2, 0);
//new_ind.Translate ();
new_ind.fitness = 0.0f;
new_ind.evaluated = false;
return new_ind;
}
public override string ToString ()
{
string res = "[BitFlipIndividual] Chromosome1: [";
for (int i = 0; i < chromosomeSize; i++) {
res += chromosome1 [i].ToString ();
if (i != chromosomeSize - 1) {
res += ",";
}
}
res += "] Chromosome2: [";
for (int i = 0; i < chromosomeSize; i++) {
res += chromosome2 [i].ToString ();
if (i != chromosomeSize - 1) {
res += ",";
}
}
res += "]";
return res;
}
}