Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/26/16 09:00:20 (9 years ago)
Author:
abeham
Message:

#2457: added code to auto-sync stopping criterion with embedded algorithm in iterated algorithms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Analysis/3.3/Optimizers/IteratedAlgorithm.cs

    r13564 r13568  
    6161    }
    6262
    63     public IValueParameter<DoubleValue> TargetValueParameter {
    64       get { return (IValueParameter<DoubleValue>)Parameters["TargetValue"]; }
     63    public IValueParameter<DoubleValue> TargetQualityParameter {
     64      get { return (IValueParameter<DoubleValue>)Parameters["TargetQuality"]; }
    6565    }
    6666
     
    131131          RegisterAlgorithmEvents();
    132132          AddAlgorithmAnalyzers();
     133          HandleAlgorithmMaximumEvaluationsChanged();
     134          HandleAlgorithmTargetQualityChanged();
    133135        }
    134136        if (AlgorithmParameter.Value != algorithm)
     
    152154        var bestQuality = Results.ContainsKey(BestQualityResultName) ? ((DoubleValue)Results[BestQualityResultName].Value).Value
    153155                                                                     : (Maximization ? double.MinValue : double.MaxValue);
    154         var targetValue = TargetValueParameter.Value != null ? TargetValueParameter.Value.Value
     156        var targetValue = TargetQualityParameter.Value != null ? TargetQualityParameter.Value.Value
    155157                                                             : Maximization ? double.MaxValue : double.MinValue;
    156158
     
    180182      Parameters.Add(new OptionalValueParameter<TimeSpanValue>("MaximumExecutionTime", "The maximum wall-clock time that the algorithm should run."));
    181183      Parameters.Add(new OptionalValueParameter<IntValue>("MaximumEvaluations", "The maximum number of function evaluations that the algorithm should run.", new IntValue(100000000)));
    182       Parameters.Add(new OptionalValueParameter<DoubleValue>("TargetValue", "The target value that the algorithm should run for."));
     184      Parameters.Add(new OptionalValueParameter<DoubleValue>("TargetQuality", "The target quality that the algorithm should run for."));
    183185      Parameters.Add(new FixedValueParameter<DoubleValue>("MoveCostPerSolution", "The amount of solution evaluation equivalents of a single move. Use 1 for a black-box scenario.", new DoubleValue(1)));
    184186      Parameters.Add(new FixedValueParameter<BoolValue>("StoreSolutionInRun", "Whether the solution data types should be kept in the run."));
     
    203205    private void AfterDeserialization() {
    204206      algorithm = AlgorithmParameter.Value;
     207      // BackwardsCompatibility3.3
     208      #region Backwards compatible code, remove with 3.4
     209      if (Parameters.ContainsKey("TargetValue")) {
     210        var target = ((OptionalValueParameter<DoubleValue>)Parameters["TargetValue"]).Value;
     211        Parameters.Remove("TargetValue");
     212        Parameters.Add(new OptionalValueParameter<DoubleValue>("TargetQuality", "The target quality that the algorithm should run for.", target));
     213      }
    205214      if (!Parameters.ContainsKey("Analyzer")) {
    206215        Parameters.Add(new FixedValueParameter<MultiAnalyzer>("Analyzer", "Analyzers that should be called in addition to the default algorithm analyzers.", new MultiAnalyzer()));
     
    222231        }
    223232      }
     233      if (bestSolutionAnalyzer == null) {
     234        bestSolutionAnalyzer = new BestScopeSolutionAnalyzer();
     235        Analyzer.Operators.Add(bestSolutionAnalyzer, StoreSolutionInRun);
     236      }
     237      #endregion
    224238      RegisterEventHandlers();
    225239    }
     
    245259      Algorithm.Stopped += Algorithm_Stopped;
    246260      Algorithm.ProblemChanged += Algorithm_ProblemChanged;
    247     }
     261      #region MaximumEvaluations
     262      IParameter param;
     263      if (Algorithm.Parameters.TryGetValue("MaximumEvaluations", out param)) {
     264        var intFixedValueParam = (param as IFixedValueParameter<IntValue>);
     265        if (intFixedValueParam != null) {
     266          intFixedValueParam.Value.ValueChanged += AlgorithmMaximumEvaluationsOnValueChanged;
     267        } else {
     268          var intValueParam = (param as IValueParameter<IntValue>);
     269          if (intValueParam != null) {
     270            intValueParam.ValueChanged += AlgorithmMaximumEvaluationsParameterOnChanged;
     271            if (intValueParam.Value != null) intValueParam.Value.ValueChanged += AlgorithmMaximumEvaluationsOnValueChanged;
     272          }
     273        }
     274      }
     275      #endregion
     276      #region TargetQuality
     277      if (Algorithm.Parameters.TryGetValue("TargetQuality", out param)) {
     278        var intValueParam = (param as IValueParameter<IntValue>);
     279        if (intValueParam != null) {
     280          intValueParam.ValueChanged += AlgorithmTargetQualityParameterOnChanged;
     281          if (intValueParam.Value != null) intValueParam.Value.ValueChanged += AlgorithmTargetQualityOnValueChanged;
     282        }
     283      }
     284      #endregion
     285    }
     286
    248287    private void DeregisterAlgorithmEvents() {
    249288      Algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
     
    251290      Algorithm.Stopped -= Algorithm_Stopped;
    252291      Algorithm.ProblemChanged -= Algorithm_ProblemChanged;
     292      #region MaximumEvaluations
     293      IParameter param;
     294      if (Algorithm.Parameters.TryGetValue("MaximumEvaluations", out param)) {
     295        var intFixedValueParam = (param as IFixedValueParameter<IntValue>);
     296        if (intFixedValueParam != null) {
     297          intFixedValueParam.Value.ValueChanged -= AlgorithmMaximumEvaluationsOnValueChanged;
     298        } else {
     299          var intValueParam = (param as IValueParameter<IntValue>);
     300          if (intValueParam != null) {
     301            intValueParam.ValueChanged -= AlgorithmMaximumEvaluationsParameterOnChanged;
     302            if (intValueParam.Value != null) intValueParam.Value.ValueChanged -= AlgorithmMaximumEvaluationsOnValueChanged;
     303          }
     304        }
     305      }
     306      #endregion
     307      #region TargetQuality
     308      if (Algorithm.Parameters.TryGetValue("TargetQuality", out param)) {
     309        var intValueParam = (param as IValueParameter<IntValue>);
     310        if (intValueParam != null) {
     311          intValueParam.ValueChanged -= AlgorithmTargetQualityParameterOnChanged;
     312          if (intValueParam.Value != null) intValueParam.Value.ValueChanged -= AlgorithmTargetQualityOnValueChanged;
     313        }
     314      }
     315      #endregion
    253316    }
    254317    #endregion
     
    263326      Analyzer.Operators.ItemsRemoved += AnalyzerOperatorsChanged;
    264327      Analyzer.Operators.ItemsReplaced += AnalyzerOperatorsChanged;
     328      MaximumEvaluationsParameter.Value.ValueChanged += MaximumEvaluationsParameterOnValueChanged;
     329      TargetQualityParameter.ValueChanged += TargetQualityParameterChanged;
     330      if (TargetQualityParameter.Value != null) TargetQualityParameter.Value.ValueChanged += TargetQualityParameterOnValueChanged;
    265331    }
    266332
     
    516582      var bkParam = ((ISingleObjectiveHeuristicOptimizationProblem)Problem).BestKnownQualityParameter as IValueParameter<DoubleValue>;
    517583      if (bkParam != null && bkParam.Value != null)
    518         TargetValueParameter.Value = new DoubleValue(bkParam.Value.Value);
     584        TargetQualityParameter.Value = new DoubleValue(bkParam.Value.Value);
    519585      else if (bkParam != null && bkParam.Value == null)
    520         TargetValueParameter.Value = null;
     586        TargetQualityParameter.Value = null;
    521587    }
    522588
     
    568634    }
    569635
     636    private void AlgorithmTargetQualityParameterOnChanged(object sender, EventArgs e) {
     637      var doubleValueParam = (sender as IValueParameter<DoubleValue>);
     638      if (doubleValueParam.Value != null) doubleValueParam.Value.ValueChanged += AlgorithmTargetQualityOnValueChanged;
     639      HandleAlgorithmTargetQualityChanged();
     640    }
     641
     642    private void AlgorithmTargetQualityOnValueChanged(object sender, EventArgs e) {
     643      HandleAlgorithmTargetQualityChanged();
     644    }
     645
     646    private void AlgorithmMaximumEvaluationsParameterOnChanged(object sender, EventArgs eventArgs) {
     647      var intValueParam = (sender as IValueParameter<IntValue>);
     648      if (intValueParam.Value == null) {
     649        intValueParam.Value = new IntValue();
     650        return;
     651      }
     652      intValueParam.Value.ValueChanged += AlgorithmMaximumEvaluationsOnValueChanged;
     653      HandleAlgorithmMaximumEvaluationsChanged();
     654    }
     655
     656    private void AlgorithmMaximumEvaluationsOnValueChanged(object sender, EventArgs eventArgs) {
     657      var intValue = (IntValue)sender;
     658      HandleAlgorithmMaximumEvaluationsChanged();
     659    }
     660
    570661    private void Problem_BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
    571662      var param = sender as IValueParameter<DoubleValue>;
     
    592683      if (Algorithm != null) Algorithm.Problem = Problem;
    593684      if (Problem != null) UpdateTargetValueFromBestKnownQuality();
     685    }
     686
     687    private void MaximumEvaluationsParameterOnValueChanged(object sender, EventArgs eventArgs) {
     688      SynchronizeMaximumEvaluationsParameter();
     689    }
     690
     691    private void TargetQualityParameterChanged(object sender, EventArgs e) {
     692      if (TargetQualityParameter.Value != null) TargetQualityParameter.Value.ValueChanged += TargetQualityParameterOnValueChanged;
     693      HandleAlgorithmTargetQualityChanged();
     694    }
     695
     696    private void TargetQualityParameterOnValueChanged(object sender, EventArgs eventArgs) {
     697      HandleAlgorithmTargetQualityChanged();
    594698    }
    595699
     
    603707    }
    604708    #endregion
     709
     710    private void HandleAlgorithmTargetQualityChanged() {
     711      IParameter param;
     712      if (!Algorithm.Parameters.TryGetValue("TargetQuality", out param)) throw new InvalidOperationException("Cannot find TargetQuality parameter in Algorithm " + Algorithm.Name);
     713      var doubleValueParam = (param as IValueParameter<DoubleValue>);
     714
     715      if (doubleValueParam.Value == null && TargetQualityParameter.Value != null) {
     716        doubleValueParam.Value = new DoubleValue(TargetQualityParameter.Value.Value);
     717      } else if (doubleValueParam.Value != null && TargetQualityParameter.Value == null) {
     718        doubleValueParam.Value = null;
     719      } else if (doubleValueParam.Value != null && TargetQualityParameter.Value != null) {
     720        doubleValueParam.Value.Value = TargetQualityParameter.Value.Value;
     721      }
     722    }
     723
     724    private void HandleAlgorithmMaximumEvaluationsChanged() {
     725      IParameter param;
     726      if (!Algorithm.Parameters.TryGetValue("MaximumEvaluations", out param)) throw new InvalidOperationException("Cannot find MaximumEvaluations parameter in Algorithm " + Algorithm.Name);
     727      var intValueParam = (param as IValueParameter<IntValue>);
     728      var value = intValueParam.Value;
     729      if (value.Value > MaximumEvaluationsParameter.Value.Value)
     730        value.Value = MaximumEvaluationsParameter.Value.Value;
     731    }
     732
     733    private void SynchronizeMaximumEvaluationsParameter() {
     734      IParameter param;
     735      if (Algorithm.Parameters.TryGetValue("MaximumEvaluations", out param)) {
     736        var intValueParam = (param as IValueParameter<IntValue>);
     737        if (intValueParam.Value == null) intValueParam.Value = new IntValue(MaximumEvaluationsParameter.Value.Value);
     738        else intValueParam.Value.Value = MaximumEvaluationsParameter.Value.Value;
     739      }
     740    }
    605741  }
    606742}
Note: See TracChangeset for help on using the changeset viewer.