-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGA.cs
229 lines (181 loc) · 5.87 KB
/
GA.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class GA : MonoBehaviour
{
public float mutatePR=0.5f;
Car[] cars;
int[] childCarsValue;
int geneCount = 10;
float[] fitness;
int[] selected;
int generationCount = 0;
int MAXIMUM_VALUE = 0xFFFFFF;
public Text text_Fitness;
public Text text_generation;
// Start is called before the first frame update
void Start()
{
cars = new Car[geneCount];
for(int i=0; i<geneCount; i++)
{
cars[i] = transform.GetChild(i).gameObject.GetComponent<Car>();
}
fitness = new float[geneCount];
selected = new int[geneCount];
childCarsValue = new int[geneCount];
for (int i = 0; i < geneCount; i++)
{
cars[i].value = Random.Range(0, MAXIMUM_VALUE+1);
cars[i].Initialize();
}
text_Fitness.text = "Fitness : 00.00";
text_generation.text = "Generation : 0";
}
// Update is called once per frame
void Update()
{
if (IsAllDead())
{
CalculateFitness(); // fitness is definated (100-record)*1000
Selection();
CrossOver();
Mutate(mutatePR);// mutatePB : 0.5% means picking 1 in 200 total sample
NextGeneration();
text_Fitness.text = "Fitness : " + getBiggestFitness();
text_generation.text="Generation : "+generationCount++;
}
}
bool IsAllDead()
{
bool result = true;
foreach(Car c in cars)
{
result &= c.dead;
}
return result;
}
void CalculateFitness()
{
for (int i = 0; i < geneCount; i++)
{
fitness[i] = cars[i].record;
}
}
void Selection()
{
for(int i=0; i<geneCount; i++)
{
float rand_roulette = Random.Range(0, 100);
selected[i] = RouletteWheelTable(rand_roulette);
}
}
int RouletteWheelTable(float rand)
{
float fitness_sum = 0;
foreach (float f in fitness)
{
fitness_sum += f;
}
for (int i=0; i<geneCount; i++)
{
float PR = (fitness[i] / fitness_sum) * 100;
if (PR<= rand)
{
rand -= PR;
}
else
{
return i;
}
}
return -1;
}
void CrossOver()
{
int rand_selectedIndex1;
int rand_selectedIndex2;
FileStream FS = new FileStream(@"C:\Users\luraw\OneDrive\바탕 화면\crossOverData.txt", FileMode.Open, FileAccess.Write);
StreamWriter wr = new StreamWriter(FS);
rand_selectedIndex1 = Random.Range(0, geneCount);
rand_selectedIndex2 = Random.Range(0, geneCount);
childCarsValue[0] = cars[selected[rand_selectedIndex1]].value; // to save two of parents
childCarsValue[1] = cars[selected[rand_selectedIndex2]].value;
wr.WriteLine("selected[" + rand_selectedIndex1 + "]");
wr.WriteLine("childvalue" + 0 + " : " + cars[selected[rand_selectedIndex1]].value.ToString("x4"));
wr.WriteLine("selected[" + rand_selectedIndex2 + "]");
wr.WriteLine("childvalue" + 1 + " : " + cars[selected[rand_selectedIndex2]].value.ToString("x4"));
for (int i = 2; i < geneCount; i++)
{
rand_selectedIndex1 = Random.Range(0, geneCount);
rand_selectedIndex2 = Random.Range(0, geneCount);
int cv = (cars[selected[rand_selectedIndex1]].value & 0xCF0F0C) + (cars[selected[rand_selectedIndex2]].value & 0x30F0F3);
childCarsValue[i] = cv;
wr.WriteLine("selected[" + rand_selectedIndex1 + "] crossOver "+"selected["+ rand_selectedIndex2+"]");
wr.WriteLine("childvalue" + i + " : " + cv.ToString("x4"));
}
wr.Close();
FS.Close();
showingCrossOverData();
}
void Mutate(float mutatePR)
{
for(int i=0; i<geneCount; i++)
{
float mutateRand = Random.Range(0, 100.0f);
if (mutateRand < mutatePR)
{
Debug.Log("MUTATE");
int mutate= Random.Range(0, 0xFFFFFF+1);
childCarsValue[i] = mutate;
}
}
}
void NextGeneration()
{
showingGeneData();
for (int i = 0; i < geneCount; i++)
{
cars[i].value = childCarsValue[i];
cars[i].Initialize();
}
}
float getBiggestFitness()
{
float max=fitness[0];
foreach(float f in fitness)
{
if(f > max)
{
max = f;
}
}
return max;
}
void showingCrossOverData()
{
using (StreamWriter wr = new StreamWriter(@"C:\Users\luraw\OneDrive\바탕 화면\selectedData.txt"))
{
for (int i = 0; i < geneCount; i++)
{
wr.WriteLine("car["+i+"] fitness : " + fitness[i]);
}
for (int i = 0; i < geneCount; i++)
{
wr.WriteLine("selected cars ["+i+"] : " + selected[i] + " || "+cars[selected[i]].value.ToString("x4"));
}
}
}
void showingGeneData()
{
using (StreamWriter wr = new StreamWriter(@"C:\Users\luraw\OneDrive\바탕 화면\geneData.txt"))
{
for (int i = 0; i < geneCount; i++)
{
wr.WriteLine(i + " geneData: " + cars[i].value.ToString("x4") +" -> "+childCarsValue[i].ToString("x4"));
}
}
}
}