Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/27/15 14:30:56 (10 years ago)
Author:
pfleck
Message:

#2027

  • Uses FixedValueParameters and added some properties.
  • Added the comparison-symbol in the string-representation of Terminators.
  • Renamed Terminator parameter to Termination to use a more neutral term.
  • Used Red-Flag as Termination-Icon.
Location:
branches/TerminationCriteria/HeuristicLab.Termination/3.3
Files:
8 edited

Legend:

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

    r12402 r12405  
    3030  [Item("ComparisonTerminator", "An termination criterion which compares two values.")]
    3131  [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"]; }
    3535    }
    3636
    37     private ValueParameter<Comparison> ComparisonParameter {
    38       get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
     37    private IFixedValueParameter<Comparison> ComparisonParameter {
     38      get { return (IFixedValueParameter<Comparison>)Parameters["Comparison"]; }
    3939    }
    4040
    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; }
    4444    }
    4545
    4646    [StorableConstructor]
    4747    protected ComparisonTerminator(bool deserializing) : base(deserializing) { }
     48    [StorableHook(HookType.AfterDeserialization)]
     49    private void AfterDeserialization() {
     50      Initialize();
     51    }
    4852    protected ComparisonTerminator(ComparisonTerminator<T> original, Cloner cloner)
    49       : base(original, cloner) { }
     53      : base(original, cloner) {
     54      Initialize();
     55    }
    5056    public override IDeepCloneable Clone(Cloner cloner) {
    5157      return new ComparisonTerminator<T>(this, cloner);
    5258    }
    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();
    5764    }
    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;
    6369    }
    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();
    6973    }
    7074
    7175    protected override bool CheckTermination() {
    72       IComparable lhs = ValueParameter.ActualValue;
    73       IComparable rhs = ThresholdParameter.ActualValue;
     76      IComparable lhs = ComparisonValueParameter.ActualValue;
     77      IComparable rhs = ThresholdParameter.Value;
    7478
     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) {
    7590      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.");
    91111      }
    92112    }
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/ExecutionTimeTerminator.cs

    r12402 r12405  
    4444    }
    4545    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) {
    4849      this.executable = executable;
    49       ThresholdParameter.Value = new TimeSpanValue(maximumExecutionTime);
    50 
    5150      Name = "Execution Time";
    5251    }
     
    5756      return executable.ExecutionTime > max;
    5857    }
     58
     59    public override string ToString() {
     60      if (Threshold == null) return Name;
     61      else return string.Format("{0} {1} {2}", Name, ">", ThresholdParameter.Value);
     62    }
    5963  }
    6064}
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/MultiTerminator.cs

    r12402 r12405  
    3535  public sealed class MultiTerminator : CheckedMultiOperator<ITerminator>, ITerminator {
    3636    public static new Image StaticItemImage {
    37       get { return HeuristicLab.Common.Resources.VSImageLibrary.Object; }
     37      get { return HeuristicLab.Common.Resources.VSImageLibrary.FlagRed; }
    3838    }
    3939    public override Image ItemImage {
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/TerminationOperator.cs

    r12404 r12405  
    3131  [StorableClass]
    3232  public sealed class TerminationOperator : InstrumentedOperator {
    33     public ILookupParameter<ITerminator> TerminatorParameter {
    34       get { return (ILookupParameter<ITerminator>)Parameters["Terminator"]; }
     33    public ILookupParameter<ITerminator> TerminationParameter {
     34      get { return (ILookupParameter<ITerminator>)Parameters["Termination"]; }
    3535    }
    3636    public ILookupParameter<BoolValue> TerminateParameter {
     
    6464    public TerminationOperator()
    6565      : base() {
    66       Parameters.Add(new LookupParameter<ITerminator>("Terminator", "The termination criteria which sould be checked."));
     66      Parameters.Add(new LookupParameter<ITerminator>("Termination", "The termination criteria which sould be checked."));
    6767      Parameters.Add(new LookupParameter<BoolValue>("Terminate", "The parameter which will be set to determine if execution should be terminated or schould continue."));
    6868      Parameters.Add(new OperatorParameter("ContinueBranch", "The operator which is executed if no termination criteria has met."));
     
    7474
    7575      var placeholder = new Placeholder() { Name = "Check termination criteria (Placeholder)" };
    76       placeholder.OperatorParameter.ActualName = TerminatorParameter.Name;
     76      placeholder.OperatorParameter.ActualName = TerminationParameter.Name;
    7777
    7878      BeforeExecutionOperators.Add(assigner);
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/Terminator.cs

    r12402 r12405  
    2020#endregion
    2121
     22using System.Drawing;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3132  [StorableClass]
    3233  public abstract class Terminator : InstrumentedOperator, ITerminator {
     34    public static new Image StaticItemImage {
     35      get { return HeuristicLab.Common.Resources.VSImageLibrary.FlagRed; }
     36    }
     37
    3338    public ILookupParameter<BoolValue> TerminateParameter {
    3439      get { return (ILookupParameter<BoolValue>)Parameters["Terminate"]; }
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/ThresholdTerminator.cs

    r12402 r12405  
    2929  [Item("ThresholdTerminator", "Base class for all termination criteria which specifies some threshold.")]
    3030  [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; }
    3438    }
    3539
     
    4448      Initialize();
    4549    }
    46     protected ThresholdTerminator()
     50    protected ThresholdTerminator(T threshold = default(T))
    4751      : 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));
    5053      Initialize();
    5154    }
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/Views/ThresholdTerminatorView.Designer.cs

    r12404 r12405  
    2020#endregion
    2121
     22using System;
     23using HeuristicLab.Core;
     24
    2225namespace HeuristicLab.Termination.Views {
    23   partial class ThresholdTerminatorView<T> {
     26  partial class ThresholdTerminatorView<T> where T : class, IItem, IComparable, new() {
    2427    /// <summary>
    2528    /// Required designer variable.
  • branches/TerminationCriteria/HeuristicLab.Termination/3.3/Views/ThresholdTerminatorView.cs

    r12402 r12405  
    2929  [View("ThresholdTerminator View")]
    3030  [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() {
    3232
    3333    public new ThresholdTerminator<T> Content {
Note: See TracChangeset for help on using the changeset viewer.