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
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 {
[12368]30  [Item("ComparisonTerminationCriterion", "An termination criterion which compares two values.")]
[12303]31  [StorableClass]
[12366]32  public class ComparisonTerminationCriterion<T> : ThresholdTerminationCriterion<T> where T : class, IItem, IComparable {
33    public ILookupParameter<T> ValueParameter {
34      get { return (ILookupParameter<T>)Parameters["Value"]; }
[12303]35    }
[12366]36
[12310]37    private ValueParameter<Comparison> ComparisonParameter {
38      get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
39    }
[12303]40
[12310]41    public Comparison Comparison {
42      get { return ComparisonParameter.Value; }
43      set { ComparisonParameter.Value = value; }
[12303]44    }
45
46    [StorableConstructor]
[12328]47    protected ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
48    protected ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner)
[12366]49      : base(original, cloner) { }
[12303]50    public override IDeepCloneable Clone(Cloner cloner) {
[12310]51      return new ComparisonTerminationCriterion<T>(this, cloner);
[12303]52    }
[12310]53    public ComparisonTerminationCriterion()
[12303]54      : base() {
[12366]55      Parameters.Add(new LookupParameter<T>("Value", "The left side value of the comparison."));
[12328]56      Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(ComparisonType.GreaterOrEqual)) { Hidden = true });
[12303]57    }
[12310]58    public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual)
59      : this() {
[12366]60      ValueParameter.ActualName = leftSideActualName;
61      ThresholdParameter.ActualName = rightSideActualName;
[12310]62      Comparison.Value = comparison;
63    }
64    public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual)
65      : this() {
[12366]66      ValueParameter.ActualName = leftSideActualName;
67      ThresholdParameter.Value = rightSideValue;
[12310]68      Comparison.Value = comparison;
69    }
[12303]70
71    protected override bool CheckTermination() {
[12366]72      IComparable lhs = ValueParameter.ActualValue;
73      IComparable rhs = ThresholdParameter.ActualValue;
[12310]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      }
[12303]92    }
[12364]93
[12366]94
[12303]95  }
96}
Note: See TracBrowser for help on using the repository browser.