Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/LPHull.cs @ 10060

Last change on this file since 10060 was 10060, checked in by ascheibe, 11 years ago

#1886

  • added point calculation from distance matrices
  • added convex hull algorithm based on LP (work in progress)
  • added unit tests
File size: 5.5 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;
25
26namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
27  /*
28   * Calculate Convex Hull using Linear Programming as described in
29   * J.B. Rosen et. al., 1989,  Efficient computation of convex hull in Rd
30   * and
31   * P.M. Pardalos et. al., 1995,
32   * Linear programming approaches to the convex hull problem in Rm
33   *
34   */
35  public static class LPHull {
36    public static List<double[]> Calculate(double[][] inputs) {
37      List<double[]> C = new List<double[]>();
38      List<double[]> E = new List<double[]>();
39
40      //Phase 1
41      double[][] A = SortA(inputs);
42
43      //Phase 2
44      C.Add(A[0]);
45      for (int i = 1; i < A.Length; i++) {
46        if (EXT(C, A[i])) {
47          C.Add(A[i]);
48        }
49      }
50
51      //Phase 3
52      for (int i = 0; i < C.Count; i++) {
53        //TODO: this is not very efficient
54        if (EXT(ExcludeRow(C, i), C[i], i)) {
55          E.Add(C[i]);
56        }
57      }
58
59      return E;
60    }
61
62    private static List<double[]> ExcludeRow(List<double[]> matrix, int row) {
63      List<double[]> result = new List<double[]>();
64
65      for (int i = 0; i < matrix.Count; i++) {
66        if (i != row) {
67          var tmpRow = new double[matrix[i].Length];
68          result.Add(tmpRow);
69          for (int j = 0; j < matrix[i].Length; j++) {
70            tmpRow[j] = matrix[i][j];
71          }
72        }
73      }
74
75      return result;
76    }
77
78    //sort A decreasing by distance to center of polytope
79    public static double[][] SortA(double[][] A) {
80      //TODO: sort inplace
81      int length = A[0].Length;
82      double[] maxs = new double[length];
83      double[] mins = new double[length];
84      double[][] sortedA = new double[A.Length][];
85
86      for (int i = 0; i < maxs.Length; i++) {
87        maxs[i] = double.MinValue;
88        mins[i] = double.MaxValue;
89      }
90
91      for (int i = 0; i < A.Length; i++) {
92        for (int j = 0; j < A[i].Length; j++) {
93          if (A[i][j] > maxs[j]) maxs[j] = A[i][j];
94          if (A[i][j] < mins[j]) mins[j] = A[i][j];
95        }
96      }
97
98      double[] d = new double[length];
99      double[] r = new double[length];
100
101      //calculate center
102      for (int i = 0; i < length; i++) {
103        d[i] = (maxs[i] + mins[i]) / 2.0;
104      }
105
106      //calculate length of sides of rectangle
107      for (int i = 0; i < length; i++) {
108        r[i] = maxs[i] - mins[i];
109      }
110
111      VertexComparer comparer = new VertexComparer();
112      comparer.d = d;
113      comparer.r = r;
114      sortedA = A.OrderByDescending(x => x, comparer).ToArray();
115
116      return sortedA;
117    }
118
119    /// <summary>
120    /// Checks if alpha is an extreme point (lies on convex hull) of A.
121    /// Returns true if alpha is an extreme point, else false.
122    /// </summary>
123    private static bool EXT(List<double[]> A, double[] alpha, int aIdx = -1) {
124      alglib.minbleicstate state;
125      double diffstep = 1.0e-6;
126      int N = alpha.Length;
127      int K = A.Count;
128
129
130      double[] init = new double[N];
131      double[] lowerBound = new double[N];
132      double[] upperBound = new double[N];
133      double[] x;
134      alglib.minbleicreport rep;
135      double[,] c = new double[K + 1, N + 1];
136      int[] ct = new int[K + 1]; //init with 0, means equal
137
138      for (int i = 0; i < N; i++) {
139        //TODO: this should be a feasible solution   
140        init[i] = 1.0;
141        lowerBound[i] = 0.0;
142        upperBound[i] = double.MaxValue;
143      }
144
145      //last column gets b
146      for (int i = 0; i < K; i++) {
147        c[i, N] = alpha[i];
148      }
149      //sum(x) == 1 constraint
150      for (int i = 0; i < N + 1; i++) {
151        c[K, i] = 1.0;
152      }
153
154      for (int i = 0; i < K; i++) {
155        for (int j = 0; j < N; j++) {
156          c[j, i] = A[i][j];
157        }
158      }
159
160      alglib.minbleiccreatef(init, diffstep, out state);
161      alglib.minbleicsetbc(state, lowerBound, upperBound);
162      alglib.minbleicsetlc(state, c, ct);
163      alglib.minbleicsetcond(state, 0.0, 0.0, 0.0, 0);
164      alglib.minbleicoptimize(state, Func, null, new Tuple<List<double[]>, int>(A, aIdx));
165      alglib.minbleicresults(state, out x, out rep);
166
167      return x.Sum() > 0;
168    }
169
170    private static void Func(double[] x, ref double func, object obj) {
171      Tuple<List<double[]>, int> data = obj as Tuple<List<double[]>, int>;
172      int aIdx = data.Item2;
173      List<double[]> A = data.Item1;
174
175      //TODO: I have no idea what I'm doing...
176      func = 0.0;
177      for (int i = 0; i < A.Count; i++) {
178        if (i != aIdx) {
179          for (int j = 0; j < A[i].Length; j++) {
180            func += A[i][j] * x[i];
181          }
182        }
183      }
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.