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 MIConvexHull;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
28 | public static class ConvexHullMeasures {
|
---|
29 | //Calculates the volumne of a d-simplex
|
---|
30 | private static double CalculateSimplexVolume(List<double[]> simplex) {
|
---|
31 | double[,] diffs = new double[simplex.Count, simplex.Count];
|
---|
32 |
|
---|
33 | for (int i = 0; i < simplex.Count; i++) {
|
---|
34 | for (int j = 0; j < simplex.Count; j++) {
|
---|
35 | diffs[i, j] = simplex[i].EuclideanDistance(simplex[j]);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | double det = alglib.rmatrixdet(diffs);
|
---|
40 | double result = det / simplex[0].Count().Fact();
|
---|
41 |
|
---|
42 | return result;
|
---|
43 | }
|
---|
44 |
|
---|
45 | //calculate center for boundary triangulation (slowwwww.....)
|
---|
46 | private static double[] CalculateInnerPoint(List<double[]> hyperHull) {
|
---|
47 | double[] result = new double[hyperHull[0].Count()];
|
---|
48 |
|
---|
49 | for (int i = 0; i < hyperHull[0].Count(); i++) {
|
---|
50 | result[i] = (hyperHull.Max(x => x[i]) + hyperHull.Min(x => x[i])) / 2.0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | return result;
|
---|
54 | }
|
---|
55 |
|
---|
56 | //calculates the volume of a convex d-polytope
|
---|
57 | //decomposition based on boundary triangulation
|
---|
58 | public static double CalculateVolume(List<double[]> convexHull) {
|
---|
59 | double[] innerPoint = CalculateInnerPoint(convexHull);
|
---|
60 | double volume = 0.0;
|
---|
61 |
|
---|
62 | for (int i = 0; i < convexHull.Count - 1; i += 2) {
|
---|
63 | List<double[]> simplex = new List<double[]>();
|
---|
64 | simplex.Add(innerPoint);
|
---|
65 | simplex.Add(convexHull[i]);
|
---|
66 | simplex.Add(convexHull[i + 1]);
|
---|
67 | volume += CalculateSimplexVolume(simplex);
|
---|
68 | }
|
---|
69 | return volume;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static double CalculateVolumeNew(List<double[]> convexHull) {
|
---|
73 | double volume = 0.0;
|
---|
74 |
|
---|
75 | int dim = convexHull.First().Length;
|
---|
76 | if (dim > convexHull.Count)
|
---|
77 | throw new ArgumentException("Nr. of points for volume calculation must be greater than dimension", "convexHull");
|
---|
78 |
|
---|
79 | var triangulation = Triangulation.CreateDelaunay(ConvertToVertex(convexHull));
|
---|
80 | var simplices = ConvertFromTriangulation(triangulation);
|
---|
81 | foreach (var simplex in simplices) {
|
---|
82 | volume += CalculateSimplexVolume(simplex.ToList());
|
---|
83 | }
|
---|
84 |
|
---|
85 | return volume;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private static List<double[][]> ConvertFromTriangulation(ITriangulation<DefaultVertex, DefaultTriangulationCell<DefaultVertex>> triangulation) {
|
---|
89 | List<double[][]> results = new List<double[][]>();
|
---|
90 |
|
---|
91 | foreach (var cell in triangulation.Cells) {
|
---|
92 | results.Add(cell.Vertices.Select(x => x.Position).ToArray());
|
---|
93 | }
|
---|
94 |
|
---|
95 | return results;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static List<DefaultVertex> ConvertToVertex(List<double[]> data) {
|
---|
99 | List<DefaultVertex> result = new List<DefaultVertex>();
|
---|
100 | for (int i = 0; i < data.Count(); i++) {
|
---|
101 | double[] d = data[i];
|
---|
102 | DefaultVertex vertex = new DefaultVertex();
|
---|
103 | vertex.Position = d;
|
---|
104 | result.Add(vertex);
|
---|
105 | }
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|