Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/RunCollectionModifiers/RealVectorConvexHullModifier.cs @ 10272

Last change on this file since 10272 was 10272, checked in by ascheibe, 10 years ago

#1886 improved convex hull modifier for test functions

File size: 4.2 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.RealVectorEncoding;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
33  [Item("RealVectorConvexHullModifier", "Calculates the convex hull from a solution cache. ")]
34  [StorableClass]
35  public class RealVectorConvexHullModifier : ParameterizedNamedItem, IRunCollectionModifier {
36    private const string SolutionCacheResultName = "SolutionCache";
37
38    [StorableConstructor]
39    protected RealVectorConvexHullModifier(bool deserializing) : base(deserializing) { }
40    protected RealVectorConvexHullModifier(RealVectorConvexHullModifier original, Cloner cloner) : base(original, cloner) { }
41    public RealVectorConvexHullModifier() { }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new RealVectorConvexHullModifier(this, cloner);
44    }
45
46    public void Modify(List<IRun> runs) {
47      foreach (var run in runs) {
48        int i = 0;
49        if (!run.Results.ContainsKey(SolutionCacheResultName)) continue;
50        var solutionCache = run.Results.Single(x => x.Key == "SolutionCache").Value as RealVectorSolutionCache;
51        if (solutionCache == null) continue;
52
53        DataTable volDataTable = new DataTable("Convex hull volume over generations");
54        DataTable nrOfPointsTable = new DataTable("Nr. of points of convex hull");
55        DoubleValue overallVolume = new DoubleValue();
56
57        DataRow dtVolumeRow = new DataRow("Volume");
58        DataRow dtNrPointsRow = new DataRow("Nr. of points");
59        volDataTable.Rows.Add(dtVolumeRow);
60        nrOfPointsTable.Rows.Add(dtNrPointsRow);
61
62        List<double[]> curHull = null;
63        var sols = solutionCache.GetSolutionsFromGeneration(i);
64        while (sols.Count != 0) {
65          var input = sols.Select(x => ConvertRealVectorToVertexArray(x)).ToArray();
66          var convexHull = LPHull.Calculate(input);
67          if (convexHull.First().Length < convexHull.Count) {
68            var volume = ConvexHullMeasures.CalculateVolume(convexHull);
69            dtVolumeRow.Values.Add(volume);
70          } else {
71            dtVolumeRow.Values.Add(double.NaN);
72          }
73          dtNrPointsRow.Values.Add(convexHull.Count);
74
75          if (curHull == null) {
76            curHull = convexHull;
77          } else {
78            var newPHull = curHull.Union(convexHull).ToArray();
79            curHull = LPHull.Calculate(newPHull);
80          }
81
82          sols = solutionCache.GetSolutionsFromGeneration(++i);
83        }
84
85        if (curHull != null && curHull.Any() && curHull.First().Length < curHull.Count) {
86          overallVolume.Value = ConvexHullMeasures.CalculateVolume(curHull);
87        } else {
88          overallVolume.Value = double.NaN;
89        }
90
91        run.Results["Overall volume"] = overallVolume;
92        run.Results["Convex hull volume"] = volDataTable;
93        run.Results["Nr. of points on convex hull"] = nrOfPointsTable;
94      }
95    }
96
97    private double[] ConvertRealVectorToVertexArray(RealVector p) {
98      double[] vertex = p.Select(x => (double)x).ToArray();
99      return vertex;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.