#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Linq; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Optimization; using System.Collections.Generic; namespace HeuristicLab.Analysis.FitnessLandscape.Analysis { [Item("Neutrality Analzyer", "Analyzes neutral parts of a trajectory")] [StorableClass] public class NeutralityAnalyzer : AlgorithmOperator, IAnalyzer { public bool EnabledByDefault { get { return false; } } #region Parameters public LookupParameter QualityParameter { get { return (LookupParameter)Parameters["Quality"]; } } public LookupParameter CurrentNeutralDistanceParameter { get { return (LookupParameter)Parameters["CurrentNeutralDistance"]; } } public LookupParameter CurrentFractionOfNeutralNeighborsParameter { get { return (LookupParameter)Parameters["CurrentFractionOfNeutralNeighbors"]; } } public LookupParameter NeutralWalkDistancesTableParameter { get { return (LookupParameter)Parameters["NeutralWalkDistancesTable"]; } } public LookupParameter AverageNeutralWalkLengthParameter { get { return (LookupParameter)Parameters["AverageNeutralWalkLength"]; } } public LookupParameter AverageNeutralWalkDistanceParameter { get { return (LookupParameter)Parameters["AverageNeutralWalkDistance"]; } } public LookupParameter AverageNeutralNeighborFractionParameter { get { return (LookupParameter)Parameters["AverageNeutralNeighborFraction"]; } } public LookupParameter AverageInnerNeutralNeighborFractionParameter { get { return (LookupParameter)Parameters["AverageInnerNeutralNeighborFraction"]; } } public LookupParameter NeutralWalkLengthVarianceParameter { get { return (LookupParameter)Parameters["NeutralWalkLengthVariance"]; } } public LookupParameter NeutralWalkDistanceVarianceParameter { get { return (LookupParameter)Parameters["NeutralWalkDistanceVariance"]; } } public LookupParameter NeutralNeighborFractionVarianceParameter { get { return (LookupParameter)Parameters["NeutralNeighborFractionVariance"]; } } public LookupParameter InnerNeutralNeighborFractionVarianceParameter { get { return (LookupParameter)Parameters["InnerNeutralNeighborFractionVariance"]; } } public LookupParameter ResultsParameter { get { return (LookupParameter)Parameters["Results"]; } } #endregion [StorableConstructor] protected NeutralityAnalyzer(bool deserializing) : base(deserializing) { } protected NeutralityAnalyzer(NeutralityAnalyzer original, Cloner cloner) : base(original, cloner) { } public NeutralityAnalyzer() { Parameters.Add(new LookupParameter("Quality", "The current quality")); Parameters.Add(new LookupParameter("CurrentNeutralDistance", "The distance of the current solution to the starting point of the neutral (portion of the) walk.")); Parameters.Add(new LookupParameter("CurrentFractionOfNeutralNeighbors", "The current fraction of neighbors with the same fitness (within epsilon)")); Parameters.Add(new LookupParameter("NeutralWalkDistancesTable", "The historical values of the current neutral distance.")); Parameters.Add(new LookupParameter("AverageNeutralWalkLength", "The average length of a neutral walk")); Parameters.Add(new LookupParameter("AverageNeutralWalkDistance", "The average distance of the neutral walk to the starting point.")); Parameters.Add(new LookupParameter("AverageNeutralNeighborFraction", "The average fraction of neutral neighbors overall.")); Parameters.Add(new LookupParameter("AverageInnerNeutralNeighborFraction", "The average fraction of neutral neighbors within a neutral area.")); Parameters.Add(new LookupParameter("NeutralWalkLengthVariance", "The variance of the neutral walks lengths")); Parameters.Add(new LookupParameter("NeutralWalkDistanceVariance", "The varaince of the neutral walks distances")); Parameters.Add(new LookupParameter("NeutralNeighborFractionVariance", "The average fraction of neutral neighbors overall.")); Parameters.Add(new LookupParameter("InnerNeutralNeighborFractionVariance", "The average fraction of neutral neighbors within a neutral area.")); var resultsCollector = new ResultsCollector(); resultsCollector.CollectedValues.Add(new LookupParameter(CurrentNeutralDistanceParameter.ActualName)); resultsCollector.CollectedValues.Add(new LookupParameter(CurrentFractionOfNeutralNeighborsParameter.ActualName)); resultsCollector.CollectedValues.Add(new LookupParameter(NeutralWalkDistancesTableParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(AverageNeutralWalkLengthParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(AverageNeutralWalkDistanceParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(AverageNeutralNeighborFractionParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(AverageInnerNeutralNeighborFractionParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(NeutralWalkLengthVarianceParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(NeutralWalkDistanceVarianceParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(NeutralNeighborFractionVarianceParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(InnerNeutralNeighborFractionVarianceParameter.Name)); OperatorGraph.InitialOperator = resultsCollector; resultsCollector.Successor = null; } public override IDeepCloneable Clone(Cloner cloner) { return new NeutralityAnalyzer(this, cloner); } public override IOperation Apply() { DataTable distanceTable = GetOrCreateDistanceTable(); var distanceValues = distanceTable.Rows["Neutral Walk Distances"].Values; var fractionValues = distanceTable.Rows["Neutral Neighbor Fractions"].Values; distanceValues.Add(CurrentNeutralDistanceParameter.ActualValue == null ? 0 : CurrentNeutralDistanceParameter.ActualValue.Value); fractionValues.Add(CurrentFractionOfNeutralNeighborsParameter.ActualValue == null ? 0 : CurrentFractionOfNeutralNeighborsParameter.ActualValue.Value); var lengths = new List(); var distances = new List(); var innerFractions = new List(); double lastValue = -1; int lastStart = 0; for (int i = 0; i 0 && lengths.Count > 0) { AverageNeutralWalkDistanceParameter.ActualValue = new DoubleValue(distances.Average()); NeutralWalkDistanceVarianceParameter.ActualValue = new DoubleValue(distances.Variance()); AverageNeutralWalkLengthParameter.ActualValue = new DoubleValue(lengths.Average()); NeutralWalkLengthVarianceParameter.ActualValue = new DoubleValue(lengths.Variance()); AverageNeutralNeighborFractionParameter.ActualValue = new DoubleValue(fractionValues.Average()); NeutralNeighborFractionVarianceParameter.ActualValue = new DoubleValue(fractionValues.Variance()); AverageInnerNeutralNeighborFractionParameter.ActualValue = new DoubleValue(innerFractions.Average()); InnerNeutralNeighborFractionVarianceParameter.ActualValue = new DoubleValue(innerFractions.Variance()); } return base.Apply(); } private DataTable GetOrCreateDistanceTable() { DataTable distancesTable = NeutralWalkDistancesTableParameter.ActualValue; if (distancesTable == null) { distancesTable = new DataTable("Neutral Walk Distances"); NeutralWalkDistancesTableParameter.ActualValue = distancesTable; distancesTable.Rows.Add(new DataRow("Neutral Walk Distances")); distancesTable.Rows.Add(new DataRow("Neutral Neighbor Fractions") {VisualProperties = {SecondYAxis = true}}); } return distancesTable; } } }