-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChromosomePool.cs
33 lines (30 loc) · 1.02 KB
/
ChromosomePool.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
using System;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.SelectionStrategies
{
/// <summary>
/// Holds a pool of chromosomes.
/// ChromosomePool give two guarantees:
/// 1) If the pool contains n chromosomes, and the GetChromosome method is called n times, every chromosome will be returns exactly once.
/// 2) The chromosomes will be returned in a random order.
///
/// Note that GetChromosome can only be called chromosomes.Length times
/// </summary>
public class ChromosomePool
{
private readonly IChromosome[] chromosomes;
private int counter = -1;
public ChromosomePool(IChromosome[] chromosomes)
{
this.chromosomes = chromosomes.Shuffle(new Random());
}
/// <summary>
/// Note that GetChromosome can only be called chromosomes.Length times
/// </summary>
public IChromosome GetChromosome()
{
counter++;
return chromosomes[counter];
}
}
}