-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
LogicalOperatorTerminationBase.cs
101 lines (90 loc) · 3.61 KB
/
LogicalOperatorTerminationBase.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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// A base class for logical operator terminations.
/// </summary>
public abstract class LogicalOperatorTerminationBase : ITermination
{
#region Fields
private readonly int m_minOperands;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LogicalOperatorTerminationBase"/> class.
/// </summary>
/// <param name="minOperands">The minimum number of operands.</param>
protected LogicalOperatorTerminationBase(int minOperands)
{
m_minOperands = minOperands;
Terminations = new List<ITermination>();
}
/// <summary>
/// Initializes a new instance of the <see cref="LogicalOperatorTerminationBase"/> class.
/// </summary>
/// <param name="terminations">The terminations.</param>
protected LogicalOperatorTerminationBase(params ITermination[] terminations)
: this(2)
{
if (terminations != null)
{
for (int i = 0; i < terminations.Length; i++)
{
AddTermination(terminations[i]);
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets the terminations.
/// </summary>
protected IList<ITermination> Terminations { get; private set; }
#endregion
#region Properties
/// <summary>
/// Adds the termination.
/// </summary>
/// <param name="termination">The termination.</param>
public void AddTermination(ITermination termination)
{
ExceptionHelper.ThrowIfNull("termination", termination);
Terminations.Add(termination);
}
/// <summary>
/// Determines whether the specified geneticAlgorithm reached the termination condition.
/// </summary>
/// <param name="geneticAlgorithm">The genetic algorithm.</param>
/// <returns>
/// True if termination has been reached, otherwise false.
/// </returns>
public bool HasReached(IGeneticAlgorithm geneticAlgorithm)
{
ExceptionHelper.ThrowIfNull("geneticAlgorithm", geneticAlgorithm);
if (Terminations.Count < m_minOperands)
{
throw new InvalidOperationException("The {0} needs at least {1} terminations to perform. Please, add the missing terminations.".With(GetType().Name, m_minOperands));
}
return PerformHasReached(geneticAlgorithm);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GeneticSharp.LogicalOperatorTerminationBase"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GeneticSharp.LogicalOperatorTerminationBase"/>.</returns>
public override string ToString()
{
return "{0} ({1})".With(GetType().Name, String.Join(", ", Terminations.Select(t => t.ToString()).ToArray()));
}
/// <summary>
/// Determines whether the specified geneticAlgorithm reached the termination condition.
/// </summary>
/// <param name="geneticAlgorithm">The genetic algorithm.</param>
/// <returns>
/// True if termination has been reached, otherwise false.
/// </returns>
protected abstract bool PerformHasReached(IGeneticAlgorithm geneticAlgorithm);
#endregion
}
}