-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUniformCrossover.cs
55 lines (43 loc) · 1.46 KB
/
UniformCrossover.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Georgia
{
public class UniformCrossover : ICrossoverStrategy
{
public double ParentFavortism { get; set; }
public int ChildrenProduced { get; set; }
public UniformCrossover()
{
}
public UniformCrossover(double favortism)
{
ParentFavortism = favortism;
}
public UniformCrossover(double favortism, int children)
: this(favortism)
{
ChildrenProduced = children;
}
#region ICrossoverStrategy Members
public IList<IChromosome> Recombine(IList<IChromosome> parents)
{
IList<IChromosome> children = new IChromosome[ChildrenProduced];
Random prng = RandomFactory.Instance();
int parent, count = parents.First().Count;
for (int i=0; i<ChildrenProduced; i++)
{
children[i] = parents[i].GetCopy();
for (int j = 0; j < count; j++)
{
// TODO: Generalize to more than 2 parents... but what does ParentFavortism mean then?
parent = (prng.NextDouble() <= ParentFavortism) ? 0 : 1;
children[i][j] = parents[parent][j];
}
}
return children;
}
#endregion
}
}