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