Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/RunCollectionModifiers/PermutationConvexHullModifier.cs @ 10139

Last change on this file since 10139 was 10139, checked in by ascheibe, 11 years ago

#1886 fixed a bug in the PermutationConvexHullModifier

File size: 4.6 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis.SolutionCaching.PermutationEncoding;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.TravelingSalesman;
32
33namespace 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        List<double[]> curHull = null;
64        var sols = solutionCache.GetSolutionsFromGeneration(i);
65        int dim = sols.First().Length;
66        while (sols.Count != 0) {
67          double[][] dm = CalculateDistanceMatrixFromPermutations(sols);
68
69          if (dim > dm.Length)
70            dim = dm.Length - 1;
71
72          double[][] popPoints = DistanceMatrixToPoints.MetricMDS(dm, dim);
73
74          var convexHull = LPHull.Calculate(popPoints);
75          var volume = ConvexHullMeasures.CalculateVolume(convexHull);
76          dtVolumeRow.Values.Add(volume);
77          dtNrPointsRow.Values.Add(convexHull.Count);
78
79          if (curHull == null) {
80            curHull = convexHull;
81          } else {
82            var newPHull = curHull.Union(convexHull).ToArray();
83            curHull = LPHull.Calculate(newPHull);
84          }
85
86          sols = solutionCache.GetSolutionsFromGeneration(++i);
87        }
88        overallVolumn.Value = ConvexHullMeasures.CalculateVolume(curHull);
89
90        run.Results.Add("Overall volumn: ", overallVolumn);
91        run.Results.Add("Convex hull volumn", volDataTable);
92        run.Results.Add("Convex nr. of points", nrOfPointsTable);
93      }
94    }
95
96    private double[][] CalculateDistanceMatrixFromPermutations(List<Permutation> points) {
97      double[][] tmpDm = new double[points.Count][];
98      AllocArray(tmpDm, points.Count);
99
100      for (int i = 0; i < points.Count; i++) {
101        for (int j = 0; j < points.Count; j++) {
102          double diversity = TSPSimilarityCalculator.CalculateSimilarity(points[i], points[j]);
103          tmpDm[i][j] = diversity;
104        }
105      }
106
107      return DistanceMatrixToPoints.TransformToDistances(tmpDm);
108    }
109
110    private void AllocArray(double[][] arr, int size) {
111      for (int i = 0; i < arr.Length; i++) {
112        arr[i] = new double[size];
113      }
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.