source: branches/TerminationCriteria/HeuristicLab.Termination/3.3/ComparisonTerminator.cs @ 12411

Last change on this file since 12411 was 12411, checked in by pfleck, 4 years ago

#2027

  • Removed MaximumIterationsTerminator and MaximumEvaluatedSolutionsTermimator because they practically do the same.
  • Add the possibility for ThresholdTerminators to use a foreign parameter (e.g. MaximumSelectionPressure). This way the terminator can use parameters which are still relevant for the algorithm and still provide consistent management of the terminators.
  • Renamed IThresholdTerminator to ISingleValueTerminator.
File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Termination {
30  [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
31  [StorableClass]
32  public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, IStringConvertibleValue, new() {
33    public ILookupParameter<T> ComparisonValueParameter {
34      get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; }
35    }
36
37    private IFixedValueParameter<Comparison> ComparisonParameter {
38      get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
39    }
40
41    public ComparisonType Comparison {
42      get { return ComparisonParameter.Value.Value; }
43      set { ComparisonParameter.Value.Value = value; }
44    }
45
46    [StorableConstructor]
47    protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
48    [StorableHook(HookType.AfterDeserialization)]
49    private void AfterDeserialization() {
50      Initialize();
51    }
52    protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
53      : base(original, cloner) {
54      Initialize();
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new ComparisonTerminator<T>(this, cloner);
58    }
59
60    public ComparisonTerminator()
61      : this(new T()) { }
62    public ComparisonTerminator(T threshold)
63      : base(threshold) {
64      Parameters.Add(new LookupParameter<T>("ComparisonValue", "The left side value of the comparison.") { Hidden = false });
65      Parameters.Add(new FixedValueParameter<Comparison>("Comparison", "The type of comparison."));
66      Initialize();
67    }
68    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, T threshold)
69      : this(threshold) {
70      ComparisonValueParameter.ActualName = comparisonValueActualName;
71      Comparison = comparison;
72    }
73    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, IFixedValueParameter<T> thresholdParameter)
74      : this() {
75      ComparisonValueParameter.ActualName = comparisonValueActualName;
76      Comparison = comparison;
77      ThresholdParameter = thresholdParameter;
78    }
79
80    private void Initialize() {
81      ComparisonParameter.Value.ValueChanged += new EventHandler(Comparison_ValueChanged);
82    }
83
84    private void Comparison_ValueChanged(object sender, EventArgs e) {
85      OnComparisonChanged();
86    }
87    protected virtual void OnComparisonChanged() {
88      OnToStringChanged();
89    }
90
91    protected override bool CheckContinueCriterion() {
92      IComparable lhs = ComparisonValueParameter.ActualValue;
93      IComparable rhs = ThresholdParameter.Value;
94
95      return Comparison.Compare(lhs, rhs);
96    }
97
98    public override string ToString() {
99      if (Threshold == null) return Name;
100      else return string.Format("{0} {1} {2}", Name, Comparison.ToSymbol(), ThresholdParameter.Value);
101    }
102  }
103
104  internal static class ComparisonTypeHelper {
105    public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) {
106      int i = lhs.CompareTo(rhs);
107      switch (comparison) {
108        case ComparisonType.Less: return i < 0;
109        case ComparisonType.LessOrEqual: return i <= 0;
110        case ComparisonType.Equal: return i == 0;
111        case ComparisonType.GreaterOrEqual: return i >= 0;
112        case ComparisonType.Greater: return i > 0;
113        case ComparisonType.NotEqual: return i != 0;
114        default: throw new InvalidOperationException(comparison + " is not supported.");
115      }
116    }
117
118    public static string ToSymbol(this ComparisonType comparison) {
119      switch (comparison) {
120        case ComparisonType.Less: return "<";
121        case ComparisonType.LessOrEqual: return "<=";
122        case ComparisonType.Equal: return "=";
123        case ComparisonType.GreaterOrEqual: return ">=";
124        case ComparisonType.Greater: return ">";
125        case ComparisonType.NotEqual: return "!=";
126        default: throw new InvalidOperationException(comparison + " is not supported.");
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.