Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/RuggednessAnalyzer.cs @ 13583

Last change on this file since 13583 was 13583, checked in by abeham, 8 years ago

#2457:

  • Added stripped-down version of FLA branch
  • Added appropriate calculators
  • Fixed detecting maximization in RLD view
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System.Linq;
30
31namespace HeuristicLab.Analysis.FitnessLandscape {
32  [Item("Ruggedness Analyzer", "Analyzes autocorrelation for one mutation step and correlation length.")]
33  [StorableClass]
34  public class RuggednessAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
35    public bool EnabledByDefault {
36      get { return false; }
37    }
38
39    #region Parameters
40    public LookupParameter<DataTable> QualityTrailParameter {
41      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
42    }
43    public LookupParameter<ResultCollection> ResultsParameter {
44      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
45    }
46    public LookupParameter<DoubleValue> AutoCorrelation1Parameter {
47      get { return (LookupParameter<DoubleValue>)Parameters["AutoCorrelation1"]; }
48    }
49    public LookupParameter<IntValue> CorrelationLengthParameter {
50      get { return (LookupParameter<IntValue>)Parameters["CorrelationLength"]; }
51    }
52    #endregion
53
54    [StorableConstructor]
55    protected RuggednessAnalyzer(bool deserializing) : base(deserializing) { }
56    protected RuggednessAnalyzer(RuggednessAnalyzer original, Cloner cloner) : base(original, cloner) { }
57    public RuggednessAnalyzer() {
58      Parameters.Add(new LookupParameter<DataTable>("Quality Trail", "The quality of the solution"));
59      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection of all results of this algorithm"));
60      Parameters.Add(new LookupParameter<DoubleValue>("AutoCorrelation1", "Autocorrelation for one mutation step."));
61      Parameters.Add(new LookupParameter<IntValue>("CorrelationLength", "The correlation length."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new RuggednessAnalyzer(this, cloner);
66    }
67
68    public override IOperation Apply() {
69      var qualityTrail = QualityTrailParameter.ActualValue;
70      if (qualityTrail == null || qualityTrail.Rows.Count <= 0) return base.Apply();
71      var results = ResultsParameter.ActualValue;
72      double[] autocorrelationValues;
73      var correlationLength = RuggednessCalculator.CalculateCorrelationLength(qualityTrail.Rows.First().Values.ToArray(), out autocorrelationValues);
74      var cl = new IntValue(correlationLength);
75      CorrelationLengthParameter.ActualValue = cl;
76      AddOrUpdateResult(results, CorrelationLengthParameter.Name, cl);
77      var ac1 = new DoubleValue(autocorrelationValues.Length > 1 ? autocorrelationValues[1] : 0.0);
78      AutoCorrelation1Parameter.ActualValue = ac1;
79      AddOrUpdateResult(results, AutoCorrelation1Parameter.Name, ac1);
80      return base.Apply();
81    }
82
83    private static void AddOrUpdateResult(ResultCollection results, string name, IItem item, bool clone = false) {
84      IResult r;
85      if (!results.TryGetValue(name, out r)) {
86        results.Add(new Result(name, clone ? (IItem)item.Clone() : item));
87      } else r.Value = clone ? (IItem)item.Clone() : item;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.