Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 improved RealVectorConvexHullModifier

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