forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultOperatorsStrategy.cs
50 lines (45 loc) · 1.89 KB
/
DefaultOperatorsStrategy.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
using System.Collections.Generic;
namespace GeneticSharp
{
/// <summary>
/// Defines an operators strategy which use a linear execution
/// </summary>
public class DefaultOperatorsStrategy : OperatorsStrategyBase
{
/// <summary>
/// Crosses the specified parents.
/// </summary>
/// <param name="population">the population from which parents are selected</param>
/// <param name="crossover">The crossover class.</param>
/// <param name="crossoverProbability">The crossover probability.</param>
/// <param name="parents">The parents.</param>
/// <returns>The result chromosomes.</returns>
public override IList<IChromosome> Cross(IPopulation population, ICrossover crossover, float crossoverProbability, IList<IChromosome> parents)
{
var minSize = population.MinSize;
var offspring = new List<IChromosome>(minSize);
for (int i = 0; i < minSize; i += crossover.ParentsNumber)
{
var children = SelectParentsAndCross(population, crossover, crossoverProbability, parents, i);
if (children != null)
{
offspring.AddRange(children);
}
}
return offspring;
}
/// <summary>
/// Mutate the specified chromosomes.
/// </summary>
/// <param name="mutation">The mutation class.</param>
/// <param name="mutationProbability">The mutation probability.</param>
/// <param name="chromosomes">The chromosomes.</param>
public override void Mutate(IMutation mutation, float mutationProbability, IList<IChromosome> chromosomes)
{
for (int i = 0; i < chromosomes.Count; i++)
{
mutation.Mutate(chromosomes[i], mutationProbability);
}
}
}
}