-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFromElementsVectorChromosomePopulationGenerator.cs
66 lines (57 loc) · 2.72 KB
/
FromElementsVectorChromosomePopulationGenerator.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
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
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> by using the elements provided to it in its constructor.
/// </summary>
public class FromElementsVectorChromosomePopulationGenerator<T> : IPopulationGenerator
{
private readonly T[] elements;
private readonly int length;
private readonly bool repeatElements;
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> by using the elements in element.
/// </summary>
public FromElementsVectorChromosomePopulationGenerator(T[] elements, int vectorLength, bool repeatElements, IMutationManager<T> mutationManager, IEvaluator evaluator)
{
if (elements.Length == 0)
throw new GeneticAlgorithmException($"{nameof(elements)} is empty");
if (vectorLength <= 0)
throw new GeneticAlgorithmException($"{nameof(vectorLength)} must be greater than 0 (it was {vectorLength})");
if (!repeatElements && elements.Length < vectorLength)
throw new GeneticAlgorithmException($"{nameof(elements)}.Count is only {elements.Length}, while {nameof(vectorLength)} is {vectorLength}.");
this.repeatElements = repeatElements;
length = vectorLength;
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++)
{
var vector = repeatElements
? SelectKRandomElementsRepeating(elements, length)
: elements.SelectKRandomElementsNonRepeating(length).Shuffle(random);
population[i] = new VectorChromosome<T>(vector, mutationManager, evaluator);
}
return population;
}
private T[] SelectKRandomElementsRepeating(T[] allElements, int k)
{
var selectedElements = new T[k];
for (int i = 0; i < k; i++)
selectedElements[i] = allElements[random.Next(allElements.Length)];
return selectedElements;
}
}
}