Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13042 was 7783, checked in by epitzer, 13 years ago

#1696 add neutrality breadth analysis by including and analyzing the number of possible neutral neighbors both overall and inside neutral areas

File size: 10.1 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<DoubleValue> CurrentFractionOfNeutralNeighborsParameter {
51      get { return (LookupParameter<DoubleValue>)Parameters["CurrentFractionOfNeutralNeighbors"]; }
52    }
53    public LookupParameter<DataTable> NeutralWalkDistancesTableParameter {
54      get { return (LookupParameter<DataTable>)Parameters["NeutralWalkDistancesTable"]; }
55    }
56    public LookupParameter<DoubleValue> AverageNeutralWalkLengthParameter {
57      get { return (LookupParameter<DoubleValue>)Parameters["AverageNeutralWalkLength"]; }
58    }
59    public LookupParameter<DoubleValue> AverageNeutralWalkDistanceParameter {
60      get { return (LookupParameter<DoubleValue>)Parameters["AverageNeutralWalkDistance"]; }
61    }
62    public LookupParameter<DoubleValue> AverageNeutralNeighborFractionParameter {
63      get { return (LookupParameter<DoubleValue>)Parameters["AverageNeutralNeighborFraction"]; }
64    }
65    public LookupParameter<DoubleValue> AverageInnerNeutralNeighborFractionParameter {
66      get { return (LookupParameter<DoubleValue>)Parameters["AverageInnerNeutralNeighborFraction"]; }
67    }
68    public LookupParameter<DoubleValue> NeutralWalkLengthVarianceParameter {
69      get { return (LookupParameter<DoubleValue>)Parameters["NeutralWalkLengthVariance"]; }
70    }
71    public LookupParameter<DoubleValue> NeutralWalkDistanceVarianceParameter {
72      get { return (LookupParameter<DoubleValue>)Parameters["NeutralWalkDistanceVariance"]; }
73    }       
74    public LookupParameter<DoubleValue> NeutralNeighborFractionVarianceParameter {
75      get { return (LookupParameter<DoubleValue>)Parameters["NeutralNeighborFractionVariance"]; }
76    }
77    public LookupParameter<DoubleValue> InnerNeutralNeighborFractionVarianceParameter {
78      get { return (LookupParameter<DoubleValue>)Parameters["InnerNeutralNeighborFractionVariance"]; }
79    }
80    public LookupParameter<VariableCollection> ResultsParameter {
81      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
82    }
83    #endregion
84
85    [StorableConstructor]
86    protected NeutralityAnalyzer(bool deserializing) : base(deserializing) { }
87    protected NeutralityAnalyzer(NeutralityAnalyzer original, Cloner cloner) : base(original, cloner) { }
88
89    public NeutralityAnalyzer() {
90      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The current quality"));
91      Parameters.Add(new LookupParameter<DoubleValue>("CurrentNeutralDistance", "The distance of the current solution to the starting point of the neutral (portion of the) walk."));
92      Parameters.Add(new LookupParameter<DoubleValue>("CurrentFractionOfNeutralNeighbors", "The current fraction of neighbors with the same fitness (within epsilon)"));
93      Parameters.Add(new LookupParameter<DataTable>("NeutralWalkDistancesTable", "The historical values of the current neutral distance."));
94      Parameters.Add(new LookupParameter<DoubleValue>("AverageNeutralWalkLength", "The average length of a neutral walk"));
95      Parameters.Add(new LookupParameter<DoubleValue>("AverageNeutralWalkDistance", "The average distance of the neutral walk to the starting point."));
96      Parameters.Add(new LookupParameter<DoubleValue>("AverageNeutralNeighborFraction", "The average fraction of neutral neighbors overall."));
97      Parameters.Add(new LookupParameter<DoubleValue>("AverageInnerNeutralNeighborFraction", "The average fraction of neutral neighbors within a neutral area."));
98      Parameters.Add(new LookupParameter<DoubleValue>("NeutralWalkLengthVariance", "The variance of the neutral walks lengths"));
99      Parameters.Add(new LookupParameter<DoubleValue>("NeutralWalkDistanceVariance", "The varaince of the neutral walks distances"));
100      Parameters.Add(new LookupParameter<DoubleValue>("NeutralNeighborFractionVariance", "The average fraction of neutral neighbors overall."));
101      Parameters.Add(new LookupParameter<DoubleValue>("InnerNeutralNeighborFractionVariance", "The average fraction of neutral neighbors within a neutral area."));
102
103      var resultsCollector = new ResultsCollector();
104      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentNeutralDistanceParameter.ActualName));
105      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentFractionOfNeutralNeighborsParameter.ActualName));
106      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(NeutralWalkDistancesTableParameter.Name));
107      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralWalkLengthParameter.Name));
108      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralWalkDistanceParameter.Name));
109      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralNeighborFractionParameter.Name));
110      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageInnerNeutralNeighborFractionParameter.Name));
111      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkLengthVarianceParameter.Name));
112      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkDistanceVarianceParameter.Name));
113      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralNeighborFractionVarianceParameter.Name));
114      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(InnerNeutralNeighborFractionVarianceParameter.Name));
115
116      OperatorGraph.InitialOperator = resultsCollector;
117      resultsCollector.Successor = null;
118    }
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new NeutralityAnalyzer(this, cloner);
122    }
123
124    public override IOperation Apply() {
125      DataTable distanceTable = GetOrCreateDistanceTable();
126      var distanceValues = distanceTable.Rows["Neutral Walk Distances"].Values;
127      var fractionValues = distanceTable.Rows["Neutral Neighbor Fractions"].Values;
128      distanceValues.Add(CurrentNeutralDistanceParameter.ActualValue == null ? 0 : CurrentNeutralDistanceParameter.ActualValue.Value);
129      fractionValues.Add(CurrentFractionOfNeutralNeighborsParameter.ActualValue == null ? 0 : CurrentFractionOfNeutralNeighborsParameter.ActualValue.Value);
130      var lengths = new List<double>();
131      var distances = new List<double>();
132      var innerFractions = new List<double>();
133      double lastValue = -1;
134      int lastStart = 0;
135      for (int i = 0; i<distanceValues.Count; i++) {
136        if (distanceValues[i] < lastValue) { // restart
137          lengths.Add(i - lastStart - 1);
138          distances.Add(lastValue);
139          lastStart = i;
140        } else { // inside neutral area
141          innerFractions.Add(fractionValues[i]);
142        }
143        lastValue = distanceValues[i];
144      }
145      if (lastStart != distanceValues.Count-1) {
146        lengths.Add(distanceValues.Count - lastStart - 1);
147        distances.Add(lastValue);
148      }
149      if (distances.Count > 0 && lengths.Count > 0) {
150        AverageNeutralWalkDistanceParameter.ActualValue = new DoubleValue(distances.Average());
151        NeutralWalkDistanceVarianceParameter.ActualValue = new DoubleValue(distances.Variance());
152        AverageNeutralWalkLengthParameter.ActualValue = new DoubleValue(lengths.Average());
153        NeutralWalkLengthVarianceParameter.ActualValue = new DoubleValue(lengths.Variance());
154        AverageNeutralNeighborFractionParameter.ActualValue = new DoubleValue(fractionValues.Average());
155        NeutralNeighborFractionVarianceParameter.ActualValue = new DoubleValue(fractionValues.Variance());
156        AverageInnerNeutralNeighborFractionParameter.ActualValue = new DoubleValue(innerFractions.Average());
157        InnerNeutralNeighborFractionVarianceParameter.ActualValue = new DoubleValue(innerFractions.Variance());
158      }
159      return base.Apply();
160    }
161
162    private DataTable GetOrCreateDistanceTable() {
163      DataTable distancesTable = NeutralWalkDistancesTableParameter.ActualValue;
164      if (distancesTable == null) {
165        distancesTable = new DataTable("Neutral Walk Distances");
166        NeutralWalkDistancesTableParameter.ActualValue = distancesTable;
167        distancesTable.Rows.Add(new DataRow("Neutral Walk Distances"));
168        distancesTable.Rows.Add(new DataRow("Neutral Neighbor Fractions") {VisualProperties = {SecondYAxis = true}});
169      }
170      return distancesTable;
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.