[7128] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Analysis.FitnessLandscape.DataTables;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using System.Collections.Generic;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
|
---|
| 35 |
|
---|
| 36 | [Item("Neutrality Analzyer", "Analyzes neutral parts of a trajectory")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public class NeutralityAnalyzer : AlgorithmOperator, IAnalyzer {
|
---|
[7176] | 39 | public bool EnabledByDefault {
|
---|
| 40 | get { return false; }
|
---|
| 41 | }
|
---|
[7128] | 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 | }
|
---|
[7783] | 50 | public LookupParameter<DoubleValue> CurrentFractionOfNeutralNeighborsParameter {
|
---|
| 51 | get { return (LookupParameter<DoubleValue>)Parameters["CurrentFractionOfNeutralNeighbors"]; }
|
---|
| 52 | }
|
---|
[7128] | 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 | }
|
---|
[7783] | 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 | }
|
---|
[7128] | 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 | }
|
---|
[7783] | 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 | }
|
---|
[7128] | 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."));
|
---|
[7783] | 92 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentFractionOfNeutralNeighbors", "The current fraction of neighbors with the same fitness (within epsilon)"));
|
---|
[7128] | 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."));
|
---|
[7783] | 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."));
|
---|
[7128] | 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"));
|
---|
[7783] | 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."));
|
---|
[7128] | 102 |
|
---|
| 103 | var resultsCollector = new ResultsCollector();
|
---|
| 104 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentNeutralDistanceParameter.ActualName));
|
---|
[7783] | 105 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentFractionOfNeutralNeighborsParameter.ActualName));
|
---|
[7128] | 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));
|
---|
[7783] | 109 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageNeutralNeighborFractionParameter.Name));
|
---|
| 110 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AverageInnerNeutralNeighborFractionParameter.Name));
|
---|
[7128] | 111 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkLengthVarianceParameter.Name));
|
---|
| 112 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralWalkDistanceVarianceParameter.Name));
|
---|
[7783] | 113 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(NeutralNeighborFractionVarianceParameter.Name));
|
---|
| 114 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(InnerNeutralNeighborFractionVarianceParameter.Name));
|
---|
[7128] | 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();
|
---|
[7783] | 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>();
|
---|
[7128] | 133 | double lastValue = -1;
|
---|
| 134 | int lastStart = 0;
|
---|
[7783] | 135 | for (int i = 0; i<distanceValues.Count; i++) {
|
---|
| 136 | if (distanceValues[i] < lastValue) { // restart
|
---|
| 137 | lengths.Add(i - lastStart - 1);
|
---|
[7128] | 138 | distances.Add(lastValue);
|
---|
| 139 | lastStart = i;
|
---|
[7783] | 140 | } else { // inside neutral area
|
---|
| 141 | innerFractions.Add(fractionValues[i]);
|
---|
[7128] | 142 | }
|
---|
[7783] | 143 | lastValue = distanceValues[i];
|
---|
[7128] | 144 | }
|
---|
[7783] | 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) {
|
---|
[7128] | 150 | AverageNeutralWalkDistanceParameter.ActualValue = new DoubleValue(distances.Average());
|
---|
| 151 | NeutralWalkDistanceVarianceParameter.ActualValue = new DoubleValue(distances.Variance());
|
---|
[7783] | 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());
|
---|
[7128] | 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;
|
---|
[7783] | 167 | distancesTable.Rows.Add(new DataRow("Neutral Walk Distances"));
|
---|
| 168 | distancesTable.Rows.Add(new DataRow("Neutral Neighbor Fractions") {VisualProperties = {SecondYAxis = true}});
|
---|
[7128] | 169 | }
|
---|
| 170 | return distancesTable;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | }
|
---|