#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.TravelingSalesman; namespace HeuristicLab.Analysis.AlgorithmBehavior { /// /// An operator that analyzes the quality of schemas. /// [Item("AlleleOccurencesInGenerationsPerInd", "An operator that analyzes broken inheritance of building blocks.")] [StorableClass] public sealed class AlleleOccurencesInGenerationsPerInd : SingleSuccessorOperator, IAnalyzer { private const string ResultsParameterName = "Results"; private const string PopulationGraphResultParameterName = "PopulationGraph"; private const string SchemataParameterName = "Schemata"; private const string SchemaOccurenceInGenerationsMatrixParameterName = "SchemaOccurenceInGenerationsMatrix"; private const string QualitiesParameterName = "Qualities"; private const string DistanceMatrixParameterName = "DistanceMatrix"; private const string GenerationsParameterName = "Generations"; private const string BestSolutionParameterName = "Best TSP Solution"; #region IAnalyzer Members public bool EnabledByDefault { get { return true; } } #endregion #region Parameter properties public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters[ResultsParameterName]; } } public ILookupParameter DistanceMatrixParameter { get { return (ILookupParameter)Parameters[DistanceMatrixParameterName]; } } public ILookupParameter GenerationsParameter { get { return (ILookupParameter)Parameters[GenerationsParameterName]; } } #endregion #region Properties public ResultCollection Results { get { return ResultsParameter.ActualValue; } } public DistanceMatrix DistanceMatrix { get { return DistanceMatrixParameter.ActualValue; } } public HeuristicLab.Analysis.DataTable Qualities { get { return ((HeuristicLab.Analysis.DataTable)ResultsParameter.ActualValue[QualitiesParameterName].Value); } } public PathTSPTour BestSolution { get { return ((PathTSPTour)ResultsParameter.ActualValue[BestSolutionParameterName].Value); } } #endregion [StorableConstructor] private AlleleOccurencesInGenerationsPerInd(bool deserializing) : base(deserializing) { } private AlleleOccurencesInGenerationsPerInd(AlleleOccurencesInGenerationsPerInd original, Cloner cloner) : base(original, cloner) { } public AlleleOccurencesInGenerationsPerInd() : base() { Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection where the analysis values should be stored.")); Parameters.Add(new LookupParameter(DistanceMatrixParameterName, "The distance matrix of the TSP.")); Parameters.Add(new LookupParameter(GenerationsParameterName, "Nr of generations.")); } public override IDeepCloneable Clone(Cloner cloner) { return new AlleleOccurencesInGenerationsPerInd(this, cloner); } public override IOperation Apply() { var graph = (GenealogyGraph)Results[PopulationGraphResultParameterName].Value; var subtours = AlgorithmBehaviorHelpers.ExtractSubtours(BestSolution.Permutation, 2); List> points = new List>(); for (int i = 0; i < GenerationsParameter.ActualValue.Value; i++) { var individuals = graph.Values.Where(x => x.Rank.Contains(i)); var others = graph.Values.Where(x => x.Rank.Contains(i - 0.5)); var others2 = graph.Values.Where(x => x.Rank.Contains(i + 0.5)); List qualitiesInGen = new List(); foreach (var individual in individuals) { qualitiesInGen.Add(TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, (Permutation)individual.Data)); } points.Clear(); foreach (var individual in individuals) { var ind = (IntArray)individual.Data; int cnt = 0; double curQuality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, (Permutation)individual.Data); foreach (var subtour in subtours) { if (AlgorithmBehaviorHelpers.IsSubtour(subtour, (Permutation)ind)) { cnt++; } } double qualityRange = Math.Abs(qualitiesInGen.Max() - qualitiesInGen.Min()); points.Add(new Point2D(cnt, Math.Abs(curQuality - qualitiesInGen.Min()) / qualityRange)); } var plot = new ScatterPlot("Contained Edges of Best Found Solution and Relative Solution Qualtiy", null); plot.VisualProperties.XAxisTitle = "Contained Alleles of Best Found Solution"; plot.VisualProperties.YAxisTitle = "Relative Solution Quality"; plot.VisualProperties.XAxisMinimumAuto = false; plot.VisualProperties.XAxisMinimumFixedValue = 0.0; plot.VisualProperties.XAxisMaximumAuto = false; plot.VisualProperties.XAxisMaximumFixedValue = individuals.Count(); plot.VisualProperties.YAxisMinimumAuto = false; plot.VisualProperties.YAxisMinimumFixedValue = 0.0; plot.VisualProperties.YAxisMaximumAuto = false; plot.VisualProperties.YAxisMaximumFixedValue = 1.0; var row = new ScatterPlotDataRow("Solutions of Current Generation", null, points); row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle; row.VisualProperties.PointSize = 5; plot.Rows.Add(row); if (!Results.ContainsKey("Scatter Plot per Ind")) Results.Add(new Result("Scatter Plot per Ind", plot)); else Results["Scatter Plot per Ind"].Value = plot; if (!Results.ContainsKey("Scatter Plot per Ind History")) { Results.Add(new Result("Scatter Plot per Ind History", new ScatterPlotHistory())); } ((ScatterPlotHistory)Results["Scatter Plot per Ind History"].Value).Add(plot); points.Clear(); } return base.Apply(); } } }