#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Termination {
[Item("ComparisonTerminationCriterion", "An termination criterion which compares two values.")]
[StorableClass]
public class ComparisonTerminationCriterion : ThresholdTerminationCriterion where T : class, IItem, IComparable {
public ILookupParameter ValueParameter {
get { return (ILookupParameter)Parameters["Value"]; }
}
private ValueParameter ComparisonParameter {
get { return (ValueParameter)Parameters["Comparison"]; }
}
public Comparison Comparison {
get { return ComparisonParameter.Value; }
set { ComparisonParameter.Value = value; }
}
[StorableConstructor]
protected ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
protected ComparisonTerminationCriterion(ComparisonTerminationCriterion original, Cloner cloner)
: base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new ComparisonTerminationCriterion(this, cloner);
}
public ComparisonTerminationCriterion()
: base() {
Parameters.Add(new LookupParameter("Value", "The left side value of the comparison."));
Parameters.Add(new ValueParameter("Comparison", "The type of comparison.", new Comparison(ComparisonType.GreaterOrEqual)) { Hidden = true });
}
public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual)
: this() {
ValueParameter.ActualName = leftSideActualName;
ThresholdParameter.ActualName = rightSideActualName;
Comparison.Value = comparison;
}
public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual)
: this() {
ValueParameter.ActualName = leftSideActualName;
ThresholdParameter.Value = rightSideValue;
Comparison.Value = comparison;
}
protected override bool CheckTermination() {
IComparable lhs = ValueParameter.ActualValue;
IComparable rhs = ThresholdParameter.ActualValue;
int i = lhs.CompareTo(rhs);
switch (Comparison.Value) {
case ComparisonType.Less:
return i < 0;
case ComparisonType.LessOrEqual:
return i <= 0;
case ComparisonType.Equal:
return i == 0;
case ComparisonType.GreaterOrEqual:
return i >= 0;
case ComparisonType.Greater:
return i > 0;
case ComparisonType.NotEqual:
return i != 0;
default:
throw new InvalidOperationException(Name + ": " + Comparison.Value + " is not supported.");
}
}
}
}