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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Analysis.SolutionCaching.PermutationEncoding;
|
---|
27 | using HeuristicLab.Analysis.SolutionCaching.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
36 | [Item("PermutationConvexHullModifier", "Calculates the convex hull from a solution cache. ")]
|
---|
37 | [StorableClass]
|
---|
38 | public class PermutationConvexHullModifier : ParameterizedNamedItem, IRunCollectionModifier {
|
---|
39 | private const string SolutionCacheResultName = "SolutionCache";
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected PermutationConvexHullModifier(bool deserializing) : base(deserializing) { }
|
---|
43 | protected PermutationConvexHullModifier(PermutationConvexHullModifier original, Cloner cloner) : base(original, cloner) { }
|
---|
44 | public PermutationConvexHullModifier() { }
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
46 | return new PermutationConvexHullModifier(this, cloner);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void Modify(List<IRun> runs) {
|
---|
50 | foreach (var run in runs) {
|
---|
51 | int i = 0;
|
---|
52 | if (!run.Results.ContainsKey(SolutionCacheResultName)) continue;
|
---|
53 | var solutionCache = run.Results.Single(x => x.Key == "SolutionCache").Value as PermutationSolutionCache;
|
---|
54 | if (solutionCache == null) continue;
|
---|
55 |
|
---|
56 | DataTable dt = new DataTable("Convex Hull Volume over Generations");
|
---|
57 | DataRow dtVolumeRow = new DataRow("Volume");
|
---|
58 | DataRow dtNrPointsRow = new DataRow("Nr. of Points");
|
---|
59 | dt.Rows.Add(dtVolumeRow);
|
---|
60 | dt.Rows.Add(dtNrPointsRow);
|
---|
61 |
|
---|
62 | var sols = solutionCache.GetSolutionsFromGeneration(i);
|
---|
63 | int dim = sols.First().Length;
|
---|
64 | while (sols.Count != 0) {
|
---|
65 | double[][] dm = new double[sols.Count][];
|
---|
66 | for (int j = 0; j < sols.Count; j++) {
|
---|
67 | dm[j] = new double[sols.Count];
|
---|
68 | }
|
---|
69 |
|
---|
70 | for (int j = 0; j < sols.Count; j++) {
|
---|
71 | for (int k = 0; k < sols.Count; k++) {
|
---|
72 | dm[j][k] = TSPSimilarityCalculator.CalculateSimilarity(sols[j], sols[k]);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | double[][] popPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(DistanceMatrixToPoints.MetricMDS(dm, dim, true));
|
---|
76 |
|
---|
77 | var convexHull = LPHull.Calculate(popPoints);
|
---|
78 | var volume = ConvexHullMeasures.CalculateVolume(convexHull);
|
---|
79 | dtVolumeRow.Values.Add(volume);
|
---|
80 | dtNrPointsRow.Values.Add(convexHull.Count);
|
---|
81 |
|
---|
82 | sols = solutionCache.GetSolutionsFromGeneration(++i);
|
---|
83 | }
|
---|
84 |
|
---|
85 | run.Results.Add("Convex Hull Measures", dt);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | } |
---|