#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", "")] [StorableClass] public sealed class ComparisonTerminationCriterion : TerminationCriterion where T : class, IItem, IComparable { public ILookupParameter LeftSideParameter { get { return (ILookupParameter)Parameters["LeftSide"]; } } public IValueLookupParameter RightSideParameter { get { return (IValueLookupParameter)Parameters["RightSide"]; } } private ValueParameter ComparisonParameter { get { return (ValueParameter)Parameters["Comparison"]; } } public Comparison Comparison { get { return ComparisonParameter.Value; } set { ComparisonParameter.Value = value; } } [StorableConstructor] private ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { } private 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("LeftSide", "The left side of the comparison.")); Parameters.Add(new ValueLookupParameter("RightSide", "The right side of the comparison.")); Parameters.Add(new ValueParameter("Comparison", "The type of comparison.", new Comparison(ComparisonType.Equal)) { Hidden = true }); } public ComparisonTerminationCriterion(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual) : this() { LeftSideParameter.ActualName = leftSideActualName; RightSideParameter.ActualName = rightSideActualName; Comparison.Value = comparison; } public ComparisonTerminationCriterion(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual) : this() { LeftSideParameter.ActualName = leftSideActualName; RightSideParameter.Value = rightSideValue; Comparison.Value = comparison; } protected override bool CheckTermination() { IComparable lhs = LeftSideParameter.ActualValue; IComparable rhs = RightSideParameter.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."); } } } }