Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/BestIndividualQualityAnalyzer.cs @ 8229

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

#1886 worked on an analyzer for the best found solution

File size: 6.1 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 System.Text;
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("BestIndividualQualityAnalyzer", "An operator that analyzes broken inheritance of building blocks.")]
41  [StorableClass]
42  public sealed class BestIndividualQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
43    private const string ResultsParameterName = "Results";
44    private const string PopulationGraphResultParameterName = "PopulationGraph";
45    private const string SchemataParameterName = "BestIndividualSchemata";
46    private const string SchemaQualityMatrixParameterName = "SchemaQualityMatrix";
47    private const string QualitiesParameterName = "Qualities";
48    private const string DistanceMatrixParameterName = "DistanceMatrix";
49    private const string GenerationsParameterName = "Generations";
50
51    #region IAnalyzer Members
52    public bool EnabledByDefault {
53      get { return true; }
54    }
55    #endregion
56
57    #region Parameter properties
58    public ILookupParameter<ResultCollection> ResultsParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
60    }
61    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
62      get { return (ILookupParameter<DistanceMatrix>)Parameters[DistanceMatrixParameterName]; }
63    }
64    public ILookupParameter<IntValue> GenerationsParameter {
65      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
66    }
67    #endregion
68
69    #region Properties
70    public ResultCollection Results {
71      get { return ResultsParameter.ActualValue; }
72    }
73    public DistanceMatrix DistanceMatrix {
74      get { return DistanceMatrixParameter.ActualValue; }
75    }
76    public HeuristicLab.Analysis.DataTable Qualities {
77      get { return ((HeuristicLab.Analysis.DataTable)ResultsParameter.ActualValue[QualitiesParameterName].Value); }
78    }
79    #endregion
80
81    [StorableConstructor]
82    private BestIndividualQualityAnalyzer(bool deserializing) : base(deserializing) { }
83    private BestIndividualQualityAnalyzer(BestIndividualQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
84    public BestIndividualQualityAnalyzer()
85      : base() {
86      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should are stored."));
87      Parameters.Add(new LookupParameter<DistanceMatrix>(DistanceMatrixParameterName, "The distance matrix of the TSP."));
88      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new BestIndividualQualityAnalyzer(this, cloner);
93    }
94
95    public override IOperation Apply() {
96      var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
97      var schemata = (ItemArray<IntArray>)Results[SchemataParameterName].Value;
98
99      var schemataMatrix = new Dictionary<IntArray, string>(new SchemaEqualityComparer());
100
101      foreach (var schema in schemata) {
102        if (schema == schemata.Last()) {
103          break; // the last one is the best found solution
104        }
105
106        if (!schemataMatrix.ContainsKey(schema)) {
107          string data = ExploreSchemaOccurrence(graph, schema, schemata.Last());
108          schemataMatrix.Add(schema, data);
109        }
110      }
111
112      // order schemas according to their global occurrence count
113      var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
114
115      var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 2);
116      for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
117        var element = sortedSchemataMatrix.ElementAt(i);
118        stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
119        stringMatrix[i, 1] = element.Value;
120      }
121      stringMatrix.ColumnNames = new[] { "Schema", "Occurence in hierarchy" };
122
123      Results.Add(new Result(SchemaQualityMatrixParameterName, stringMatrix));
124
125      return base.Apply();
126    }
127
128    private string ExploreSchemaOccurrence(GenealogyGraph<Permutation> graph, IntArray schema, IntArray parent) {
129      SchemaEqualityComparer comparer = new SchemaEqualityComparer();
130
131      var lastGen = graph.Values.Where(x => x.Rank == GenerationsParameter.ActualValue.Value);
132      var parents = lastGen.Where(x => comparer.Equals((IntArray)x.Data, parent));
133      var descendants = parents.First().Descendants().Where(x => AlgorithmBehaviorHelpers.IsSubtour(schema, (Permutation)x.Data));
134
135      StringBuilder sb = new StringBuilder();
136      foreach (var d in descendants) {
137        sb.Append(d.Rank + ", ");
138      }
139      return sb.ToString();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.