-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoubleVectorChromosomePopulationGenerator.cs
60 lines (52 loc) · 2.5 KB
/
DoubleVectorChromosomePopulationGenerator.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
using System;
using System.Collections.Generic;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Exceptions;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.Components.PopulationGenerators
{
public class DoubleVectorChromosomePopulationGenerator : IPopulationGenerator
{
private readonly Random random = new Random();
private readonly int vectorSize;
private readonly double minGenome;
private readonly double range;
private readonly IMutationManager<double> mutationManager;
private readonly IEvaluator evaluator;
/// <summary>
/// This class will create chromosomes of type VectorChromosome<double>
/// </summary>
/// <param name="vectorSize">The size of the generated chromosomes</param>
/// <param name="minGenome">The genomes will be equal to or greater than minGenom</param>
/// <param name="maxGenome">The genomes will be equal to or smaller than minGenom</param>
/// <param name="mutationManager">A mutation manager to use</param>
/// <param name="evaluator">An evaluator to use</param>
public DoubleVectorChromosomePopulationGenerator(int vectorSize, double minGenome, double maxGenome, IMutationManager<double> mutationManager, IEvaluator evaluator)
{
if (vectorSize <= 0)
throw new GeneticAlgorithmException($"{nameof(vectorSize)} must be bigger than 0. It was {vectorSize}");
if (maxGenome < minGenome)
throw new GeneticAlgorithmException($"{nameof(maxGenome)} ({maxGenome}) must be bigger than {nameof(minGenome)} ({minGenome})");
this.vectorSize = vectorSize;
this.minGenome = minGenome;
range = maxGenome - minGenome;
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] = GetChromosome();
return population;
}
private VectorChromosome<double> GetChromosome()
{
var vector = new double[vectorSize];
for (int i = 0; i < vectorSize; i++)
vector[i] = minGenome + random.NextDouble() * range;
return new VectorChromosome<double>(vector, mutationManager, evaluator);
}
}
}