Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12394 was 12368, checked in by pfleck, 10 years ago

#2027 Fixed some typos.

File size: 3.8 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 two values.")]
31  [StorableClass]
32  public class ComparisonTerminationCriterion<T> : ThresholdTerminationCriterion<T> where T : class, IItem, IComparable {
33    public ILookupParameter<T> ValueParameter {
34      get { return (ILookupParameter<T>)Parameters["Value"]; }
35    }
36
37    private ValueParameter<Comparison> ComparisonParameter {
38      get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
39    }
40
41    public Comparison Comparison {
42      get { return ComparisonParameter.Value; }
43      set { ComparisonParameter.Value = value; }
44    }
45
46    [StorableConstructor]
47    protected ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
48    protected ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner)
49      : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new ComparisonTerminationCriterion<T>(this, cloner);
52    }
53    public ComparisonTerminationCriterion()
54      : base() {
55      Parameters.Add(new LookupParameter<T>("Value", "The left side value of the comparison."));
56      Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(ComparisonType.GreaterOrEqual)) { Hidden = true });
57    }
58    public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual)
59      : this() {
60      ValueParameter.ActualName = leftSideActualName;
61      ThresholdParameter.ActualName = rightSideActualName;
62      Comparison.Value = comparison;
63    }
64    public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual)
65      : this() {
66      ValueParameter.ActualName = leftSideActualName;
67      ThresholdParameter.Value = rightSideValue;
68      Comparison.Value = comparison;
69    }
70
71    protected override bool CheckTermination() {
72      IComparable lhs = ValueParameter.ActualValue;
73      IComparable rhs = ThresholdParameter.ActualValue;
74
75      int i = lhs.CompareTo(rhs);
76      switch (Comparison.Value) {
77        case ComparisonType.Less:
78          return i < 0;
79        case ComparisonType.LessOrEqual:
80          return i <= 0;
81        case ComparisonType.Equal:
82          return i == 0;
83        case ComparisonType.GreaterOrEqual:
84          return i >= 0;
85        case ComparisonType.Greater:
86          return i > 0;
87        case ComparisonType.NotEqual:
88          return i != 0;
89        default:
90          throw new InvalidOperationException(Name + ": " + Comparison.Value + " is not supported.");
91      }
92    }
93
94
95  }
96}
Note: See TracBrowser for help on using the repository browser.