Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/Termination/ComparisonTerminator.cs @ 16878

Last change on this file since 16878 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Optimization {
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 ComparisonType {
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 comparisonType, T threshold)
70      : this(threshold) {
71      ComparisonValueParameter.ActualName = comparisonValueActualName;
72      ComparisonType = comparisonType;
73    }
74    public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparisonType, IFixedValueParameter<T> thresholdParameter)
75      : this() {
76      ComparisonValueParameter.ActualName = comparisonValueActualName;
77      ComparisonType = comparisonType;
78      ThresholdParameter = thresholdParameter;
79    }
80
81    protected override bool CheckContinueCriterion() {
82      IComparable lhs = ComparisonValueParameter.ActualValue;
83      IComparable rhs = ThresholdParameter.Value;
84
85      return ComparisonType.Compare(lhs, rhs);
86    }
87
88    private void Initialize() {
89      ComparisonParameter.Value.ValueChanged += new EventHandler(ComparisonType_ValueChanged);
90    }
91    private void ComparisonType_ValueChanged(object sender, EventArgs e) {
92      OnComparisonTypeChanged();
93    }
94    protected virtual void OnComparisonTypeChanged() {
95      OnToStringChanged();
96    }
97
98    public override void CollectParameterValues(IDictionary<string, IItem> values) {
99      base.CollectParameterValues(values);
100      values.Add(ComparisonValueParameter.Name, new StringValue(ComparisonValueParameter.ActualName));
101    }
102
103    public override string ToString() {
104      if (Threshold == null) return Name;
105      else return string.Format("{0} {1} {2}", Name, ComparisonType.ToSymbol(), ThresholdParameter.Value);
106    }
107  }
108
109  internal static class ComparisonTypeHelper {
110    public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) {
111      int i = lhs.CompareTo(rhs);
112      switch (comparison) {
113        case ComparisonType.Less: return i < 0;
114        case ComparisonType.LessOrEqual: return i <= 0;
115        case ComparisonType.Equal: return i == 0;
116        case ComparisonType.GreaterOrEqual: return i >= 0;
117        case ComparisonType.Greater: return i > 0;
118        case ComparisonType.NotEqual: return i != 0;
119        default: throw new InvalidOperationException(comparison + " is not supported.");
120      }
121    }
122
123    public static string ToSymbol(this ComparisonType comparison) {
124      switch (comparison) {
125        case ComparisonType.Less: return "<";
126        case ComparisonType.LessOrEqual: return "<=";
127        case ComparisonType.Equal: return "=";
128        case ComparisonType.GreaterOrEqual: return ">=";
129        case ComparisonType.Greater: return ">";
130        case ComparisonType.NotEqual: return "!=";
131        default: throw new InvalidOperationException(comparison + " is not supported.");
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.