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

Last change on this file since 12310 was 12310, checked in by pfleck, 7 years ago

#2027 Made a genetic ComparisonTerminationCriterion instead of the MaximumIterationsTerminationCriterion.

File size: 4.0 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", "")]
31  [StorableClass]
32  public sealed 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    private ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
50    private ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner)
51      : base(original, cloner) {
52    }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new ComparisonTerminationCriterion<T>(this, cloner);
55    }
56    public ComparisonTerminationCriterion()
57      : base() {
58      Parameters.Add(new LookupParameter<T>("LeftSide", "The left side of the comparison."));
59      Parameters.Add(new ValueLookupParameter<T>("RightSide", "The right side of the comparison."));
60      Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(ComparisonType.Equal)) { Hidden = true });
61    }
62    public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual)
63      : this() {
64      LeftSideParameter.ActualName = leftSideActualName;
65      RightSideParameter.ActualName = rightSideActualName;
66      Comparison.Value = comparison;
67    }
68    public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual)
69      : this() {
70      LeftSideParameter.ActualName = leftSideActualName;
71      RightSideParameter.Value = rightSideValue;
72      Comparison.Value = comparison;
73    }
74
75    protected override bool CheckTermination() {
76      IComparable lhs = LeftSideParameter.ActualValue;
77      IComparable rhs = RightSideParameter.ActualValue;
78
79      int i = lhs.CompareTo(rhs);
80      switch (Comparison.Value) {
81        case ComparisonType.Less:
82          return i < 0;
83        case ComparisonType.LessOrEqual:
84          return i <= 0;
85        case ComparisonType.Equal:
86          return i == 0;
87        case ComparisonType.GreaterOrEqual:
88          return i >= 0;
89        case ComparisonType.Greater:
90          return i > 0;
91        case ComparisonType.NotEqual:
92          return i != 0;
93        default:
94          throw new InvalidOperationException(Name + ": " + Comparison.Value + " is not supported.");
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.