forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TplPopulation.cs
54 lines (47 loc) · 1.78 KB
/
TplPopulation.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GeneticSharp
{
/// <summary>
/// Represents a population of candidate solutions (chromosomes) using TPL to create them.
/// </summary>
public class TplPopulation : Population
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Populations.TplPopulation"/> 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>
public TplPopulation(int minSize, int maxSize, IChromosome adamChromosome) : base(minSize, maxSize, adamChromosome)
{
}
#endregion
#region Public methods
/// <summary>
/// Creates the initial generation.
/// </summary>
public override void CreateInitialGeneration()
{
Generations = new List<Generation>();
GenerationsNumber = 0;
var chromosomes = new ConcurrentBag<IChromosome>();
Parallel.For(0, MinSize, i =>
{
var c = AdamChromosome.CreateNew();
if (c == null)
{
throw new InvalidOperationException("The Adam chromosome's 'CreateNew' method generated a null chromosome. This is a invalid behavior, please, check your chromosome code.");
}
c.ValidateGenes();
chromosomes.Add(c);
});
CreateNewGeneration(chromosomes.ToList());
}
#endregion
}
}