Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added convex hull RunCollectionModifiers

File size: 3.7 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Analysis.SolutionCaching.PermutationEncoding;
27using HeuristicLab.Analysis.SolutionCaching.RealVectorEncoding;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.TravelingSalesman;
34
35namespace 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}
Note: See TracBrowser for help on using the repository browser.