source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/AlleleOccurencesInGenerationsPerIndAnalyzer.cs @ 8336

Last change on this file since 8336 was 8336, checked in by ascheibe, 8 years ago

#1886 worked on crossover performance analyzer

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.TravelingSalesman;
34
35namespace HeuristicLab.Analysis.AlgorithmBehavior {
36
37  /// <summary>
38  /// An operator that analyzes the quality of schemas.
39  /// </summary>
40  [Item("AlleleOccurencesInGenerationsPerInd", "An operator that analyzes broken inheritance of building blocks.")]
41  [StorableClass]
42  public sealed class AlleleOccurencesInGenerationsPerInd : SingleSuccessorOperator, IAnalyzer {
43    private const string ResultsParameterName = "Results";
44    private const string PopulationGraphResultParameterName = "PopulationGraph";
45    private const string SchemataParameterName = "Schemata";
46    private const string SchemaOccurenceInGenerationsMatrixParameterName = "SchemaOccurenceInGenerationsMatrix";
47    private const string QualitiesParameterName = "Qualities";
48    private const string DistanceMatrixParameterName = "DistanceMatrix";
49    private const string GenerationsParameterName = "Generations";
50    private const string BestSolutionParameterName = "Best TSP Solution";
51
52    #region IAnalyzer Members
53    public bool EnabledByDefault {
54      get { return true; }
55    }
56    #endregion
57
58    #region Parameter properties
59    public ILookupParameter<ResultCollection> ResultsParameter {
60      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
61    }
62    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
63      get { return (ILookupParameter<DistanceMatrix>)Parameters[DistanceMatrixParameterName]; }
64    }
65    public ILookupParameter<IntValue> GenerationsParameter {
66      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
67    }
68    #endregion
69
70    #region Properties
71    public ResultCollection Results {
72      get { return ResultsParameter.ActualValue; }
73    }
74    public DistanceMatrix DistanceMatrix {
75      get { return DistanceMatrixParameter.ActualValue; }
76    }
77    public HeuristicLab.Analysis.DataTable Qualities {
78      get { return ((HeuristicLab.Analysis.DataTable)ResultsParameter.ActualValue[QualitiesParameterName].Value); }
79    }
80    public PathTSPTour BestSolution {
81      get { return ((PathTSPTour)ResultsParameter.ActualValue[BestSolutionParameterName].Value); }
82    }
83    #endregion
84
85    [StorableConstructor]
86    private AlleleOccurencesInGenerationsPerInd(bool deserializing) : base(deserializing) { }
87    private AlleleOccurencesInGenerationsPerInd(AlleleOccurencesInGenerationsPerInd original, Cloner cloner) : base(original, cloner) { }
88    public AlleleOccurencesInGenerationsPerInd()
89      : base() {
90      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
91      Parameters.Add(new LookupParameter<DistanceMatrix>(DistanceMatrixParameterName, "The distance matrix of the TSP."));
92      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new AlleleOccurencesInGenerationsPerInd(this, cloner);
97    }
98
99    public override IOperation Apply() {
100      var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
101      var subtours = AlgorithmBehaviorHelpers.ExtractSubtours(BestSolution.Permutation, 2);
102      List<Point2D<double>> points = new List<Point2D<double>>();
103
104      for (int i = 0; i < GenerationsParameter.ActualValue.Value; i++) {
105        var individuals = graph.Values.Where(x => x.Rank.Contains(i));
106        var others = graph.Values.Where(x => x.Rank.Contains(i - 0.5));
107        var others2 = graph.Values.Where(x => x.Rank.Contains(i + 0.5));
108        List<double> qualitiesInGen = new List<double>();
109        foreach (var individual in individuals) {
110          qualitiesInGen.Add(TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, (Permutation)individual.Data));
111        }
112
113        points.Clear();
114        foreach (var individual in individuals) {
115          var ind = (IntArray)individual.Data;
116          int cnt = 0;
117          double curQuality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, (Permutation)individual.Data);
118
119          foreach (var subtour in subtours) {
120            if (AlgorithmBehaviorHelpers.IsSubtour(subtour, (Permutation)ind)) {
121              cnt++;
122            }
123          }
124
125          double qualityRange = Math.Abs(qualitiesInGen.Max() - qualitiesInGen.Min());
126          points.Add(new Point2D<double>(cnt, Math.Abs(curQuality - qualitiesInGen.Min()) / qualityRange));
127        }
128
129        var plot = new ScatterPlot("Contained Edges of Best Found Solution and Relative Solution Qualtiy", null);
130        plot.VisualProperties.XAxisTitle = "Contained Alleles of Best Found Solution";
131        plot.VisualProperties.YAxisTitle = "Relative Solution Quality";
132        plot.VisualProperties.XAxisMinimumAuto = false;
133        plot.VisualProperties.XAxisMinimumFixedValue = 0.0;
134        plot.VisualProperties.XAxisMaximumAuto = false;
135        plot.VisualProperties.XAxisMaximumFixedValue = individuals.Count();
136        plot.VisualProperties.YAxisMinimumAuto = false;
137        plot.VisualProperties.YAxisMinimumFixedValue = 0.0;
138        plot.VisualProperties.YAxisMaximumAuto = false;
139        plot.VisualProperties.YAxisMaximumFixedValue = 1.0;
140        var row = new ScatterPlotDataRow("Solutions of Current Generation", null, points);
141        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
142        row.VisualProperties.PointSize = 5;
143        plot.Rows.Add(row);
144
145        if (!Results.ContainsKey("Scatter Plot per Ind"))
146          Results.Add(new Result("Scatter Plot per Ind", plot));
147        else
148          Results["Scatter Plot per Ind"].Value = plot;
149
150        if (!Results.ContainsKey("Scatter Plot per Ind History")) {
151          Results.Add(new Result("Scatter Plot per Ind History", new ScatterPlotHistory()));
152        }
153        ((ScatterPlotHistory)Results["Scatter Plot per Ind History"].Value).Add(plot);
154        points.Clear();
155      }
156
157      return base.Apply();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.