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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Analysis.AlgorithmBehavior {
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// An operator that analyzes the quality of schemas.
|
---|
39 | /// </summary>
|
---|
40 | [Item("SchemaOccurenceInGenerationsAnalyzer", "An operator that analyzes broken inheritance of building blocks.")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class SchemaOccurenceInGenerationsAnalyzer : 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 |
|
---|
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 SchemaOccurenceInGenerationsAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
83 | private SchemaOccurenceInGenerationsAnalyzer(SchemaOccurenceInGenerationsAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
84 | public SchemaOccurenceInGenerationsAnalyzer()
|
---|
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 SchemaOccurenceInGenerationsAnalyzer(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 HeuristicLab.Analysis.AlgorithmBehavior.SchemaAnalyzer.SchemaEqualityComparer());
|
---|
100 | foreach (var schema in schemata) {
|
---|
101 | if (!schemataMatrix.ContainsKey(schema)) {
|
---|
102 | string result = ExploreSchemaCountOverGenerations(graph, schema);
|
---|
103 | schemataMatrix.Add(schema, result);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | // order schemas according to their global occurrence count
|
---|
108 | var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
|
---|
109 |
|
---|
110 | var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 2);
|
---|
111 | for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
|
---|
112 | var element = sortedSchemataMatrix.ElementAt(i);
|
---|
113 | stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
|
---|
114 | stringMatrix[i, 1] = element.Value;
|
---|
115 | }
|
---|
116 | stringMatrix.ColumnNames = new[] { "Schema", "SchemaCountOverGeneration" };
|
---|
117 |
|
---|
118 | Results.Add(new Result(SchemaOccurenceInGenerationsMatrixParameterName, stringMatrix));
|
---|
119 |
|
---|
120 | return base.Apply();
|
---|
121 | }
|
---|
122 |
|
---|
123 | private string ExploreSchemaCountOverGenerations(GenealogyGraph<Permutation> graph, IntArray schema) {
|
---|
124 | int[] occurences = new int[GenerationsParameter.ActualValue.Value + 1];
|
---|
125 |
|
---|
126 | foreach (var individual in graph.Values) {
|
---|
127 | if (SchemaAnalyzer.IsSubtour(schema, (Permutation)individual.Data)) {
|
---|
128 | occurences[(int)individual.Rank]++;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | StringBuilder builder = new StringBuilder();
|
---|
133 | foreach (int x in occurences)
|
---|
134 | builder.Append(x.ToString() + ", ");
|
---|
135 |
|
---|
136 | return builder.ToString();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|