-
Notifications
You must be signed in to change notification settings - Fork 358
/
GeneticAlgorithm.cs
392 lines (345 loc) · 14.5 KB
/
GeneticAlgorithm.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
/// Author: Samuel Arzt
/// Date: March 2017
#region Includes
using System;
using System.Collections.Generic;
#endregion
/// <summary>
/// Class implementing a modified genetic algorithm
/// </summary>
public class GeneticAlgorithm
{
#region Members
#region Default Parameters
/// <summary>
/// Default min value of inital population parameters.
/// </summary>
public const float DefInitParamMin = -1.0f;
/// <summary>
/// Default max value of initial population parameters.
/// </summary>
public const float DefInitParamMax = 1.0f;
/// <summary>
/// Default probability of a parameter being swapped during crossover.
/// </summary>
public const float DefCrossSwapProb = 0.6f;
/// <summary>
/// Default probability of a parameter being mutated.
/// </summary>
public const float DefMutationProb = 0.3f;
/// <summary>
/// Default amount by which parameters may be mutated.
/// </summary>
public const float DefMutationAmount = 2.0f;
/// <summary>
/// Default percent of genotypes in a new population that are mutated.
/// </summary>
public const float DefMutationPerc = 1.0f;
#endregion
#region Operator Delegates
/// <summary>
/// Method template for methods used to initialise the initial population.
/// </summary>
/// <param name="initialPopulation">The population to be initialised.</param>
public delegate void InitialisationOperator(IEnumerable<Genotype> initialPopulation);
/// <summary>
/// Method template for methods used to evaluate (or start the evluation process of) the current population.
/// </summary>
/// <param name="currentPopulation">The current population.</param>
public delegate void EvaluationOperator(IEnumerable<Genotype> currentPopulation);
/// <summary>
/// Method template for methods used to calculate the fitness value of each genotype of the current population.
/// </summary>
/// <param name="currentPopulation"></param>
public delegate void FitnessCalculation(IEnumerable<Genotype> currentPopulation);
/// <summary>
/// Method template for methods used to select genotypes of the current population and create the intermediate population.
/// </summary>
/// <param name="currentPopulation">The current population,</param>
/// <returns>The intermediate population.</returns>
public delegate List<Genotype> SelectionOperator(List<Genotype> currentPopulation);
/// <summary>
/// Method template for methods used to recombine the intermediate population to generate a new population.
/// </summary>
/// <param name="intermediatePopulation">The intermediate population.</param>
/// <returns>The new population.</returns>
public delegate List<Genotype> RecombinationOperator(List<Genotype> intermediatePopulation, uint newPopulationSize);
/// <summary>
/// Method template for methods used to mutate the new population.
/// </summary>
/// <param name="newPopulation">The mutated new population.</param>
public delegate void MutationOperator(List<Genotype> newPopulation);
/// <summary>
/// Method template for method used to check whether any termination criterion has been met.
/// </summary>
/// <param name="currentPopulation">The current population.</param>
/// <returns>Whether the algorithm shall be terminated.</returns>
public delegate bool CheckTerminationCriterion(IEnumerable<Genotype> currentPopulation);
#endregion
#region Operator Methods
/// <summary>
/// Method used to initialise the initial population.
/// </summary>
public InitialisationOperator InitialisePopulation = DefaultPopulationInitialisation;
/// <summary>
/// Method used to evaluate (or start the evaluation process of) the current population.
/// </summary>
public EvaluationOperator Evaluation = AsyncEvaluation;
/// <summary>
/// Method used to calculate the fitness value of each genotype of the current population.
/// </summary>
public FitnessCalculation FitnessCalculationMethod = DefaultFitnessCalculation;
/// <summary>
/// Method used to select genotypes of the current population and create the intermediate population.
/// </summary>
public SelectionOperator Selection = DefaultSelectionOperator;
/// <summary>
/// Method used to recombine the intermediate population to generate a new population.
/// </summary>
public RecombinationOperator Recombination = DefaultRecombinationOperator;
/// <summary>
/// Method used to mutate the new population.
/// </summary>
public MutationOperator Mutation = DefaultMutationOperator;
/// <summary>
/// Method used to check whether any termination criterion has been met.
/// </summary>
public CheckTerminationCriterion TerminationCriterion = null;
#endregion
private static Random randomizer = new Random();
private List<Genotype> currentPopulation;
/// <summary>
/// The amount of genotypes in a population.
/// </summary>
public uint PopulationSize
{
get;
private set;
}
/// <summary>
/// The amount of generations that have already passed.
/// </summary>
public uint GenerationCount
{
get;
private set;
}
/// <summary>
/// Whether the current population shall be sorted before calling the termination criterion operator.
/// </summary>
public bool SortPopulation
{
get;
private set;
}
/// <summary>
/// Whether the genetic algorithm is currently running.
/// </summary>
public bool Running
{
get;
private set;
}
/// <summary>
/// Event for when the algorithm is eventually terminated.
/// </summary>
public event System.Action<GeneticAlgorithm> AlgorithmTerminated;
/// <summary>
/// Event for when the algorithm has finished fitness calculation. Given parameter is the
/// current population sorted by fitness if sorting is enabled (see <see cref="SortPopulation"/>).
/// </summary>
public event System.Action<IEnumerable<Genotype>> FitnessCalculationFinished;
#endregion
#region Constructors
/// <summary>
/// Initialises a new genetic algorithm instance, creating a initial population of given size with genotype
/// of given parameter count.
/// </summary>
/// <param name="genotypeParamCount">The amount of parameters per genotype.</param>
/// <param name="populationSize">The size of the population.</param>
/// <remarks>
/// The parameters of the genotypes of the inital population are set to the default float value.
/// In order to initialise a population properly, assign a method to <see cref="InitialisePopulation"/>
/// and call <see cref="Start"/> to start the genetic algorithm.
/// </remarks>
public GeneticAlgorithm(uint genotypeParamCount, uint populationSize)
{
this.PopulationSize = populationSize;
//Initialise empty population
currentPopulation = new List<Genotype>((int) populationSize);
for (int i = 0; i < populationSize; i++)
currentPopulation.Add(new Genotype(new float[genotypeParamCount]));
GenerationCount = 1;
SortPopulation = true;
Running = false;
}
#endregion
#region Methods
public void Start()
{
Running = true;
InitialisePopulation(currentPopulation);
Evaluation(currentPopulation);
}
public void EvaluationFinished()
{
//Calculate fitness from evaluation
FitnessCalculationMethod(currentPopulation);
//Sort population if flag was set
if (SortPopulation)
currentPopulation.Sort();
//Fire fitness calculation finished event
if (FitnessCalculationFinished != null)
FitnessCalculationFinished(currentPopulation);
//Check termination criterion
if (TerminationCriterion != null && TerminationCriterion(currentPopulation))
{
Terminate();
return;
}
//Apply Selection
List<Genotype> intermediatePopulation = Selection(currentPopulation);
//Apply Recombination
List<Genotype> newPopulation = Recombination(intermediatePopulation, PopulationSize);
//Apply Mutation
Mutation(newPopulation);
//Set current population to newly generated one and start evaluation again
currentPopulation = newPopulation;
GenerationCount++;
Evaluation(currentPopulation);
}
private void Terminate()
{
Running = false;
if (AlgorithmTerminated != null)
AlgorithmTerminated(this);
}
#region Static Methods
#region Default Operators
/// <summary>
/// Initialises the population by setting each parameter to a random value in the default range.
/// </summary>
/// <param name="population">The population to be initialised.</param>
public static void DefaultPopulationInitialisation(IEnumerable<Genotype> population)
{
//Set parameters to random values in set range
foreach (Genotype genotype in population)
genotype.SetRandomParameters(DefInitParamMin, DefInitParamMax);
}
public static void AsyncEvaluation(IEnumerable<Genotype> currentPopulation)
{
//At this point the async evaluation should be started and after it is finished EvaluationFinished should be called
}
/// <summary>
/// Calculates the fitness of each genotype by the formula: fitness = evaluation / averageEvaluation.
/// </summary>
/// <param name="currentPopulation">The current population.</param>
public static void DefaultFitnessCalculation(IEnumerable<Genotype> currentPopulation)
{
//First calculate average evaluation of whole population
uint populationSize = 0;
float overallEvaluation = 0;
foreach (Genotype genotype in currentPopulation)
{
overallEvaluation += genotype.Evaluation;
populationSize++;
}
float averageEvaluation = overallEvaluation / populationSize;
//Now assign fitness with formula fitness = evaluation / averageEvaluation
foreach (Genotype genotype in currentPopulation)
genotype.Fitness = genotype.Evaluation / averageEvaluation;
}
/// <summary>
/// Only selects the best three genotypes of the current population and copies them to the intermediate population.
/// </summary>
/// <param name="currentPopulation">The current population.</param>
/// <returns>The intermediate population.</returns>
public static List<Genotype> DefaultSelectionOperator(List<Genotype> currentPopulation)
{
List<Genotype> intermediatePopulation = new List<Genotype>();
intermediatePopulation.Add(currentPopulation[0]);
intermediatePopulation.Add(currentPopulation[1]);
intermediatePopulation.Add(currentPopulation[2]);
return intermediatePopulation;
}
/// <summary>
/// Simply crosses the first with the second genotype of the intermediate population until the new
/// population is of desired size.
/// </summary>
/// <param name="intermediatePopulation">The intermediate population that was created from the selection process.</param>
/// <returns>The new population.</returns>
public static List<Genotype> DefaultRecombinationOperator(List<Genotype> intermediatePopulation, uint newPopulationSize)
{
if (intermediatePopulation.Count < 2) throw new ArgumentException("Intermediate population size must be greater than 2 for this operator.");
List<Genotype> newPopulation = new List<Genotype>();
while (newPopulation.Count < newPopulationSize)
{
Genotype offspring1, offspring2;
CompleteCrossover(intermediatePopulation[0], intermediatePopulation[1], DefCrossSwapProb, out offspring1, out offspring2);
newPopulation.Add(offspring1);
if (newPopulation.Count < newPopulationSize)
newPopulation.Add(offspring2);
}
return newPopulation;
}
/// <summary>
/// Simply mutates each genotype with the default mutation probability and amount.
/// </summary>
/// <param name="newPopulation">The mutated new population.</param>
public static void DefaultMutationOperator(List<Genotype> newPopulation)
{
foreach (Genotype genotype in newPopulation)
{
if (randomizer.NextDouble() < DefMutationPerc)
MutateGenotype(genotype, DefMutationProb, DefMutationAmount);
}
}
#endregion
#region Recombination Operators
public static void CompleteCrossover(Genotype parent1, Genotype parent2, float swapChance, out Genotype offspring1, out Genotype offspring2)
{
//Initialise new parameter vectors
int parameterCount = parent1.ParameterCount;
float[] off1Parameters = new float[parameterCount], off2Parameters = new float[parameterCount];
//Iterate over all parameters randomly swapping
for (int i = 0; i < parameterCount; i++)
{
if (randomizer.Next() < swapChance)
{
//Swap parameters
off1Parameters[i] = parent2[i];
off2Parameters[i] = parent1[i];
}
else
{
//Don't swap parameters
off1Parameters[i] = parent1[i];
off2Parameters[i] = parent2[i];
}
}
offspring1 = new Genotype(off1Parameters);
offspring2 = new Genotype(off2Parameters);
}
#endregion
#region Mutation Operators
/// <summary>
/// Mutates the given genotype by adding a random value in range [-mutationAmount, mutationAmount] to each parameter with a probability of mutationProb.
/// </summary>
/// <param name="genotype">The genotype to be mutated.</param>
/// <param name="mutationProb">The probability of a parameter being mutated.</param>
/// <param name="mutationAmount">A parameter may be mutated by an amount in range [-mutationAmount, mutationAmount].</param>
public static void MutateGenotype(Genotype genotype, float mutationProb, float mutationAmount)
{
for (int i = 0; i < genotype.ParameterCount; i++)
{
if (randomizer.NextDouble() < mutationProb)
{
//Mutate by random amount in range [-mutationAmount, mutationAmoun]
genotype[i] += (float)(randomizer.NextDouble() * (mutationAmount * 2) - mutationAmount);
}
}
}
#endregion
#endregion
#endregion
}