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 | public static double CalculateSimplexVolume(List<double[]> simplex) {
|
---|
31 | int dim = simplex.First().Length;
|
---|
32 | double[,] diffs = new double[dim, dim];
|
---|
33 |
|
---|
34 | for (int i = 1; i < simplex.Count; i++) {
|
---|
35 | for (int j = 0; j < dim; j++) {
|
---|
36 | diffs[j, i - 1] = simplex[i][j] - simplex[0][j];
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | double det = Math.Abs(alglib.rmatrixdet(diffs));
|
---|
41 | double result = det / dim.Fact();
|
---|
42 |
|
---|
43 | return result;
|
---|
44 | }
|
---|
45 |
|
---|
46 | //calculates the volume of a convex d-polytope
|
---|
47 | //decomposition based on delaunay triangulation
|
---|
48 | public static double CalculateVolume(List<double[]> convexHull) {
|
---|
49 | double volume = 0.0;
|
---|
50 |
|
---|
51 | int dim = convexHull.First().Length;
|
---|
52 | if (dim > convexHull.Count)
|
---|
53 | throw new ArgumentException("Nr. of points for volume calculation must be greater than dimension", "convexHull");
|
---|
54 |
|
---|
55 | var triangulation = Triangulation.CreateDelaunay(ConvertToVertex(convexHull));
|
---|
56 | var simplices = ConvertFromTriangulation(triangulation);
|
---|
57 | foreach (var simplex in simplices) {
|
---|
58 | volume += CalculateSimplexVolume(simplex.ToList());
|
---|
59 | }
|
---|
60 |
|
---|
61 | return volume;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static List<double[][]> ConvertFromTriangulation(ITriangulation<DefaultVertex, DefaultTriangulationCell<DefaultVertex>> triangulation) {
|
---|
65 | List<double[][]> results = new List<double[][]>();
|
---|
66 |
|
---|
67 | foreach (var cell in triangulation.Cells) {
|
---|
68 | results.Add(cell.Vertices.Select(x => x.Position).ToArray());
|
---|
69 | }
|
---|
70 |
|
---|
71 | return results;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private static List<DefaultVertex> ConvertToVertex(List<double[]> data) {
|
---|
75 | List<DefaultVertex> result = new List<DefaultVertex>();
|
---|
76 | for (int i = 0; i < data.Count(); i++) {
|
---|
77 | double[] d = data[i];
|
---|
78 | DefaultVertex vertex = new DefaultVertex();
|
---|
79 | vertex.Position = d;
|
---|
80 | result.Add(vertex);
|
---|
81 | }
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|