-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathPopulation.cs
452 lines (393 loc) · 13.1 KB
/
Population.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using System;
using System.Collections.Generic;
using System.Linq;
using Amib.Threading;
using HelperSharp;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Crossovers;
using GeneticSharp.Domain.Fitnesses;
using GeneticSharp.Domain.Mutations;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Domain.Selections;
namespace GeneticSharp.Domain.Populations
{
/// <summary>
/// Represents a population of candidate solutions (chromosomes).
/// </summary>
public class Population
{
#region Constants
/// <summary>
/// The default crossover probability.
/// </summary>
public const float DefaultCrossoverProbability = 0.75f;
/// <summary>
/// The default mutation probability.
/// </summary>
public const float DefaultMutationProbability = 0.1f;
#endregion
#region Events
/// <summary>
/// Occurs when generation ran.
/// </summary>
public event EventHandler GenerationRan;
/// <summary>
/// Occurs when best chromosome changed.
/// </summary>
public event EventHandler BestChromosomeChanged;
#endregion
#region Fields
private IChromosome m_adamChromosome;
private SmartThreadPool m_threadPool;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Populations.Population"/> class.
/// </summary>
/// <param name="minSize">The minimum size (chromosomes).</param>
/// <param name="maxSize">The maximum size (chromosomes).</param>
/// <param name="adamChromosome">The original chromosome of all population ;).</param>
/// <param name="fitness">The fitness evaluation function.</param>
/// <param name="selection">The selection operator.</param>
/// <param name="crossover">The crossover operator.</param>
/// <param name="mutation">The mutation operator.</param>
public Population(int minSize,
int maxSize,
IChromosome adamChromosome,
IFitness fitness,
ISelection selection,
ICrossover crossover,
IMutation mutation)
{
if (minSize < 2)
{
throw new ArgumentOutOfRangeException("minSize", "The minimum size for a population is 2 chromosomes.");
}
if (maxSize < minSize)
{
throw new ArgumentOutOfRangeException("maxSize", "The maximum size for a population should be equal or greater than minimum size.");
}
ExceptionHelper.ThrowIfNull("adamChromosome", adamChromosome);
ExceptionHelper.ThrowIfNull("fitness", fitness);
ExceptionHelper.ThrowIfNull("selection", selection);
ExceptionHelper.ThrowIfNull("crossover", crossover);
ExceptionHelper.ThrowIfNull("mutation", mutation);
MinSize = minSize;
MaxSize = maxSize;
m_adamChromosome = adamChromosome;
Fitness = fitness;
Selection = selection;
Crossover = crossover;
Mutation = mutation;
Generations = new List<Generation> ();
CrossoverProbability = DefaultCrossoverProbability;
MutationProbability = DefaultMutationProbability;
}
#endregion
#region Properties
/// <summary>
/// Gets the generations.
/// </summary>
/// <value>The generations.</value>
public IList<Generation> Generations { get; private set; }
/// <summary>
/// Gets the current generation.
/// </summary>
/// <value>The current generation.</value>
public Generation CurrentGeneration { get; private set; }
/// <summary>
/// Gets the minimum size.
/// </summary>
/// <value>The minimum size.</value>
public int MinSize { get; private set; }
/// <summary>
/// Gets the size of the max.
/// </summary>
/// <value>The size of the max.</value>
public int MaxSize { get; private set; }
/// <summary>
/// Gets the fitness function.
/// </summary>
public IFitness Fitness { get; private set; }
/// <summary>
/// Gets the selection operator.
/// </summary>
public ISelection Selection { get; private set; }
/// <summary>
/// Gets the crossover operator.
/// </summary>
/// <value>The crossover.</value>
public ICrossover Crossover { get; private set; }
/// <summary>
/// Gets or sets the crossover probability.
/// </summary>
public float CrossoverProbability { get; set; }
/// <summary>
/// Gets the mutation operator.
/// </summary>
public IMutation Mutation { get; private set; }
/// <summary>
/// Gets or sets the mutation probability.
/// </summary>
public float MutationProbability { get; set; }
/// <summary>
/// Gets the best chromosome.
/// </summary>
/// <value>The best chromosome.</value>
public IChromosome BestChromosome { get; private set; }
#endregion
#region Public methods
/// <summary>
/// Runs a generation.
/// </summary>
/// <param name="timeout">The timeout to run the generation.</param>
public void RunGeneration(int timeout = 0)
{
if (Generations.Count == 0) {
CurrentGeneration = CreateNewGeneration (CreateInitialChromosomes ());
EvaluateFitness (timeout);
CurrentGeneration.Chromosomes = SelectParents ();
} else {
EvaluateFitness (timeout);
CurrentGeneration = CreateNewGeneration(SelectParents ());
}
Mutate (Cross ());
EvaluateFitness(timeout);
ElectBestChromosome();
FinalizeGeneration();
if (GenerationRan != null)
{
GenerationRan(this, EventArgs.Empty);
}
}
/// <summary>
/// Runs the generations.
/// </summary>
/// <param name="generations">The number of generations to run.</param>
/// <param name="timeoutPerGeneration">Timeout per generation.</param>
public void RunGenerations(int generations, int timeoutPerGeneration = 0)
{
for (var i = 0; i < generations; i++) {
RunGeneration (timeoutPerGeneration);
}
}
/// <summary>
/// Aborts the generation.
/// </summary>
/// <param name="timeout">Timeout to wait to abort.</param>
public void AbortGeneration (int timeout = 60000)
{
if (m_threadPool != null) {
m_threadPool.Shutdown (true, timeout);
}
}
#endregion
#region Private methods
/// <summary>
/// Finalizes the generation.
/// </summary>
private void FinalizeGeneration ()
{
if(CurrentGeneration.Chromosomes.Count > MaxSize)
{
CurrentGeneration.Chromosomes = Selection.SelectChromosomes(MaxSize, CurrentGeneration);
if (!CurrentGeneration.Chromosomes.Any (c => c == CurrentGeneration.BestChromosome)) {
CurrentGeneration.Chromosomes.RemoveAt (CurrentGeneration.Chromosomes.Count - 1);
CurrentGeneration.Chromosomes.Add (CurrentGeneration.BestChromosome);
}
}
}
/// <summary>
/// Creates a new generation.
/// </summary>
/// <returns>The new generation.</returns>
/// <param name="chromosomes">Chromosomes.</param>
private Generation CreateNewGeneration(IList<IChromosome> chromosomes)
{
var g = new Generation (Generations.Count + 1, chromosomes);
Generations.Add (g);
return g;
}
/// <summary>
/// Creates the initial chromosomes.
/// </summary>
/// <returns>The initial chromosomes.</returns>
private IList<IChromosome> CreateInitialChromosomes ()
{
var chromosomes = new List<IChromosome> ();
for(int i = 0; i < MinSize; i++)
{
var c = m_adamChromosome.CreateNew ();
chromosomes.Add (c);
}
return chromosomes;
}
/// <summary>
/// Evaluates the fitness.
/// </summary>
/// <param name="timeout">Timeout.</param>
private void EvaluateFitness(int timeout)
{
if (Fitness.SupportsParallel)
{
EvaluateFitnessParallel(timeout);
}
else
{
EvaluateFitnessLinear(timeout);
}
}
/// <summary>
/// Evaluates the fitness linear.
/// </summary>
/// <param name="timeout">Timeout.</param>
private void EvaluateFitnessLinear(int timeout)
{
var chromosomesWithoutFitness = CurrentGeneration.Chromosomes.Where(c => !c.Fitness.HasValue);
foreach(var c in chromosomesWithoutFitness)
{
c.Fitness = Fitness.Evaluate(c);
if (c.Fitness < 0 || c.Fitness > 1)
{
throw new FitnessException(Fitness, "The {0}.Evaluate returns a fitness with value {1}. The fitness value should be between 0.0 and 1.0."
.With(Fitness.GetType(), c.Fitness));
}
}
}
/// <summary>
/// Evaluates the fitness parallel.
/// </summary>
/// <param name="timeout">Timeout.</param>
private void EvaluateFitnessParallel (int timeout)
{
m_threadPool = new SmartThreadPool();
try {
m_threadPool.MinThreads = MinSize;
m_threadPool.MaxThreads = MinSize;
var chromosomesWithoutFitness = CurrentGeneration.Chromosomes.Where(c => !c.Fitness.HasValue).ToList();
var workItemResults = new IWorkItemResult[chromosomesWithoutFitness.Count];
for (int i = 0; i < chromosomesWithoutFitness.Count; i++)
{
var c = chromosomesWithoutFitness[i];
try
{
workItemResults[i] = m_threadPool.QueueWorkItem(new WorkItemCallback(RunEvaluateFitness), c);
}
catch (Exception ex)
{
throw new InvalidOperationException("Error executing Fitness.Evaluate for chromosome {0}: {1}".With(c.Id, ex.Message), ex);
}
}
m_threadPool.Start ();
if(!m_threadPool.WaitForIdle (timeout == 0 ? int.MaxValue : timeout))
{
throw new TimeoutException("The RunGeneration reach the {0} milliseconds timeout.".With(timeout));
}
foreach (var wi in workItemResults)
{
Exception ex;
wi.GetResult(out ex);
if (ex != null)
{
throw ex;
}
}
foreach (var c in chromosomesWithoutFitness)
{
if (c.Fitness < 0 || c.Fitness > 1)
{
throw new FitnessException(Fitness, "The {0}.Evaluate returns a fitness with value {1}. The fitness value should be between 0.0 and 1.0."
.With(Fitness.GetType(), c.Fitness));
}
}
}
finally {
m_threadPool.Shutdown(true, 1000);
}
}
/// <summary>
/// Runs the evaluate fitness.
/// </summary>
/// <returns>The evaluate fitness.</returns>
/// <param name="state">State.</param>
private object RunEvaluateFitness(object state)
{
var c = state as IChromosome;
try
{
c.Fitness = Fitness.Evaluate(c);
}
catch (Exception ex)
{
throw new FitnessException(Fitness, "Error executing Fitness.Evaluate for chromosome {0}: {1}".With(c.Id, ex.Message), ex);
}
return c.Fitness;
}
/// <summary>
/// Elects the best chromosome.
/// </summary>
private void ElectBestChromosome()
{
var newBestChromosome = CurrentGeneration.Chromosomes.OrderByDescending(c => c.Fitness.Value).First();
ValidateBestChromosome (newBestChromosome);
CurrentGeneration.BestChromosome = newBestChromosome;
if (newBestChromosome != BestChromosome) {
BestChromosome = newBestChromosome;
if (BestChromosomeChanged != null) {
BestChromosomeChanged (this, EventArgs.Empty);
}
}
}
/// <summary>
/// Validates the best chromosome.
/// </summary>
/// <param name="chromosome">Chromosome.</param>
private void ValidateBestChromosome(IChromosome chromosome)
{
if (!chromosome.Fitness.HasValue) {
throw new InvalidOperationException (
"There is unknown problem in current population, because BestChromosome should have a Fitness value. BestChromosome: Id:{0}, age: {1} and length: {2}"
.With (chromosome.Id, chromosome.Age, chromosome.Length));
}
}
/// <summary>
/// Selects the parents.
/// </summary>
/// <returns>The parents.</returns>
private IList<IChromosome> SelectParents ()
{
return Selection.SelectChromosomes (MinSize, CurrentGeneration);
}
/// <summary>
/// Cross this instance.
/// </summary>
private IList<IChromosome> Cross ()
{
var children = new List<IChromosome>();
for ( int i = 0; i < MinSize; i += Crossover.ParentsNumber )
{
if (RandomizationProvider.Current.GetDouble() <= CrossoverProbability)
{
var child = Crossover.Cross (CurrentGeneration.Chromosomes.Skip(i).Take(Crossover.ParentsNumber).ToList());
children.AddRange (child);
}
}
foreach (var c in children) {
CurrentGeneration.Chromosomes.Add (c);
}
return children;
}
/// <summary>
/// Mutate the specified chromosomes.
/// </summary>
/// <param name="chromosomes">Chromosomes.</param>
private void Mutate (IList<IChromosome> chromosomes)
{
foreach(var c in chromosomes)
{
Mutation.Mutate (c, MutationProbability);
}
}
#endregion
}
}