Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12411 was 12411, checked in by pfleck, 9 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
RevLine 
[12303]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
[12310]22using System;
[12303]23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Termination {
[12402]30  [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
[12303]31  [StorableClass]
[12408]32  public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, IStringConvertibleValue, new() {
[12405]33    public ILookupParameter<T> ComparisonValueParameter {
34      get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; }
[12303]35    }
[12366]36
[12405]37    private IFixedValueParameter<Comparison> ComparisonParameter {
38      get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
[12310]39    }
[12303]40
[12405]41    public ComparisonType Comparison {
42      get { return ComparisonParameter.Value.Value; }
43      set { ComparisonParameter.Value.Value = value; }
[12303]44    }
45
46    [StorableConstructor]
[12402]47    protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
[12405]48    [StorableHook(HookType.AfterDeserialization)]
49    private void AfterDeserialization() {
50      Initialize();
51    }
[12402]52    protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
[12405]53      : base(original, cloner) {
54      Initialize();
55    }
[12303]56    public override IDeepCloneable Clone(Cloner cloner) {
[12402]57      return new ComparisonTerminator<T>(this, cloner);
[12303]58    }
[12408]59
60    public ComparisonTerminator()
61      : this(new T()) { }
[12407]62    public ComparisonTerminator(T threshold)
[12405]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();
[12303]67    }
[12407]68    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, T threshold)
[12405]69      : this(threshold) {
70      ComparisonValueParameter.ActualName = comparisonValueActualName;
71      Comparison = comparison;
[12310]72    }
[12411]73    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, IFixedValueParameter<T> thresholdParameter)
74      : this() {
75      ComparisonValueParameter.ActualName = comparisonValueActualName;
76      Comparison = comparison;
77      ThresholdParameter = thresholdParameter;
78    }
[12405]79
80    private void Initialize() {
[12408]81      ComparisonParameter.Value.ValueChanged += new EventHandler(Comparison_ValueChanged);
[12310]82    }
[12303]83
[12408]84    private void Comparison_ValueChanged(object sender, EventArgs e) {
85      OnComparisonChanged();
86    }
87    protected virtual void OnComparisonChanged() {
88      OnToStringChanged();
89    }
90
[12407]91    protected override bool CheckContinueCriterion() {
[12405]92      IComparable lhs = ComparisonValueParameter.ActualValue;
93      IComparable rhs = ThresholdParameter.Value;
[12310]94
[12405]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) {
[12310]106      int i = lhs.CompareTo(rhs);
[12405]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.");
[12310]115      }
[12303]116    }
[12405]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    }
[12303]129  }
130}
Note: See TracBrowser for help on using the repository browser.