-
Notifications
You must be signed in to change notification settings - Fork 3
/
AllElementsVectorChromosomePopulationGenerator.cs
44 lines (38 loc) · 1.68 KB
/
AllElementsVectorChromosomePopulationGenerator.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
using System;
using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Exceptions;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.Components.PopulationGenerators
{
/// <summary>
/// Creates a population of chromosomes of type VectorChromosome<T> in which each chromosome contains every element exactly once.
/// </summary>
public class AllElementsVectorChromosomePopulationGenerator<T> : IPopulationGenerator
{
private readonly ICollection<T> elements;
private readonly IMutationManager<T> mutationManager;
private readonly IEvaluator evaluator;
private readonly Random random = new Random();
/// <summary>
/// Creates a population of chromosomes of type VectorChromosome<T> in which each chromosome contains every element exactly once.
/// </summary>
public AllElementsVectorChromosomePopulationGenerator(ICollection<T> elements, IMutationManager<T> mutationManager, IEvaluator evaluator)
{
if (!elements.Any())
throw new GeneticAlgorithmException($"{nameof(elements)} is empty");
this.elements = elements;
this.mutationManager = mutationManager;
this.evaluator = evaluator;
}
public IEnumerable<IChromosome> GeneratePopulation(int size)
{
var population = new IChromosome[size];
for (int i = 0; i < size; i++)
population[i] = new VectorChromosome<T>(elements.Shuffle(random), mutationManager, evaluator);
return population;
}
}
}