[12303] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12303] | 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] | 22 | using System;
|
---|
[12413] | 23 | using System.Collections.Generic;
|
---|
[12303] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[12529] | 30 | namespace HeuristicLab.Optimization {
|
---|
[12402] | 31 | [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
|
---|
[12303] | 32 | [StorableClass]
|
---|
[12408] | 33 | public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, IStringConvertibleValue, new() {
|
---|
[12405] | 34 | public ILookupParameter<T> ComparisonValueParameter {
|
---|
| 35 | get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; }
|
---|
[12303] | 36 | }
|
---|
[12366] | 37 |
|
---|
[12405] | 38 | private IFixedValueParameter<Comparison> ComparisonParameter {
|
---|
| 39 | get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
|
---|
[12310] | 40 | }
|
---|
[12303] | 41 |
|
---|
[12447] | 42 | public ComparisonType ComparisonType {
|
---|
[12405] | 43 | get { return ComparisonParameter.Value.Value; }
|
---|
| 44 | set { ComparisonParameter.Value.Value = value; }
|
---|
[12303] | 45 | }
|
---|
| 46 |
|
---|
| 47 | [StorableConstructor]
|
---|
[12402] | 48 | protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
|
---|
[12405] | 49 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 50 | private void AfterDeserialization() {
|
---|
| 51 | Initialize();
|
---|
| 52 | }
|
---|
[12402] | 53 | protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
|
---|
[12405] | 54 | : base(original, cloner) {
|
---|
| 55 | Initialize();
|
---|
| 56 | }
|
---|
[12303] | 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[12402] | 58 | return new ComparisonTerminator<T>(this, cloner);
|
---|
[12303] | 59 | }
|
---|
[12408] | 60 |
|
---|
| 61 | public ComparisonTerminator()
|
---|
| 62 | : this(new T()) { }
|
---|
[12407] | 63 | public ComparisonTerminator(T threshold)
|
---|
[12405] | 64 | : base(threshold) {
|
---|
| 65 | Parameters.Add(new LookupParameter<T>("ComparisonValue", "The left side value of the comparison.") { Hidden = false });
|
---|
| 66 | Parameters.Add(new FixedValueParameter<Comparison>("Comparison", "The type of comparison."));
|
---|
| 67 | Initialize();
|
---|
[12303] | 68 | }
|
---|
[12447] | 69 | public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparisonType, T threshold)
|
---|
[12405] | 70 | : this(threshold) {
|
---|
| 71 | ComparisonValueParameter.ActualName = comparisonValueActualName;
|
---|
[12447] | 72 | ComparisonType = comparisonType;
|
---|
[12310] | 73 | }
|
---|
[12447] | 74 | public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparisonType, IFixedValueParameter<T> thresholdParameter)
|
---|
[12411] | 75 | : this() {
|
---|
| 76 | ComparisonValueParameter.ActualName = comparisonValueActualName;
|
---|
[12447] | 77 | ComparisonType = comparisonType;
|
---|
[12411] | 78 | ThresholdParameter = thresholdParameter;
|
---|
| 79 | }
|
---|
[12405] | 80 |
|
---|
[12436] | 81 | protected override bool CheckContinueCriterion() {
|
---|
| 82 | IComparable lhs = ComparisonValueParameter.ActualValue;
|
---|
| 83 | IComparable rhs = ThresholdParameter.Value;
|
---|
| 84 |
|
---|
[12447] | 85 | return ComparisonType.Compare(lhs, rhs);
|
---|
[12436] | 86 | }
|
---|
| 87 |
|
---|
[12405] | 88 | private void Initialize() {
|
---|
[12447] | 89 | ComparisonParameter.Value.ValueChanged += new EventHandler(ComparisonType_ValueChanged);
|
---|
[12310] | 90 | }
|
---|
[12447] | 91 | private void ComparisonType_ValueChanged(object sender, EventArgs e) {
|
---|
| 92 | OnComparisonTypeChanged();
|
---|
[12408] | 93 | }
|
---|
[12447] | 94 | protected virtual void OnComparisonTypeChanged() {
|
---|
[12408] | 95 | OnToStringChanged();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[12436] | 98 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 99 | base.CollectParameterValues(values);
|
---|
| 100 | values.Add(ComparisonValueParameter.Name, new StringValue(ComparisonValueParameter.ActualName));
|
---|
[12405] | 101 | }
|
---|
| 102 |
|
---|
| 103 | public override string ToString() {
|
---|
| 104 | if (Threshold == null) return Name;
|
---|
[12447] | 105 | else return string.Format("{0} {1} {2}", Name, ComparisonType.ToSymbol(), ThresholdParameter.Value);
|
---|
[12405] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | internal static class ComparisonTypeHelper {
|
---|
| 110 | public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) {
|
---|
[12310] | 111 | int i = lhs.CompareTo(rhs);
|
---|
[12405] | 112 | switch (comparison) {
|
---|
| 113 | case ComparisonType.Less: return i < 0;
|
---|
| 114 | case ComparisonType.LessOrEqual: return i <= 0;
|
---|
| 115 | case ComparisonType.Equal: return i == 0;
|
---|
| 116 | case ComparisonType.GreaterOrEqual: return i >= 0;
|
---|
| 117 | case ComparisonType.Greater: return i > 0;
|
---|
| 118 | case ComparisonType.NotEqual: return i != 0;
|
---|
| 119 | default: throw new InvalidOperationException(comparison + " is not supported.");
|
---|
[12310] | 120 | }
|
---|
[12303] | 121 | }
|
---|
[12405] | 122 |
|
---|
| 123 | public static string ToSymbol(this ComparisonType comparison) {
|
---|
| 124 | switch (comparison) {
|
---|
| 125 | case ComparisonType.Less: return "<";
|
---|
| 126 | case ComparisonType.LessOrEqual: return "<=";
|
---|
| 127 | case ComparisonType.Equal: return "=";
|
---|
| 128 | case ComparisonType.GreaterOrEqual: return ">=";
|
---|
| 129 | case ComparisonType.Greater: return ">";
|
---|
| 130 | case ComparisonType.NotEqual: return "!=";
|
---|
| 131 | default: throw new InvalidOperationException(comparison + " is not supported.");
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
[12303] | 134 | }
|
---|
| 135 | } |
---|