-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
GA.cs
395 lines (346 loc) · 12.8 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// All code copyright (c) 2003 Barry Lapthorn
// Website: http://www.lapthorn.net
//
// Disclaimer:
// All code is provided on an "AS IS" basis, without warranty. The author
// makes no representation, or warranty, either express or implied, with
// respect to the code, its quality, accuracy, or fitness for a specific
// purpose. Therefore, the author shall not have any liability to you or any
// other person or entity with respect to any liability, loss, or damage
// caused or alleged to have been caused directly or indirectly by the code
// provided. This includes, but is not limited to, interruption of service,
// loss of data, loss of profits, or consequential damages from the use of
// this code.
//
//
// $Author: barry $
// $Revision: 1.1 $
//
// $Id: GA.cs,v 1.1 2003/08/19 20:59:05 barry Exp $
//
// Modified by Lionel Monnier 30th aug 2004
// Modified by Kory Becker 01-Jan-2013 http://www.primaryobjects.com/kory-becker.aspx
#region Using directives
using AIProgrammer.Repository.Interface;
using AIProgrammer.Types;
using AIProgrammer.Repository.Concrete;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using AIProgrammer.Types.Interface;
#endregion
namespace AIProgrammer.GeneticAlgorithm
{
public delegate double GAFunction(params double[] values);
public delegate void OnGeneration(GA ga);
/// <summary>
/// Genetic Algorithm class
/// </summary>
public class GA : IGeneticAlgorithm
{
public GAParams GAParams { get; set; }
public bool Stop { get; set; }
private DateTime _lastEpoch = DateTime.Now;
/// <summary>
/// Default constructor sets mutation rate to 5%, crossover to 80%, population to 100,
/// and generations to 2000.
/// </summary>
public GA()
{
InitialValues();
GAParams.MutationRate = 0.05;
GAParams.CrossoverRate = 0.80;
GAParams.PopulationSize = 100;
GAParams.GenomeSize = 2000;
}
public GA(double crossoverRate,
double mutationRate,
int populationSize,
int generationSize,
int genomeSize)
{
InitialValues();
GAParams.MutationRate = mutationRate;
GAParams.CrossoverRate = crossoverRate;
GAParams.PopulationSize = populationSize;
GAParams.Generations = generationSize;
GAParams.GenomeSize = genomeSize;
}
public GA(int genomeSize)
{
InitialValues();
GAParams.GenomeSize = genomeSize;
}
public void InitialValues()
{
GAParams = new GAParams();
GAParams.Elitism = false;
}
/// <summary>
/// Method which starts the GA executing.
/// </summary>
public void Go(bool resume = false)
{
/// -------------
/// Preconditions
/// -------------
if (getFitness == null)
throw new ArgumentNullException("Need to supply fitness function");
if (GAParams.GenomeSize == 0)
throw new IndexOutOfRangeException("Genome size not set");
/// -------------
Genome.MutationRate = GAParams.MutationRate;
if (!resume)
{
// Create the fitness table.
GAParams.FitnessTable = new List<double>();
GAParams.ThisGeneration = new List<Genome>(GAParams.Generations);
GAParams.NextGeneration = new List<Genome>(GAParams.Generations);
GAParams.TotalFitness = 0;
GAParams.TargetFitness = 0;
GAParams.TargetFitnessCount = 0;
GAParams.CurrentGeneration = 0;
Stop = false;
CreateGenomes();
RankPopulation();
}
while (GAParams.CurrentGeneration < GAParams.Generations && !Stop)
{
CreateNextGeneration();
double fitness = RankPopulation();
if (GAParams.CurrentGeneration % 100 == 0)
{
Console.WriteLine("Generation " + GAParams.CurrentGeneration + ", Time: " + Math.Round((DateTime.Now - _lastEpoch).TotalSeconds, 2) + "s, Best Fitness: " + fitness);
if (GAParams.HistoryPath != "")
{
// Record history timeline.
File.AppendAllText(GAParams.HistoryPath, DateTime.Now.ToString() + "," + fitness + "," + GAParams.TargetFitness + "," + GAParams.CurrentGeneration + "\r\n");
}
_lastEpoch = DateTime.Now;
}
if (GAParams.TargetFitness > 0 && fitness >= GAParams.TargetFitness)
{
if (GAParams.TargetFitnessCount++ > 500)
break;
}
else
{
GAParams.TargetFitnessCount = 0;
}
if (OnGenerationFunction != null)
{
OnGenerationFunction(this);
}
GAParams.CurrentGeneration++;
}
}
/// <summary>
/// After ranking all the genomes by fitness, use a 'roulette wheel' selection
/// method. This allocates a large probability of selection to those with the
/// highest fitness.
/// </summary>
/// <returns>Random individual biased towards highest fitness</returns>
private int RouletteSelection()
{
double randomFitness = m_random.NextDouble() * (GAParams.FitnessTable[GAParams.FitnessTable.Count - 1] == 0 ? 1 : GAParams.FitnessTable[GAParams.FitnessTable.Count - 1]);
int idx = -1;
int mid;
int first = 0;
int last = GAParams.PopulationSize - 1;
mid = (last - first)/2;
// ArrayList's BinarySearch is for exact values only
// so do this by hand.
while (idx == -1 && first <= last)
{
if (randomFitness < GAParams.FitnessTable[mid])
{
last = mid;
}
else if (randomFitness > GAParams.FitnessTable[mid])
{
first = mid;
}
mid = (first + last)/2;
// lies between i and i+1
if ((last - first) == 1)
idx = last;
}
return idx;
}
/// <summary>
/// Rank population and sort in order of fitness.
/// </summary>
private double RankPopulation()
{
GAParams.TotalFitness = 0.0;
// Calculate fitness for each genome.
Parallel.ForEach(GAParams.ThisGeneration, (g) =>
{
g.Fitness = FitnessFunction(g.Genes());
GAParams.TotalFitness += g.Fitness;
});
GAParams.ThisGeneration.Sort(delegate(Genome x, Genome y) { return Comparer<double>.Default.Compare(x.Fitness, y.Fitness); });
// now sorted in order of fitness.
double fitness = 0.0;
GAParams.FitnessTable.Clear();
foreach (Genome t in GAParams.ThisGeneration)
{
fitness += t.Fitness;
GAParams.FitnessTable.Add(t.Fitness);
}
return GAParams.FitnessTable[GAParams.FitnessTable.Count - 1];
}
/// <summary>
/// Create the *initial* genomes by repeated calling the supplied fitness function
/// </summary>
private void CreateGenomes()
{
for (int i = 0; i < GAParams.PopulationSize; i++)
{
Genome g = new Genome(GAParams.GenomeSize);
GAParams.ThisGeneration.Add(g);
}
}
private void CreateNextGeneration()
{
GAParams.NextGeneration.Clear();
Genome g = null, g2 = null;
int length = GAParams.PopulationSize;
if (GAParams.Elitism)
{
g = GAParams.ThisGeneration[GAParams.PopulationSize - 1].DeepCopy();
g.age = GAParams.ThisGeneration[GAParams.PopulationSize - 1].age;
g2 = GAParams.ThisGeneration[GAParams.PopulationSize - 2].DeepCopy();
g2.age = GAParams.ThisGeneration[GAParams.PopulationSize - 2].age;
length -= 2;
}
for (int i = 0; i < length; i += 2)
{
int pidx1 = RouletteSelection();
int pidx2 = RouletteSelection();
Genome parent1, parent2, child1, child2;
parent1 = GAParams.ThisGeneration[pidx1];
parent2 = GAParams.ThisGeneration[pidx2];
if (m_random.NextDouble() < GAParams.CrossoverRate)
{
parent1.Crossover(ref parent2, out child1, out child2);
}
else
{
child1 = parent1;
child2 = parent2;
}
child1.Mutate();
child2.Mutate();
GAParams.NextGeneration.Add(child1);
GAParams.NextGeneration.Add(child2);
}
if (GAParams.Elitism && g != null)
{
if (g2 != null)
GAParams.NextGeneration.Add(g2);
if (g != null)
GAParams.NextGeneration.Add(g);
}
// Expand genomes.
if (GAParams.NextGeneration[0].Length != GAParams.GenomeSize)
{
Parallel.ForEach(GAParams.NextGeneration, (genome) =>
{
if (genome.Length != GAParams.GenomeSize)
{
genome.Expand(GAParams.GenomeSize);
}
});
}
GAParams.ThisGeneration = new List<Genome>(GAParams.NextGeneration);
/*GAParams.m_thisGeneration.Clear();
foreach (Genome ge in GAParams.m_nextGeneration)
GAParams.m_thisGeneration.Add(ge);*/
}
public void Save(string fileName)
{
ThreadPool.QueueUserWorkItem(new WaitCallback((p) =>
{
try
{
IRepository<GAParams> repository = new GARepository((string)p);
repository.Add(GAParams);
repository.SaveChanges();
}
catch
{
}
}), fileName);
}
public void Load(string fileName)
{
IRepository<GAParams> repository = new GARepository(fileName);
var value = repository.GetAll();
if (value.Count() > 0)
{
GAParams = value.ToList()[0];
Console.WriteLine("Loaded Genetic Algorithm: Crossover " + GAParams.CrossoverRate + ", Mutation Rate " + GAParams.MutationRate + ", Pop Size " + GAParams.PopulationSize + ", Gen Size " + GAParams.Generations + ", Current Gen " + GAParams.CurrentGeneration);
}
}
public void Resume(GAFunction fitnessFunc, OnGeneration onGenerationFunc)
{
FitnessFunction = fitnessFunc;
OnGenerationFunction = onGenerationFunc;
Go(true);
}
static Random m_random = new Random((int)DateTime.Now.Ticks);
static private GAFunction getFitness;
public GAFunction FitnessFunction
{
get
{
return getFitness;
}
set
{
getFitness = value;
}
}
public OnGeneration OnGenerationFunction;
public void GetBest(out double[] values, out double fitness)
{
Genome g = GAParams.ThisGeneration[GAParams.PopulationSize - 1];
values = new double[g.Length];
g.GetValues(ref values);
fitness = g.Fitness;
}
public void GetWorst(out double[] values, out double fitness)
{
GetNthGenome(0, out values, out fitness);
}
public void GetNthGenome(int n, out double[] values, out double fitness)
{
/// Preconditions
/// -------------
if (n < 0 || n > GAParams.PopulationSize - 1)
throw new ArgumentOutOfRangeException("n too large, or too small");
/// -------------
Genome g = GAParams.ThisGeneration[n];
values = new double[g.Length];
g.GetValues(ref values);
fitness = g.Fitness;
}
public void SetNthGenome(int n, double[] values, double fitness)
{
/// Preconditions
/// -------------
if (n < 0 || n > GAParams.PopulationSize - 1)
throw new ArgumentOutOfRangeException("n too large, or too small");
/// -------------
Genome g = GAParams.ThisGeneration[n];
g.m_genes = values;
g.Fitness = fitness;
GAParams.ThisGeneration[n] = g;
}
}
}