1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Analysis.SolutionCaching.PermutationEncoding;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
34 | [Item("PermutationConvexHullModifier", "Calculates the convex hull from a solution cache. ")]
|
---|
35 | [StorableClass]
|
---|
36 | public class PermutationConvexHullModifier : ParameterizedNamedItem, IRunCollectionModifier {
|
---|
37 | private const string SolutionCacheResultName = "SolutionCache";
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected PermutationConvexHullModifier(bool deserializing) : base(deserializing) { }
|
---|
41 | protected PermutationConvexHullModifier(PermutationConvexHullModifier original, Cloner cloner) : base(original, cloner) { }
|
---|
42 | public PermutationConvexHullModifier() { }
|
---|
43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
44 | return new PermutationConvexHullModifier(this, cloner);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void Modify(List<IRun> runs) {
|
---|
48 | foreach (var run in runs) {
|
---|
49 | int i = 0;
|
---|
50 | if (!run.Results.ContainsKey(SolutionCacheResultName)) continue;
|
---|
51 | var solutionCache = run.Results.Single(x => x.Key == "SolutionCache").Value as PermutationSolutionCache;
|
---|
52 | if (solutionCache == null) continue;
|
---|
53 |
|
---|
54 | DataTable volDataTable = new DataTable("Convex hull volume over generations");
|
---|
55 | DataTable nrOfPointsTable = new DataTable("Nr. of points of convex hull");
|
---|
56 | DoubleValue overallVolumn = new DoubleValue();
|
---|
57 |
|
---|
58 | DataRow dtVolumeRow = new DataRow("Volume");
|
---|
59 | DataRow dtNrPointsRow = new DataRow("Nr. of points");
|
---|
60 | volDataTable.Rows.Add(dtVolumeRow);
|
---|
61 | nrOfPointsTable.Rows.Add(dtNrPointsRow);
|
---|
62 |
|
---|
63 | var sols = solutionCache.GetSolutionsFromGeneration(i);
|
---|
64 | int dim;
|
---|
65 | double[][] dm;
|
---|
66 | double[][] popPoints;
|
---|
67 | while (sols.Count != 0) {
|
---|
68 | dm = CalculateDistanceMatrixFromPermutations(sols);
|
---|
69 |
|
---|
70 | dim = sols.First().Length;
|
---|
71 | if (dim > dm.Length)
|
---|
72 | dim = dm.Length - 1;
|
---|
73 |
|
---|
74 | popPoints = DistanceMatrixToPoints.MetricMDS(dm, dim);
|
---|
75 |
|
---|
76 | var convexHull = LPHull.Calculate(popPoints);
|
---|
77 | var volume = ConvexHullMeasures.CalculateVolume(convexHull);
|
---|
78 | dtVolumeRow.Values.Add(volume);
|
---|
79 | dtNrPointsRow.Values.Add(convexHull.Count);
|
---|
80 |
|
---|
81 | sols = solutionCache.GetSolutionsFromGeneration(++i);
|
---|
82 | }
|
---|
83 |
|
---|
84 | //calculate convex hull for all individuals
|
---|
85 | /* dm = CalculateDistanceMatrixFromPermutations(solutionCache.Keys().ToList());
|
---|
86 | dim = solutionCache.Keys().First().Length;
|
---|
87 | if (dim > dm.Length)
|
---|
88 | dim = dm.Length - 1;
|
---|
89 | popPoints = DistanceMatrixToPoints.MetricMDS(dm, dim);
|
---|
90 |
|
---|
91 | List<double[]> allPoints = LPHull.Calculate(popPoints);
|
---|
92 | overallVolumn.Value = ConvexHullMeasures.CalculateVolume(allPoints);*/
|
---|
93 |
|
---|
94 | //run.Results.Add("Overall volumn: ", overallVolumn);
|
---|
95 | run.Results.Add("Convex hull volumn", volDataTable);
|
---|
96 | run.Results.Add("Convex nr. of points", nrOfPointsTable);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private double[][] CalculateDistanceMatrixFromPermutations(List<Permutation> points) {
|
---|
101 | double[][] tmpDm = new double[points.Count][];
|
---|
102 | AllocArray(tmpDm, points.Count);
|
---|
103 |
|
---|
104 | for (int i = 0; i < points.Count; i++) {
|
---|
105 | for (int j = 0; j < points.Count; j++) {
|
---|
106 | double diversity = TSPSimilarityCalculator.CalculateSimilarity(points[i], points[j]);
|
---|
107 | tmpDm[i][j] = diversity;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return DistanceMatrixToPoints.TransformToDistances(tmpDm);
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void AllocArray(double[][] arr, int size) {
|
---|
115 | for (int i = 0; i < arr.Length; i++) {
|
---|
116 | arr[i] = new double[size];
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | } |
---|