Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12417 was 12413, checked in by pfleck, 10 years ago

#2027 Added some custom values when collecting parameter values of Terminators.

File size: 5.3 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 System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Termination {
31  [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
32  [StorableClass]
33  public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, IStringConvertibleValue, new() {
34    public ILookupParameter<T> ComparisonValueParameter {
35      get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; }
36    }
37
38    private IFixedValueParameter<Comparison> ComparisonParameter {
39      get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
40    }
41
42    public ComparisonType Comparison {
43      get { return ComparisonParameter.Value.Value; }
44      set { ComparisonParameter.Value.Value = value; }
45    }
46
47    [StorableConstructor]
48    protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
49    [StorableHook(HookType.AfterDeserialization)]
50    private void AfterDeserialization() {
51      Initialize();
52    }
53    protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
54      : base(original, cloner) {
55      Initialize();
56    }
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new ComparisonTerminator<T>(this, cloner);
59    }
60
61    public ComparisonTerminator()
62      : this(new T()) { }
63    public ComparisonTerminator(T threshold)
64      : base(threshold) {
65      Parameters.Add(new LookupParameter<T>("ComparisonValue", "The left side value of the comparison.") { Hidden = false });
66      Parameters.Add(new FixedValueParameter<Comparison>("Comparison", "The type of comparison."));
67      Initialize();
68    }
69    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, T threshold)
70      : this(threshold) {
71      ComparisonValueParameter.ActualName = comparisonValueActualName;
72      Comparison = comparison;
73    }
74    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison, IFixedValueParameter<T> thresholdParameter)
75      : this() {
76      ComparisonValueParameter.ActualName = comparisonValueActualName;
77      Comparison = comparison;
78      ThresholdParameter = thresholdParameter;
79    }
80
81    private void Initialize() {
82      ComparisonParameter.Value.ValueChanged += new EventHandler(Comparison_ValueChanged);
83    }
84
85    public override void CollectParameterValues(IDictionary<string, IItem> values) {
86      base.CollectParameterValues(values);
87      values.Add(ComparisonValueParameter.Name, new StringValue(ComparisonValueParameter.ActualName));
88    }
89
90    private void Comparison_ValueChanged(object sender, EventArgs e) {
91      OnComparisonChanged();
92    }
93    protected virtual void OnComparisonChanged() {
94      OnToStringChanged();
95    }
96
97    protected override bool CheckContinueCriterion() {
98      IComparable lhs = ComparisonValueParameter.ActualValue;
99      IComparable rhs = ThresholdParameter.Value;
100
101      return Comparison.Compare(lhs, rhs);
102    }
103
104    public override string ToString() {
105      if (Threshold == null) return Name;
106      else return string.Format("{0} {1} {2}", Name, Comparison.ToSymbol(), ThresholdParameter.Value);
107    }
108  }
109
110  internal static class ComparisonTypeHelper {
111    public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) {
112      int i = lhs.CompareTo(rhs);
113      switch (comparison) {
114        case ComparisonType.Less: return i < 0;
115        case ComparisonType.LessOrEqual: return i <= 0;
116        case ComparisonType.Equal: return i == 0;
117        case ComparisonType.GreaterOrEqual: return i >= 0;
118        case ComparisonType.Greater: return i > 0;
119        case ComparisonType.NotEqual: return i != 0;
120        default: throw new InvalidOperationException(comparison + " is not supported.");
121      }
122    }
123
124    public static string ToSymbol(this ComparisonType comparison) {
125      switch (comparison) {
126        case ComparisonType.Less: return "<";
127        case ComparisonType.LessOrEqual: return "<=";
128        case ComparisonType.Equal: return "=";
129        case ComparisonType.GreaterOrEqual: return ">=";
130        case ComparisonType.Greater: return ">";
131        case ComparisonType.NotEqual: return "!=";
132        default: throw new InvalidOperationException(comparison + " is not supported.");
133      }
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.