Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added volume ratios for incremental convex hull building over generations

File size: 9.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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Analysis.SolutionCaching.RealVectorEncoding;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.TestFunctions;
34
35namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
36  [Item("RealVectorConvexHullModifier", "Calculates the convex hull from a solution cache. ")]
37  [StorableClass]
38  public class RealVectorConvexHullModifier : ParameterizedNamedItem, IRunCollectionModifier {
39    private const string SolutionCacheResultName = "SolutionCache";
40    private const string CentroidMovementDistancesName = "Centroid Movement Distances";
41    private const string CentroidMotionName = "Centroid Motion";
42    private const string BestSolutionName = "Best Solution";
43    private const string IncrementalHullVolumeRatioName = "Incremental Hull Volume Ratio";
44
45    public IValueParameter<DoubleArray> BestSolutionParameter {
46      get { return (ValueParameter<DoubleArray>)Parameters[BestSolutionName]; }
47    }
48
49    [StorableConstructor]
50    protected RealVectorConvexHullModifier(bool deserializing) : base(deserializing) { }
51    protected RealVectorConvexHullModifier(RealVectorConvexHullModifier original, Cloner cloner) : base(original, cloner) { }
52    public RealVectorConvexHullModifier() {
53      Parameters.Add(new ValueParameter<DoubleArray>(BestSolutionName, new DoubleArray()));
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new RealVectorConvexHullModifier(this, cloner);
57    }
58
59    public void Modify(List<IRun> runs) {
60      foreach (var run in runs) {
61        int i = 0;
62        if (!run.Results.ContainsKey(SolutionCacheResultName)) continue;
63        var solutionCache = run.Results.Single(x => x.Key == "SolutionCache").Value as RealVectorSolutionCache;
64        if (solutionCache == null) continue;
65
66        List<double[]> centroidList = new List<double[]>();
67        var bestSolution = run.Results["Best Solution"] as SingleObjectiveTestFunctionSolution;
68        var bestKnownSolution = BestSolutionParameter.Value;
69
70        DataTable volDataTable = new DataTable("Convex hull volume over generations");
71        DataTable nrOfPointsTable = new DataTable("Nr. of points of convex hull");
72        DataTable centroidDistancesTable = new DataTable(CentroidMovementDistancesName);
73        DataTable centroidMotionTable = new DataTable(CentroidMotionName);
74        DataTable incrementalHullVolumeRatioTable = new DataTable(IncrementalHullVolumeRatioName);
75        DoubleValue overallVolume = new DoubleValue();
76        DoubleValue overallVolumeRatio = new DoubleValue();
77
78        DataRow dtVolumeRow = new DataRow("Volume");
79        DataRow dtNrPointsRow = new DataRow("Nr. of points");
80        DataRow dtCentroidDistances = new DataRow("Distances");
81        DataRow dtCentroidMotion = new DataRow("Motion");
82        DataRow dtCentroidDistanceFromOptimum = new DataRow("Distance from optimum");
83        DataRow dtIncrementalHullVolume = new DataRow("Incremental Hull Volume");
84        DataRow dtIncrementalHullVolumeRatio = new DataRow("Incremental Hull Volume Ratio");
85        centroidMotionTable.Rows.Add(dtCentroidMotion);
86        centroidDistancesTable.Rows.Add(dtCentroidDistances);
87        centroidDistancesTable.Rows.Add(dtCentroidDistanceFromOptimum);
88        volDataTable.Rows.Add(dtVolumeRow);
89        volDataTable.Rows.Add(dtIncrementalHullVolume);
90        nrOfPointsTable.Rows.Add(dtNrPointsRow);
91        incrementalHullVolumeRatioTable.Rows.Add(dtIncrementalHullVolumeRatio);
92
93        var boundsMatrix = run.Parameters["Bounds"] as DoubleMatrix;
94        var bounds = new double[] { boundsMatrix[0, 0], boundsMatrix[0, 1] };
95        double hyperCubeVolume = ConvexHullMeasures.CalculateHypercubeVolume(bounds, BestSolutionParameter.Value.Length);
96
97        List<double[]> completeHull = null;
98        var sols = solutionCache.GetSolutionsFromGeneration(i);
99        while (sols.Count != 0) {
100          var input = sols.Select(x => ConvertRealVectorToVertexArray(x)).ToArray();
101          var convexHull = LPHull.Calculate(input);
102          if (convexHull.First().Length < convexHull.Count) {
103            var volume = ConvexHullMeasures.CalculateVolume(convexHull);
104            dtVolumeRow.Values.Add(volume);
105          } else {
106            dtVolumeRow.Values.Add(double.NaN);
107          }
108          dtNrPointsRow.Values.Add(convexHull.Count);
109          centroidList.Add(ConvexHullMeasures.CalculateCentroids(convexHull));
110
111          //incrementally build complete convex hull
112          if (completeHull == null) {
113            completeHull = convexHull;
114          } else {
115            var newPHull = completeHull.Union(convexHull).ToArray();
116            completeHull = LPHull.Calculate(newPHull);
117          }
118
119          if (completeHull != null && completeHull.Any() && completeHull.First().Length < completeHull.Count) {
120            double completeHullVolume = ConvexHullMeasures.CalculateVolume(completeHull);
121            double ratio = completeHullVolume / hyperCubeVolume;
122            dtIncrementalHullVolume.Values.Add(completeHullVolume);
123            dtIncrementalHullVolumeRatio.Values.Add(ratio);
124          }
125
126          sols = solutionCache.GetSolutionsFromGeneration(++i);
127        }
128
129        if (completeHull != null && completeHull.Any() && completeHull.First().Length < completeHull.Count) {
130          overallVolume.Value = ConvexHullMeasures.CalculateVolume(completeHull);
131          overallVolumeRatio.Value = overallVolume.Value / hyperCubeVolume;
132        } else {
133          overallVolume.Value = double.NaN;
134          overallVolumeRatio.Value = double.NaN;
135        }
136
137        CalculateMovementDistances(dtCentroidDistances, centroidList, run);
138        CalculateMotionMeasures(dtCentroidMotion, centroidList, run);
139        CalculateCentroidDistanceFromOptimum(dtCentroidDistanceFromOptimum, centroidList, bestKnownSolution.ToArray());
140
141        run.Results["Overall volume"] = overallVolume;
142        run.Results["Overall volume ratio"] = overallVolumeRatio;
143        run.Results["Overall diameter"] = new DoubleValue(ConvexHullMeasures.CalculateMaxDiameter(completeHull));
144        run.Results["Convex hull volume"] = volDataTable;
145        run.Results["Nr. of points on convex hull"] = nrOfPointsTable;
146        run.Results[CentroidMovementDistancesName] = centroidDistancesTable;
147        run.Results[CentroidMotionName] = centroidMotionTable;
148        run.Results[IncrementalHullVolumeRatioName] = incrementalHullVolumeRatioTable;
149      }
150    }
151
152    private void CalculateCentroidDistanceFromOptimum(DataRow row, List<double[]> centroidList, double[] bestKnownSolution) {
153      foreach (var centroid in centroidList) {
154        double distance = centroid.EuclideanDistance(bestKnownSolution);
155        row.Values.Add(distance);
156      }
157    }
158
159    private void CalculateMovementDistances(DataRow row, List<double[]> centroidList, IRun run) {
160      var distances = ConvexHullMeasures.CalculateMovementDistances(centroidList);
161      foreach (var distance in distances) {
162        row.Values.Add(distance);
163      }
164      run.Results["Centroid Movement Distance Avg."] = new DoubleValue(distances.Average());
165      run.Results["Centroid Movement Distance Std.Dev."] = new DoubleValue(distances.StandardDeviation());
166      run.Results["Centroid Movement Distance Sum"] = new DoubleValue(distances.Sum());
167      run.Results["Centroid Movement Overall Distance"] = new DoubleValue(ConvexHullMeasures.CalculateOverallMovementDistances(centroidList));
168    }
169
170    private void CalculateMotionMeasures(DataRow row, List<double[]> centroidList, IRun run) {
171      var motions = ConvexHullMeasures.CalculateCentroidsMotion(centroidList).Select(y => Math.Abs(y)).ToList();
172      foreach (var motion in motions) {
173        row.Values.Add(motion);
174      }
175      run.Results["Centroid Motion Avg."] = new DoubleValue(motions.Average());
176      run.Results["Centroid Motion Std.Dev."] = new DoubleValue(motions.StandardDeviation());
177      run.Results["Centroid Motion Sum"] = new DoubleValue(motions.Sum());
178    }
179
180    private double[] ConvertRealVectorToVertexArray(RealVector p) {
181      double[] vertex = p.Select(x => (double)x).ToArray();
182      return vertex;
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.