Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/14/15 14:22:41 (10 years ago)
Author:
pfleck
Message:

#2027 Made a genetic ComparisonTerminationCriterion instead of the MaximumIterationsTerminationCriterion.

Location:
branches/TerminationCriteria/HeuristicLab.Termination/3.3
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/ComparisonTerminationCriterion.cs

    r12303 r12310  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
    25 using HeuristicLab.Optimization;
    2626using HeuristicLab.Parameters;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828
    2929namespace HeuristicLab.Termination {
    30   [Item("MaximumIterationsTerminationCriterion", "")]
     30  [Item("ComparisonTerminationCriterion", "")]
    3131  [StorableClass]
    32   public sealed class MaximumIterationsTerminationCriterion : TerminationCriterion, IIterationBasedOperator {
    33     public ILookupParameter<IntValue> IterationsParameter {
    34       get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
     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"]; }
    3541    }
    3642
    37     public IValueLookupParameter<IntValue> MaximumIterationsParameter {
    38       get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
     43    public Comparison Comparison {
     44      get { return ComparisonParameter.Value; }
     45      set { ComparisonParameter.Value = value; }
    3946    }
    4047
    4148    [StorableConstructor]
    42     private MaximumIterationsTerminationCriterion(bool deserializing) : base(deserializing) { }
    43     private MaximumIterationsTerminationCriterion(MaximumIterationsTerminationCriterion original, Cloner cloner)
     49    private ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { }
     50    private ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner)
    4451      : base(original, cloner) {
    4552    }
    4653    public override IDeepCloneable Clone(Cloner cloner) {
    47       return new MaximumIterationsTerminationCriterion(this, cloner);
     54      return new ComparisonTerminationCriterion<T>(this, cloner);
    4855    }
    49     public MaximumIterationsTerminationCriterion()
     56    public ComparisonTerminationCriterion()
    5057      : base() {
    51       Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration"));
    52       Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run."));
     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;
    5373    }
    5474
    5575    protected override bool CheckTermination() {
    56       return IterationsParameter.ActualValue.Value >= MaximumIterationsParameter.ActualValue.Value;
     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      }
    5796    }
    5897  }
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/HeuristicLab.Termination-3.3.csproj

    r12303 r12310  
    119119  <ItemGroup>
    120120    <Compile Include="ITerminationCriterion.cs" />
    121     <Compile Include="MaximumIterationsTerminationCriterion.cs" />
     121    <Compile Include="ComparisonTerminationCriterion.cs" />
    122122    <Compile Include="MultiTerminationCriterion.cs" />
    123123    <Compile Include="TerminationCriterion.cs" />
Note: See TracChangeset for help on using the changeset viewer.