Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886

  • renamed MenuItem for the HiveRunCollectionModifier
  • deactivated calculation of complete convex hull for permutations
File size: 4.8 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        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}
Note: See TracBrowser for help on using the repository browser.