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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Termination {
|
---|
30 | [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, new() {
|
---|
33 | public ILookupParameter<T> ComparisonValueParameter {
|
---|
34 | get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private IFixedValueParameter<Comparison> ComparisonParameter {
|
---|
38 | get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ComparisonType Comparison {
|
---|
42 | get { return ComparisonParameter.Value.Value; }
|
---|
43 | set { ComparisonParameter.Value.Value = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
|
---|
48 | [StorableHook(HookType.AfterDeserialization)]
|
---|
49 | private void AfterDeserialization() {
|
---|
50 | Initialize();
|
---|
51 | }
|
---|
52 | protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | Initialize();
|
---|
55 | }
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new ComparisonTerminator<T>(this, cloner);
|
---|
58 | }
|
---|
59 | public ComparisonTerminator(T threshold = default(T))
|
---|
60 | : base(threshold) {
|
---|
61 | Parameters.Add(new LookupParameter<T>("ComparisonValue", "The left side value of the comparison.") { Hidden = false });
|
---|
62 | Parameters.Add(new FixedValueParameter<Comparison>("Comparison", "The type of comparison."));
|
---|
63 | Initialize();
|
---|
64 | }
|
---|
65 | public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison = default(ComparisonType), T threshold = default(T))
|
---|
66 | : this(threshold) {
|
---|
67 | ComparisonValueParameter.ActualName = comparisonValueActualName;
|
---|
68 | Comparison = comparison;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void Initialize() {
|
---|
72 | ComparisonParameter.ToStringChanged += (s, a) => OnToStringChanged();
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override bool CheckTermination() {
|
---|
76 | IComparable lhs = ComparisonValueParameter.ActualValue;
|
---|
77 | IComparable rhs = ThresholdParameter.Value;
|
---|
78 |
|
---|
79 | return Comparison.Compare(lhs, rhs);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override string ToString() {
|
---|
83 | if (Threshold == null) return Name;
|
---|
84 | else return string.Format("{0} {1} {2}", Name, Comparison.ToSymbol(), ThresholdParameter.Value);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | internal static class ComparisonTypeHelper {
|
---|
89 | public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) {
|
---|
90 | int i = lhs.CompareTo(rhs);
|
---|
91 | switch (comparison) {
|
---|
92 | case ComparisonType.Less: return i < 0;
|
---|
93 | case ComparisonType.LessOrEqual: return i <= 0;
|
---|
94 | case ComparisonType.Equal: return i == 0;
|
---|
95 | case ComparisonType.GreaterOrEqual: return i >= 0;
|
---|
96 | case ComparisonType.Greater: return i > 0;
|
---|
97 | case ComparisonType.NotEqual: return i != 0;
|
---|
98 | default: throw new InvalidOperationException(comparison + " is not supported.");
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static string ToSymbol(this ComparisonType comparison) {
|
---|
103 | switch (comparison) {
|
---|
104 | case ComparisonType.Less: return "<";
|
---|
105 | case ComparisonType.LessOrEqual: return "<=";
|
---|
106 | case ComparisonType.Equal: return "=";
|
---|
107 | case ComparisonType.GreaterOrEqual: return ">=";
|
---|
108 | case ComparisonType.Greater: return ">";
|
---|
109 | case ComparisonType.NotEqual: return "!=";
|
---|
110 | default: throw new InvalidOperationException(comparison + " is not supported.");
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } |
---|