-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStopAtConvergence.cs
42 lines (35 loc) · 1.25 KB
/
StopAtConvergence.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
using GeneticAlgorithm.Exceptions;
using GeneticAlgorithm.Interfaces;
namespace GeneticAlgorithm.StopManagers
{
public class StopAtConvergence : IStopManager
{
private readonly double diff;
/// <summary>
/// Will stop when the difference between the min evaluation and max evaluation is equal to or less than "diff"
/// </summary>
public StopAtConvergence(double diff)
{
if (diff < 0)
throw GeneticAlgorithmArgumentException.SmallerThanZeroException(nameof(diff), diff);
this.diff = diff;
}
public bool ShouldStop(Population population, IEnvironment environment, int generation)
{
var minEvaluation = double.MaxValue;
var maxEvaluation = double.MinValue;
foreach (var evaluation in population.GetEvaluations())
{
if (evaluation < minEvaluation)
minEvaluation = evaluation;
if (evaluation > maxEvaluation)
maxEvaluation = evaluation;
}
return (maxEvaluation - minEvaluation) <= diff;
}
public void AddGeneration(Population population)
{
// Do nothing
}
}
}