Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/NeutralityAnalyzer.cs @ 7176

Last change on this file since 7176 was 7176, checked in by gkronber, 12 years ago

#1696 adapted analyzers to compile with changes of r7172 (#1584)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Linq;
23using HeuristicLab.Analysis.FitnessLandscape.DataTables;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Optimization;
32using System.Collections.Generic;
33
34namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
35
36  [Item("Neutrality Analzyer", "Analyzes neutral parts of a trajectory")]
37  [StorableClass]
38  public class NeutralityAnalyzer : AlgorithmOperator, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return false; }
41    }
42
43    #region Parameters
44    public LookupParameter<DoubleValue> QualityParameter {
45      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public LookupParameter<DoubleValue> CurrentNeutralDistanceParameter {
48      get { return (LookupParameter<DoubleValue>)Parameters["CurrentNeutralDistance"]; }
49    }
50    public LookupParameter<DataTable> NeutralWalkDistancesTableParameter {
51      get { return (LookupParameter<DataTable>)Parameters["NeutralWalkDistancesTable"]; }
52    }
53    public LookupParameter<DoubleValue> AverageNeutralWalkLengthParameter {
54      get { return (LookupParameter<DoubleValue>)Parameters["AverageNeutralWalkLength"]; }
55    }
56    public LookupParameter<DoubleValue> AverageNeutralWalkDistanceParameter {
57      get { return (LookupParameter<DoubleValue>)Parameters["AverageNeutralWalkDistance"]; }
58    }
59    public LookupParameter<DoubleValue> NeutralWalkLengthVarianceParameter {
60      get { return (LookupParameter<DoubleValue>)Parameters["NeutralWalkLengthVariance"]; }
61    }
62    public LookupParameter<DoubleValue> NeutralWalkDistanceVarianceParameter {
63      get { return (LookupParameter<DoubleValue>)Parameters["NeutralWalkDistanceVariance"]; }
64    }       
65    public LookupParameter<VariableCollection> ResultsParameter {
66      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
67    }
68    #endregion
69
70    [StorableConstructor]
71    protected NeutralityAnalyzer(bool deserializing) : base(deserializing) { }
72    protected NeutralityAnalyzer(NeutralityAnalyzer original, Cloner cloner) : base(original, cloner) { }
73
74    public NeutralityAnalyzer() {
75      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The current quality"));
76      Parameters.Add(new LookupParameter<DoubleValue>("CurrentNeutralDistance", "The distance of the current solution to the starting point of the neutral (portion of the) walk."));
77      Parameters.Add(new LookupParameter<DataTable>("NeutralWalkDistancesTable", "The historical values of the current neutral distance."));
78      Parameters.Add(new LookupParameter<DoubleValue>("AverageNeutralWalkLength", "The average length of a neutral walk"));
79      Parameters.Add(new LookupParameter<DoubleValue>("AverageNeutralWalkDistance", "The average distance of the neutral walk to the starting point."));
80      Parameters.Add(new LookupParameter<DoubleValue>("NeutralWalkLengthVariance", "The variance of the neutral walks lengths"));
81      Parameters.Add(new LookupParameter<DoubleValue>("NeutralWalkDistanceVariance", "The varaince of the neutral walks distances"));
82
83      var resultsCollector = new ResultsCollector();
84      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentNeutralDistanceParameter.ActualName));
85      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(NeutralWalkDistancesTableParameter.Name));
86      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralWalkLengthParameter.Name));
87      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralWalkDistanceParameter.Name));
88      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkLengthVarianceParameter.Name));
89      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkDistanceVarianceParameter.Name));
90
91      OperatorGraph.InitialOperator = resultsCollector;
92      resultsCollector.Successor = null;
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new NeutralityAnalyzer(this, cloner);
97    }
98
99    public override IOperation Apply() {
100      DataTable distanceTable = GetOrCreateDistanceTable();
101      var values = distanceTable.Rows["Neutral Walk Distances"].Values;
102      values.Add(CurrentNeutralDistanceParameter.ActualValue == null ? 0 : CurrentNeutralDistanceParameter.ActualValue.Value);
103      List<double> lenghts = new List<double>();
104      List<double> distances = new List<double>();
105      double lastValue = -1;
106      int lastStart = 0;
107      for (int i = 0; i<values.Count; i++) {
108        if (values[i] < lastValue) {
109          lenghts.Add(i - lastStart - 1);
110          distances.Add(lastValue);
111          lastStart = i;
112        }
113        lastValue = values[i];
114      }
115      if (distances.Count > 0 && lenghts.Count > 0) {
116        AverageNeutralWalkDistanceParameter.ActualValue = new DoubleValue(distances.Average());
117        NeutralWalkDistanceVarianceParameter.ActualValue = new DoubleValue(distances.Variance());
118        AverageNeutralWalkLengthParameter.ActualValue = new DoubleValue(lenghts.Average());
119        NeutralWalkLengthVarianceParameter.ActualValue = new DoubleValue(lenghts.Variance());
120      }
121      return base.Apply();
122    }
123
124    private DataTable GetOrCreateDistanceTable() {
125      DataTable distancesTable = NeutralWalkDistancesTableParameter.ActualValue;
126      if (distancesTable == null) {
127        distancesTable = new DataTable("Neutral Walk Distances");
128        NeutralWalkDistancesTableParameter.ActualValue = distancesTable;
129        var row = new DataRow("Neutral Walk Distances");
130        distancesTable.Rows.Add(row);
131      }
132      return distancesTable;
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.