Changeset 12405 for branches/TerminationCriteria/HeuristicLab.Termination
- Timestamp:
- 05/27/15 14:30:56 (10 years ago)
- Location:
- branches/TerminationCriteria/HeuristicLab.Termination/3.3
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/TerminationCriteria/HeuristicLab.Termination/3.3/ComparisonTerminator.cs
r12402 r12405 30 30 [Item("ComparisonTerminator", "An termination criterion which compares two values.")] 31 31 [StorableClass] 32 public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable {33 public ILookupParameter<T> ValueParameter {34 get { return (ILookupParameter<T>)Parameters[" Value"]; }32 public class ComparisonTerminator<T> : ThresholdTerminator<T> where T : class, IItem, IComparable, new() { 33 public ILookupParameter<T> ComparisonValueParameter { 34 get { return (ILookupParameter<T>)Parameters["ComparisonValue"]; } 35 35 } 36 36 37 private ValueParameter<Comparison> ComparisonParameter {38 get { return ( ValueParameter<Comparison>)Parameters["Comparison"]; }37 private IFixedValueParameter<Comparison> ComparisonParameter { 38 get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; } 39 39 } 40 40 41 public Comparison Comparison {42 get { return ComparisonParameter.Value ; }43 set { ComparisonParameter.Value = value; }41 public ComparisonType Comparison { 42 get { return ComparisonParameter.Value.Value; } 43 set { ComparisonParameter.Value.Value = value; } 44 44 } 45 45 46 46 [StorableConstructor] 47 47 protected ComparisonTerminator(bool deserializing) : base(deserializing) { } 48 [StorableHook(HookType.AfterDeserialization)] 49 private void AfterDeserialization() { 50 Initialize(); 51 } 48 52 protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner) 49 : base(original, cloner) { } 53 : base(original, cloner) { 54 Initialize(); 55 } 50 56 public override IDeepCloneable Clone(Cloner cloner) { 51 57 return new ComparisonTerminator<T>(this, cloner); 52 58 } 53 public ComparisonTerminator() 54 : base() { 55 Parameters.Add(new LookupParameter<T>("Value", "The left side value of the comparison.")); 56 Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(ComparisonType.GreaterOrEqual)) { Hidden = true }); 59 public ComparisonTerminator(T threshold = default(T)) 60 : base(threshold) { 61 Parameters.Add(new LookupParameter<T>("ComparisonValue", "The left side value of the comparison.") { Hidden = false }); 62 Parameters.Add(new FixedValueParameter<Comparison>("Comparison", "The type of comparison.")); 63 Initialize(); 57 64 } 58 public ComparisonTerminator(string leftSideActualName, string rightSideActualName, ComparisonType comparison = ComparisonType.GreaterOrEqual) 59 : this() { 60 ValueParameter.ActualName = leftSideActualName; 61 ThresholdParameter.ActualName = rightSideActualName; 62 Comparison.Value = comparison; 65 public ComparisonTerminator(string comparisonValueActualName, ComparisonType comparison = default(ComparisonType), T threshold = default(T)) 66 : this(threshold) { 67 ComparisonValueParameter.ActualName = comparisonValueActualName; 68 Comparison = comparison; 63 69 } 64 public ComparisonTerminator(string leftSideActualName, T rightSideValue, ComparisonType comparison = ComparisonType.GreaterOrEqual) 65 : this() { 66 ValueParameter.ActualName = leftSideActualName; 67 ThresholdParameter.Value = rightSideValue; 68 Comparison.Value = comparison; 70 71 private void Initialize() { 72 ComparisonParameter.ToStringChanged += (s, a) => OnToStringChanged(); 69 73 } 70 74 71 75 protected override bool CheckTermination() { 72 IComparable lhs = ValueParameter.ActualValue;73 IComparable rhs = ThresholdParameter. ActualValue;76 IComparable lhs = ComparisonValueParameter.ActualValue; 77 IComparable rhs = ThresholdParameter.Value; 74 78 79 return Comparison.Compare(lhs, rhs); 80 } 81 82 public override string ToString() { 83 if (Threshold == null) return Name; 84 else return string.Format("{0} {1} {2}", Name, Comparison.ToSymbol(), ThresholdParameter.Value); 85 } 86 } 87 88 internal static class ComparisonTypeHelper { 89 public static bool Compare(this ComparisonType comparison, IComparable lhs, IComparable rhs) { 75 90 int i = lhs.CompareTo(rhs); 76 switch (Comparison.Value) { 77 case ComparisonType.Less: 78 return i < 0; 79 case ComparisonType.LessOrEqual: 80 return i <= 0; 81 case ComparisonType.Equal: 82 return i == 0; 83 case ComparisonType.GreaterOrEqual: 84 return i >= 0; 85 case ComparisonType.Greater: 86 return i > 0; 87 case ComparisonType.NotEqual: 88 return i != 0; 89 default: 90 throw new InvalidOperationException(Name + ": " + Comparison.Value + " is not supported."); 91 switch (comparison) { 92 case ComparisonType.Less: return i < 0; 93 case ComparisonType.LessOrEqual: return i <= 0; 94 case ComparisonType.Equal: return i == 0; 95 case ComparisonType.GreaterOrEqual: return i >= 0; 96 case ComparisonType.Greater: return i > 0; 97 case ComparisonType.NotEqual: return i != 0; 98 default: throw new InvalidOperationException(comparison + " is not supported."); 99 } 100 } 101 102 public static string ToSymbol(this ComparisonType comparison) { 103 switch (comparison) { 104 case ComparisonType.Less: return "<"; 105 case ComparisonType.LessOrEqual: return "<="; 106 case ComparisonType.Equal: return "="; 107 case ComparisonType.GreaterOrEqual: return ">="; 108 case ComparisonType.Greater: return ">"; 109 case ComparisonType.NotEqual: return "!="; 110 default: throw new InvalidOperationException(comparison + " is not supported."); 91 111 } 92 112 } -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/ExecutionTimeTerminator.cs
r12402 r12405 44 44 } 45 45 public ExecutionTimeTerminator(IExecutable executable) 46 : this(executable, new TimeSpan(0, 10, 0)) { } 47 public ExecutionTimeTerminator(IExecutable executable, TimeSpan maximumExecutionTime) { 46 : this(executable, new TimeSpanValue(new TimeSpan(0, 10, 0))) { } 47 public ExecutionTimeTerminator(IExecutable executable, TimeSpanValue maximumExecutionTime) 48 : base(maximumExecutionTime) { 48 49 this.executable = executable; 49 ThresholdParameter.Value = new TimeSpanValue(maximumExecutionTime);50 51 50 Name = "Execution Time"; 52 51 } … … 57 56 return executable.ExecutionTime > max; 58 57 } 58 59 public override string ToString() { 60 if (Threshold == null) return Name; 61 else return string.Format("{0} {1} {2}", Name, ">", ThresholdParameter.Value); 62 } 59 63 } 60 64 } -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/MultiTerminator.cs
r12402 r12405 35 35 public sealed class MultiTerminator : CheckedMultiOperator<ITerminator>, ITerminator { 36 36 public static new Image StaticItemImage { 37 get { return HeuristicLab.Common.Resources.VSImageLibrary. Object; }37 get { return HeuristicLab.Common.Resources.VSImageLibrary.FlagRed; } 38 38 } 39 39 public override Image ItemImage { -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/TerminationOperator.cs
r12404 r12405 31 31 [StorableClass] 32 32 public sealed class TerminationOperator : InstrumentedOperator { 33 public ILookupParameter<ITerminator> Terminat orParameter {34 get { return (ILookupParameter<ITerminator>)Parameters["Terminat or"]; }33 public ILookupParameter<ITerminator> TerminationParameter { 34 get { return (ILookupParameter<ITerminator>)Parameters["Termination"]; } 35 35 } 36 36 public ILookupParameter<BoolValue> TerminateParameter { … … 64 64 public TerminationOperator() 65 65 : base() { 66 Parameters.Add(new LookupParameter<ITerminator>("Terminat or", "The termination criteria which sould be checked."));66 Parameters.Add(new LookupParameter<ITerminator>("Termination", "The termination criteria which sould be checked.")); 67 67 Parameters.Add(new LookupParameter<BoolValue>("Terminate", "The parameter which will be set to determine if execution should be terminated or schould continue.")); 68 68 Parameters.Add(new OperatorParameter("ContinueBranch", "The operator which is executed if no termination criteria has met.")); … … 74 74 75 75 var placeholder = new Placeholder() { Name = "Check termination criteria (Placeholder)" }; 76 placeholder.OperatorParameter.ActualName = Terminat orParameter.Name;76 placeholder.OperatorParameter.ActualName = TerminationParameter.Name; 77 77 78 78 BeforeExecutionOperators.Add(assigner); -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/Terminator.cs
r12402 r12405 20 20 #endregion 21 21 22 using System.Drawing; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 31 32 [StorableClass] 32 33 public abstract class Terminator : InstrumentedOperator, ITerminator { 34 public static new Image StaticItemImage { 35 get { return HeuristicLab.Common.Resources.VSImageLibrary.FlagRed; } 36 } 37 33 38 public ILookupParameter<BoolValue> TerminateParameter { 34 39 get { return (ILookupParameter<BoolValue>)Parameters["Terminate"]; } -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/ThresholdTerminator.cs
r12402 r12405 29 29 [Item("ThresholdTerminator", "Base class for all termination criteria which specifies some threshold.")] 30 30 [StorableClass] 31 public abstract class ThresholdTerminator<T> : Terminator where T : class, IItem { 32 public IValueLookupParameter<T> ThresholdParameter { 33 get { return (IValueLookupParameter<T>)Parameters["Threshold"]; } 31 public abstract class ThresholdTerminator<T> : Terminator where T : class, IItem, new() { 32 public IFixedValueParameter<T> ThresholdParameter { 33 get { return (IFixedValueParameter<T>)Parameters["Threshold"]; } 34 } 35 36 public T Threshold { 37 get { return ThresholdParameter.Value; } 34 38 } 35 39 … … 44 48 Initialize(); 45 49 } 46 protected ThresholdTerminator( )50 protected ThresholdTerminator(T threshold = default(T)) 47 51 : base() { 48 Parameters.Add(new ValueLookupParameter<T>("Threshold", "The limit of the termiation criterion.")); 49 52 Parameters.Add(new FixedValueParameter<T>("Threshold", "The limit of the termiation criterion.", threshold)); 50 53 Initialize(); 51 54 } -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/Views/ThresholdTerminatorView.Designer.cs
r12404 r12405 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Core; 24 22 25 namespace HeuristicLab.Termination.Views { 23 partial class ThresholdTerminatorView<T> {26 partial class ThresholdTerminatorView<T> where T : class, IItem, IComparable, new() { 24 27 /// <summary> 25 28 /// Required designer variable. -
branches/TerminationCriteria/HeuristicLab.Termination/3.3/Views/ThresholdTerminatorView.cs
r12402 r12405 29 29 [View("ThresholdTerminator View")] 30 30 [Content(typeof(ThresholdTerminator<>), true)] 31 public partial class ThresholdTerminatorView<T> : ItemView where T : class, IItem, IComparable {31 public partial class ThresholdTerminatorView<T> : ItemView where T : class, IItem, IComparable, new() { 32 32 33 33 public new ThresholdTerminator<T> Content {
Note: See TracChangeset
for help on using the changeset viewer.