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