Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/DistinctSolutionsAnalyzer.cs @ 8281

Last change on this file since 8281 was 8281, checked in by ascheibe, 12 years ago

#1886 added datatable to convergence speed analyzer

File size: 5.5 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.TravelingSalesman;
33
34namespace HeuristicLab.Analysis.AlgorithmBehavior {
35
36  /// <summary>
37  /// An operator that analyzes the quality of schemas.
38  /// </summary>
39  [Item("DistinctSolutionsAnalyzer", "An operator that analyzes broken inheritance of building blocks.")]
40  [StorableClass]
41  public sealed class DistinctSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
42    private const string ResultsParameterName = "Results";
43    private const string PopulationGraphResultParameterName = "PopulationGraph";
44    private const string SchemataParameterName = "Schemata";
45    private const string DistinctSolutionsInGenerationsMatrixParameterName = "DistinctSolutionsInGenerationsMatrix";
46    private const string QualitiesParameterName = "Qualities";
47    private const string DistanceMatrixParameterName = "DistanceMatrix";
48    private const string GenerationsParameterName = "Generations";
49    private const string PopulationSizeParameterName = "PopulationSize";
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    public ILookupParameter<IntValue> PopulationSizeParameter {
69      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
70    }
71    #endregion
72
73    #region Properties
74    public ResultCollection Results {
75      get { return ResultsParameter.ActualValue; }
76    }
77    public DistanceMatrix DistanceMatrix {
78      get { return DistanceMatrixParameter.ActualValue; }
79    }
80    public HeuristicLab.Analysis.DataTable Qualities {
81      get { return ((HeuristicLab.Analysis.DataTable)ResultsParameter.ActualValue[QualitiesParameterName].Value); }
82    }
83    public PathTSPTour BestSolution {
84      get { return ((PathTSPTour)ResultsParameter.ActualValue[BestSolutionParameterName].Value); }
85    }
86    #endregion
87
88    [StorableConstructor]
89    private DistinctSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
90    private DistinctSolutionsAnalyzer(DistinctSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { }
91    public DistinctSolutionsAnalyzer()
92      : base() {
93      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
94      Parameters.Add(new LookupParameter<DistanceMatrix>(DistanceMatrixParameterName, "The distance matrix of the TSP."));
95      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
96      Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "Population Size."));
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new DistinctSolutionsAnalyzer(this, cloner);
101    }
102
103    public override IOperation Apply() {
104      var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
105      int generations = GenerationsParameter.ActualValue.Value;
106      int popSize = PopulationSizeParameter.ActualValue.Value;
107
108      Dictionary<int, int> distinctSolsInGen = new Dictionary<int, int>();
109
110      for (int i = 0; i < generations; i++) {
111        var individuals = graph.Values.Where(x => x.Rank.Contains(i));
112        distinctSolsInGen.Add(i, individuals.Count());
113      }
114
115      DataTable dt = new DataTable();
116      DataRow row = new DataRow("Distinct Solutions");
117      dt.Rows.Add(row);
118      for (int i = 0; i < distinctSolsInGen.Keys.Count; i++) {
119        var element = distinctSolsInGen.ElementAt(i);
120        dt.Rows["Distinct Solutions"].Values.Add(element.Value);
121      }
122
123      Results.Add(new Result(DistinctSolutionsInGenerationsMatrixParameterName, dt));
124      return base.Apply();
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.