Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TerminationCriteria/HeuristicLab.Termination/3.3/ComparisonTerminationCriterion.cs @ 12364

Last change on this file since 12364 was 12364, checked in by pfleck, 9 years ago

#2027 Changed a few things to better show single value criteria.

File size: 4.5 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("ComparisonTerminationCriterion", "An termination criterion which compares to values.")]
31  [StorableClass]
32  public class ComparisonTerminationCriterion<T> : TerminationCriterion where T : class, IItem, IComparable {
33    public ILookupParameter<T> LeftSideParameter {
34      get { return (ILookupParameter<T>)Parameters["LeftSide"]; }
35    }
36    public IValueLookupParameter<T> RightSideParameter {
37      get { return (IValueLookupParameter<T>)Parameters["RightSide"]; }
38    }
39    private ValueParameter<Comparison> ComparisonParameter {
40      get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
41    }
42
43    public Comparison Comparison {
44      get { return ComparisonParameter.Value; }
45      set { ComparisonParameter.Value = value; }
46    }
47
48    [StorableConstructor]
49    protected ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      Initialize();
53    }
54    protected ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner)
55      : base(original, cloner) {
56      Initialize();
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new ComparisonTerminationCriterion<T>(this, cloner);
60    }
61    public ComparisonTerminationCriterion()
62      : base() {
63      Parameters.Add(new LookupParameter<T>("LeftSide", "The left side of the comparison."));
64      Parameters.Add(new ValueLookupParameter<T>("RightSide", "The right side of the comparison."));
65      Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(ComparisonType.GreaterOrEqual)) { Hidden = true });
66
67      Initialize();
68    }
69    public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual)
70      : this() {
71      LeftSideParameter.ActualName = leftSideActualName;
72      RightSideParameter.ActualName = rightSideActualName;
73      Comparison.Value = comparison;
74    }
75    public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual)
76      : this() {
77      LeftSideParameter.ActualName = leftSideActualName;
78      RightSideParameter.Value = rightSideValue;
79      Comparison.Value = comparison;
80    }
81
82    private void Initialize() {
83      RightSideParameter.ToStringChanged += (s, a) => OnToStringChanged();
84    }
85
86    protected override bool CheckTermination() {
87      IComparable lhs = LeftSideParameter.ActualValue;
88      IComparable rhs = RightSideParameter.ActualValue;
89
90      int i = lhs.CompareTo(rhs);
91      switch (Comparison.Value) {
92        case ComparisonType.Less:
93          return i < 0;
94        case ComparisonType.LessOrEqual:
95          return i <= 0;
96        case ComparisonType.Equal:
97          return i == 0;
98        case ComparisonType.GreaterOrEqual:
99          return i >= 0;
100        case ComparisonType.Greater:
101          return i > 0;
102        case ComparisonType.NotEqual:
103          return i != 0;
104        default:
105          throw new InvalidOperationException(Name + ": " + Comparison.Value + " is not supported.");
106      }
107    }
108
109    public override string ToString() {
110      if (RightSideParameter.Value == null) return Name;
111      else return Name + ": " + RightSideParameter.Value;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.