-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVectorChromosome.cs
46 lines (35 loc) · 1.37 KB
/
VectorChromosome.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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.Components.Chromosomes
{
public class VectorChromosome<T> : IChromosome, IEnumerable<T>
{
private T[] vector;
private readonly IMutationManager<T> mutationManager;
private readonly IEvaluator evaluator;
public VectorChromosome(T[] vector, IMutationManager<T> mutationManager, IEvaluator evaluator)
{
this.vector = vector;
this.mutationManager = mutationManager;
this.evaluator = evaluator;
}
public double Evaluate() => evaluator.Evaluate(this);
public void Mutate() => vector = mutationManager.Mutate(vector);
public T[] GetVector() => vector;
public T this[int index] => vector[index];
public int Length => vector.Length;
public IEnumerator<T> GetEnumerator() => vector.Cast<T>().GetEnumerator();
public override string ToString()
{
var stringBuilder = new StringBuilder();
foreach (var value in vector)
stringBuilder.Append(value + ", ");
return stringBuilder.ToString();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}