-
Notifications
You must be signed in to change notification settings - Fork 3
/
IntVectorChromosomePopulationGenerator.cs
60 lines (52 loc) · 2.47 KB
/
IntVectorChromosomePopulationGenerator.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 IntVectorChromosomePopulationGenerator : IPopulationGenerator
{
private readonly Random random = new Random();
private readonly int vectorSize;
private readonly int minGenome;
private readonly int maxGenome;
private readonly IMutationManager<int> mutationManager;
private readonly IEvaluator evaluator;
/// <summary>
/// This class will create chromosomes of type VectorChromosome<int>
/// </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 IntVectorChromosomePopulationGenerator(int vectorSize, int minGenome, int maxGenome, IMutationManager<int> 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;
this.maxGenome = maxGenome;
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<int> GetChromosome()
{
var vector = new int[vectorSize];
for (int i = 0; i < vectorSize; i++)
vector[i] = random.Next(minGenome, maxGenome + 1);
return new VectorChromosome<int>(vector, mutationManager, evaluator);
}
}
}