-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleIndividual.cs
183 lines (147 loc) · 4.79 KB
/
ExampleIndividual.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Linq;
public class ExampleIndividual : Individual
{
public int multiplier = 10;
private int chromosomeSize;
private int[] chromosome1;
private bool[] chromosome2;
private Dictionary<float, float> trackPoints;
private object info;
public ExampleIndividual (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);
}
}
public override void Mutate (float probability)
{
if (mutationType == 0) {
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 {
throw new System.NotImplementedException ();
}
}
public override void Crossover (Individual partner, float probability)
{
//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?)
ExampleIndividual bitFlipPartner = (ExampleIndividual)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 ()
{
ExampleIndividual new_ind = new ExampleIndividual (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;
//new_ind.trackPoints = new Dictionary<float,float> (this.trackPoints);
return new_ind;
}
public override string ToString ()
{
string res = "[ExampleIndividual] 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;
}
/*
//N_Point Crossover
//Needs complete overhaul
//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?)
void N_Crossover (Individual partner, float probability) {
if (UnityEngine.Random.Range(0f, 1f) > probability) {
return;
}
//this example always splits the chromosome in half
int crossoverPoint = Mathf.FloorToInt(chromosomeSize / n_cuts);
List<Individual> selectedChroms = new List<Individual>();
for (int j = 0; j< probability; j+= crossoverPoint*2)
{
for (int i = j; (i<(j+crossoverPoint))||(i< chromosomeSize); i++)
{
float tmp = trackPoints[[i]];
trackPoints[[i]] = partner.Count[selectedChroms[i]];
partner.trackPoints[selectedChroms[i]] = tmp;
}
return new_ind;
}
}
*/
/* FLAWED IMPLEMENTATION
void nCrossover (Individual partner, float probability, int cutsNum)
{
int[] cutoffpoints = new int[cutsNum];
int counter = 0;
while (cutoffpoints [cutsNum - 1] == null) {
cutoffpoints [counter] = Random.Range (0, chromosomeSize);
bool isRepeated = cutoffpoints.Length != cutoffpoints.Distinct ().Count ();
while (isRepeated) {
cutoffpoints [counter] = Random.Range (0, chromosomeSize);
}
counter++;
}
}
*/
}