Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/NeutralityAnalyzer.cs @ 17714

Last change on this file since 17714 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

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