-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.cs
More file actions
128 lines (101 loc) · 3.33 KB
/
Copy pathGeneticAlgorithm.cs
File metadata and controls
128 lines (101 loc) · 3.33 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
namespace GeneticAlgorithm {
public class GeneticAlgorithm {
public Population Population { get; protected set; }
public Action<Generation> OnAfterIteration;
public IChromosome BestChromosome { get; protected set; }
public bool IsFinished { get; protected set; }
protected GeneticAlgorithmOptions options;
private double lastFitness;
private bool initialized;
private int unchangedGenerationsCount;
public GeneticAlgorithm(GeneticAlgorithmOptions options) {
Population = new Population(options.GenerationSize, options.ChromosomeFactory);
this.options = options;
OnAfterIteration = options.OnAfterIteration;
}
protected void Init() {
if (initialized) {
return;
}
Population.CreateInitialGeneration(options.Validator);
Population.CurrentGeneration.EvaluateFitness(options.Fitness);
Population.CurrentGeneration.EvaluateBestChromosome();
initialized = true;
}
public void Run() {
if (IsFinished) {
throw new Exception("Genetic algorithm is finished");
}
Init();
while (!IsFinished) {
IterateGeneration();
};
}
public bool RunIterations(int iterationsCount) {
if (IsFinished) {
throw new Exception("Genetic algorithm is finished");
}
Init();
int i = 0;
while (!IsFinished && i < iterationsCount) {
IterateGeneration();
i++;
};
return !IsFinished;
}
private void UpdateGenerationCount() {
double bestFitness = BestChromosome.Fitness.Value;
if (lastFitness == bestFitness) {
unchangedGenerationsCount++;
} else {
unchangedGenerationsCount = 1;
}
lastFitness = bestFitness;
}
private bool IterateGeneration() {
var parents = Population.CurrentGeneration.Chromosomes;
var children = Cross(parents);
Mutate(children);
var filteredChildren = children.Where(c => options.Validator.Validate(c));
EvaluateFitness(filteredChildren);
var newGeneration = options.Selection.SelectChromosomes(Population.GenerationSize, parents.Concat(filteredChildren).ToList());
Population.CreateNewGeneration(newGeneration);
BestChromosome = Population.CurrentGeneration.BestChromosome;
UpdateGenerationCount();
OnAfterIteration.Invoke(Population.CurrentGeneration);
UpdateIsFinished();
return IsFinished;
}
protected void EvaluateFitness(IEnumerable<IChromosome> chromosomes) {
foreach (var chromosome in chromosomes) {
if (!chromosome.Fitness.HasValue) {
chromosome.Fitness = options.Fitness.СalculateFitness(chromosome);
}
}
}
protected void UpdateIsFinished() {
IsFinished = (unchangedGenerationsCount >= options.TerminateUnchangedGenetarionsCount);
}
protected IList<IChromosome> Cross(IList<IChromosome> parents) {
var offspring = new List<IChromosome>();
foreach (var parent in parents) {
if (options.Random.NextDouble() <= options.CrossoverProbability) {
var parent2 = options.ParentSelection.ChooseParent(parent, parents);
var children = options.Crossover.Crossover(parent, parent2);
offspring.AddRange(children);
}
}
return offspring;
}
protected void Mutate(IList<IChromosome> chromosomes) {
foreach (var chromosome in chromosomes) {
if (options.Random.NextDouble() <= options.MutationProbability) {
options.Mutation.Mutate(chromosome);
}
}
}
}
}