Changeset 12310 for branches/TerminationCriteria/HeuristicLab.Termination
- Timestamp:
- 04/14/15 14:22:41 (10 years ago)
- 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 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Data; 25 using HeuristicLab.Optimization;26 26 using HeuristicLab.Parameters; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 28 29 29 namespace HeuristicLab.Termination { 30 [Item(" MaximumIterationsTerminationCriterion", "")]30 [Item("ComparisonTerminationCriterion", "")] 31 31 [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"]; } 35 41 } 36 42 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; } 39 46 } 40 47 41 48 [StorableConstructor] 42 private MaximumIterationsTerminationCriterion(bool deserializing) : base(deserializing) { }43 private MaximumIterationsTerminationCriterion(MaximumIterationsTerminationCriterionoriginal, Cloner cloner)49 private ComparisonTerminationCriterion(bool deserializing) : base(deserializing) { } 50 private ComparisonTerminationCriterion(ComparisonTerminationCriterion<T> original, Cloner cloner) 44 51 : base(original, cloner) { 45 52 } 46 53 public override IDeepCloneable Clone(Cloner cloner) { 47 return new MaximumIterationsTerminationCriterion(this, cloner);54 return new ComparisonTerminationCriterion<T>(this, cloner); 48 55 } 49 public MaximumIterationsTerminationCriterion()56 public ComparisonTerminationCriterion() 50 57 : 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; 53 73 } 54 74 55 75 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 } 57 96 } 58 97 } -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/HeuristicLab.Termination-3.3.csproj
r12303 r12310 119 119 <ItemGroup> 120 120 <Compile Include="ITerminationCriterion.cs" /> 121 <Compile Include=" MaximumIterationsTerminationCriterion.cs" />121 <Compile Include="ComparisonTerminationCriterion.cs" /> 122 122 <Compile Include="MultiTerminationCriterion.cs" /> 123 123 <Compile Include="TerminationCriterion.cs" />
Note: See TracChangeset
for help on using the changeset viewer.