#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 NeutralWalkDistancesTableParameter { get { return (LookupParameter)Parameters["NeutralWalkDistancesTable"]; } } public LookupParameter AverageNeutralWalkLengthParameter { get { return (LookupParameter)Parameters["AverageNeutralWalkLength"]; } } public LookupParameter AverageNeutralWalkDistanceParameter { get { return (LookupParameter)Parameters["AverageNeutralWalkDistance"]; } } public LookupParameter NeutralWalkLengthVarianceParameter { get { return (LookupParameter)Parameters["NeutralWalkLengthVariance"]; } } public LookupParameter NeutralWalkDistanceVarianceParameter { get { return (LookupParameter)Parameters["NeutralWalkDistanceVariance"]; } } 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("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("NeutralWalkLengthVariance", "The variance of the neutral walks lengths")); Parameters.Add(new LookupParameter("NeutralWalkDistanceVariance", "The varaince of the neutral walks distances")); var resultsCollector = new ResultsCollector(); resultsCollector.CollectedValues.Add(new LookupParameter(CurrentNeutralDistanceParameter.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(NeutralWalkLengthVarianceParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(NeutralWalkDistanceVarianceParameter.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 values = distanceTable.Rows["Neutral Walk Distances"].Values; values.Add(CurrentNeutralDistanceParameter.ActualValue == null ? 0 : CurrentNeutralDistanceParameter.ActualValue.Value); List lenghts = new List(); List distances = new List(); double lastValue = -1; int lastStart = 0; for (int i = 0; i 0 && lenghts.Count > 0) { AverageNeutralWalkDistanceParameter.ActualValue = new DoubleValue(distances.Average()); NeutralWalkDistanceVarianceParameter.ActualValue = new DoubleValue(distances.Variance()); AverageNeutralWalkLengthParameter.ActualValue = new DoubleValue(lenghts.Average()); NeutralWalkLengthVarianceParameter.ActualValue = new DoubleValue(lenghts.Variance()); } return base.Apply(); } private DataTable GetOrCreateDistanceTable() { DataTable distancesTable = NeutralWalkDistancesTableParameter.ActualValue; if (distancesTable == null) { distancesTable = new DataTable("Neutral Walk Distances"); NeutralWalkDistancesTableParameter.ActualValue = distancesTable; var row = new DataRow("Neutral Walk Distances"); distancesTable.Rows.Add(row); } return distancesTable; } } }