-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectionFacade.cs
74 lines (63 loc) · 2.09 KB
/
SelectionFacade.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Georgia
{
/// <summary>
/// Selection subsystem
/// </summary>
/// <remarks>A SelectionFacade manages the selection mechanism for the genetic algorithm. Selection involves determining which Chromosomes are chosen for recombination and then calculating the fitness for chromosomes.</remarks>
public class SelectionFacade
{
private ISelectionStrategy _selectionMechanism;
private IFitnessStrategy _fitnessMechanism;
public SelectionFacade() { }
public SelectionFacade(ISelectionStrategy ss, IFitnessStrategy fs)
{
this._selectionMechanism = ss;
this._fitnessMechanism = fs;
}
public void UpdateFitnessAll(IEnumerable<IChromosome> p)
{
List<IChromosome> needsEvaluated = new List<IChromosome>();
foreach (IChromosome c in p)
{
if (!c.Fitness.HasValue)
{
needsEvaluated.Add(c);
}
}
if (needsEvaluated.Count > 0)
{
this.FitnessMechanism.EvaluatePool(needsEvaluated);
}
}
public IList<IChromosome> Select(IList<IChromosome> population, int count)
{
UpdateFitnessAll(population);
IChromosome[] r = new IChromosome[count];
for (int i=0; i<count; i++) {
r[i] = this.SelectionMechanism.Select(population);
}
return r;
}
public ISelectionStrategy SelectionMechanism
{
set
{
_selectionMechanism = value;
}
get { return _selectionMechanism; }
}
public IFitnessStrategy FitnessMechanism
{
set
{
_fitnessMechanism = value;
}
get { return _fitnessMechanism; }
}
}
}