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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Analysis.AlgorithmBehavior {
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// An operator that analyzes the quality of schemas.
|
---|
38 | /// </summary>
|
---|
39 | [Item("AlleleOccurencesInGenerationsAnalyzer", "An operator that analyzes broken inheritance of building blocks.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class AlleleOccurencesInGenerationsAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
42 | private const string ResultsParameterName = "Results";
|
---|
43 | private const string PopulationGraphResultParameterName = "PopulationGraph";
|
---|
44 | private const string SchemataParameterName = "Schemata";
|
---|
45 | private const string SchemaOccurenceInGenerationsMatrixParameterName = "SchemaOccurenceInGenerationsMatrix";
|
---|
46 | private const string QualitiesParameterName = "Qualities";
|
---|
47 | private const string DistanceMatrixParameterName = "DistanceMatrix";
|
---|
48 | private const string GenerationsParameterName = "Generations";
|
---|
49 | private const string BestSolutionParameterName = "Best TSP Solution";
|
---|
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 | public PathTSPTour BestSolution {
|
---|
80 | get { return ((PathTSPTour)ResultsParameter.ActualValue[BestSolutionParameterName].Value); }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | private AlleleOccurencesInGenerationsAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
86 | private AlleleOccurencesInGenerationsAnalyzer(AlleleOccurencesInGenerationsAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
87 | public AlleleOccurencesInGenerationsAnalyzer()
|
---|
88 | : base() {
|
---|
89 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
90 | Parameters.Add(new LookupParameter<DistanceMatrix>(DistanceMatrixParameterName, "The distance matrix of the TSP."));
|
---|
91 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
95 | return new AlleleOccurencesInGenerationsAnalyzer(this, cloner);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IOperation Apply() {
|
---|
99 | var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
|
---|
100 | var subtours = AlgorithmBehaviorHelpers.ExtractSubtours(BestSolution.Permutation, 2);
|
---|
101 |
|
---|
102 | Dictionary<IntArray, Dictionary<int, List<int>>> occurences = new Dictionary<IntArray, Dictionary<int, List<int>>>();
|
---|
103 | foreach (var s in subtours) {
|
---|
104 | occurences.Add(s, new Dictionary<int, List<int>>());
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (int i = 0; i < GenerationsParameter.ActualValue.Value; i++) {
|
---|
108 | var individuals = graph.Values.Where(x => x.Rank.Contains(i) || x.Rank.Contains(i + 0.5));
|
---|
109 | foreach (var subtour in subtours) {
|
---|
110 | int cnt = 0;
|
---|
111 | double quality = 0.0;
|
---|
112 | foreach (var individual in individuals) {
|
---|
113 | var ind = (IntArray)individual.Data;
|
---|
114 | if (AlgorithmBehaviorHelpers.IsSubtour(subtour, (Permutation)ind)) {
|
---|
115 | cnt++;
|
---|
116 | quality += TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, (Permutation)individual.Data);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | List<int> vals = new List<int>();
|
---|
121 | vals.Add(cnt);
|
---|
122 | if (cnt != 0) {
|
---|
123 | double avgQuality = quality / cnt;
|
---|
124 | double qualityRatio = avgQuality / Qualities.Rows["CurrentAverageQuality"].Values[i] * 100;
|
---|
125 | vals.Add((int)qualityRatio);
|
---|
126 | }
|
---|
127 | occurences[subtour][i] = vals;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | var schemataMatrix = new Dictionary<IntArray, string>(new SchemaEqualityComparer());
|
---|
132 | foreach (var ind in occurences.Keys) {
|
---|
133 | if (!schemataMatrix.ContainsKey(ind)) {
|
---|
134 | string result = string.Empty;
|
---|
135 | foreach (var res in occurences[ind]) {
|
---|
136 | result += res.Key + ": ";
|
---|
137 | foreach (var r in res.Value) {
|
---|
138 | result += r.ToString() + ", ";
|
---|
139 | }
|
---|
140 | result += "; ";
|
---|
141 | }
|
---|
142 | schemataMatrix.Add(ind, result);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | // order schemas according to their global occurrence count
|
---|
147 | var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
|
---|
148 |
|
---|
149 | var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 2);
|
---|
150 | for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
|
---|
151 | var element = sortedSchemataMatrix.ElementAt(i);
|
---|
152 | stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
|
---|
153 | stringMatrix[i, 1] = element.Value;
|
---|
154 | }
|
---|
155 | stringMatrix.ColumnNames = new[] { "Schema", "SchemaCountOverGeneration" };
|
---|
156 |
|
---|
157 | Results.Add(new Result(SchemaOccurenceInGenerationsMatrixParameterName, stringMatrix));
|
---|
158 |
|
---|
159 | return base.Apply();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|