Changeset 12405


Ignore:
Timestamp:
05/27/15 14:30:56 (4 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
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/TerminationCriteria/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithm.cs

    r12404 r12405  
    116116      get { return (IFixedValueParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
    117117    }
    118     private IValueParameter<MultiTerminator> TerminatorsParameter {
    119       get { return (IValueParameter<MultiTerminator>)Parameters["Terminator"]; }
     118    private IValueParameter<MultiTerminator> TerminationParameter {
     119      get { return (IValueParameter<MultiTerminator>)Parameters["Termination"]; }
    120120    }
    121121    #endregion
     
    204204    }
    205205    private MultiTerminator Terminators {
    206       get { return TerminatorsParameter.Value; }
     206      get { return TerminationParameter.Value; }
    207207    }
    208208    private RandomCreator RandomCreator {
     
    287287      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
    288288      Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
    289       Parameters.Add(new ValueParameter<MultiTerminator>("Terminator", "The termination criteria which sould be checked.", new MultiTerminator()));
     289      Parameters.Add(new ValueParameter<MultiTerminator>("Termination", "The termination criteria which sould be checked.", new MultiTerminator()));
    290290
    291291      RandomCreator randomCreator = new RandomCreator();
     
    351351      UpdateAnalyzers();
    352352
    353       generationsTerminator = new ComparisonTerminator<IntValue>("Generations", new IntValue(50), ComparisonType.GreaterOrEqual) { Name = "Generations" };
    354       evaluatedSolutionsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", "MaximumEvaluatedSolutions", ComparisonType.GreaterOrEqual) { Name = "Evaluations" };
    355       selectionPressureTerminator = new ComparisonTerminator<DoubleValue>("SelectionPressure", "MaximumSelectionPressure", ComparisonType.GreaterOrEqual) { Name = "Selection Pressure" };
    356       qualityTerminator = new ComparisonTerminator<DoubleValue>() { Name = "Quality" };
    357       executionTimeTerminator = new ExecutionTimeTerminator(this, TimeSpan.FromMinutes(5)) { Name = "Execution Time" };
     353      generationsTerminator = new ComparisonTerminator<IntValue>("Generations", ComparisonType.GreaterOrEqual, new IntValue(50)) { Name = "Generations" };
     354      evaluatedSolutionsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.GreaterOrEqual, new IntValue(int.MaxValue)) { Name = "Evaluations" };
     355      selectionPressureTerminator = new ComparisonTerminator<DoubleValue>("SelectionPressure", ComparisonType.GreaterOrEqual, new DoubleValue(200)) { Name = "Selection Pressure" };
     356      qualityTerminator = new ComparisonTerminator<DoubleValue>(threshold: new DoubleValue()) { Name = "Quality" };
     357      executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5))) { Name = "Execution Time" };
    358358      ParameterizeTerminators();
    359359      UpdateTerminators();
     
    508508        if (maximizationParameter != null) {
    509509          bool maximization = maximizationParameter.Value.Value;
    510           qualityTerminator.Comparison = new Comparison(maximization ? ComparisonType.GreaterOrEqual : ComparisonType.LessOrEqual);
    511           qualityTerminator.ThresholdParameter.Value = new DoubleValue(maximization ? double.MaxValue : double.MinValue);
     510          qualityTerminator.Comparison = maximization ? ComparisonType.GreaterOrEqual : ComparisonType.LessOrEqual;
     511          qualityTerminator.Threshold.Value = maximization ? double.MaxValue : double.MinValue;
    512512        }
    513513      }
    514       qualityTerminator.ValueParameter.ActualName = qualityAnalyzer.CurrentBestQualityParameter.Name;
     514      qualityTerminator.ComparisonValueParameter.ActualName = qualityAnalyzer.CurrentBestQualityParameter.Name;
    515515    }
    516516    private void ParameterizeComparisonFactorModifiers() {
  • 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.