-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
TplOperatorsStrategy.cs
52 lines (48 loc) · 2.05 KB
/
TplOperatorsStrategy.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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GeneticSharp
{
/// <summary>
/// An IOperatorsStrategy's implmentation which use Task Parallel Library (TPL) for parallel execution.
/// </summary>
public class TplOperatorsStrategy : 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 offspring = new ConcurrentBag<IChromosome>();
Parallel.ForEach(Enumerable.Range(0, population.MinSize / crossover.ParentsNumber).Select(i => i * crossover.ParentsNumber), i =>
{
var children = SelectParentsAndCross(population, crossover, crossoverProbability, parents, i);
if (children != null)
{
foreach (var item in children)
offspring.Add(item);
}
});
return offspring.ToList();
}
/// <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)
{
Parallel.ForEach(chromosomes, c =>
{
mutation.Mutate(c, mutationProbability);
});
}
}
}