Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10082 was 10082, checked in by ascheibe, 10 years ago

#1886 correction of LPHull Func

File size: 4.9 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        C.Add(A[i]);
47        if (!EXT(C, A[i], C.Count - 1)) {
48          C.Remove(A[i]);
49        }
50      }
51
52      //Phase 3
53      for (int i = 0; i < C.Count; i++) {
54        if (EXT(C, C[i], i)) {
55          E.Add(C[i]);
56        }
57      }
58
59      return E;
60    }
61
62    //sort A decreasing by distance to center of polytope
63    public static double[][] SortA(double[][] A) {
64      int length = A[0].Length;
65      double[] maxs = new double[length];
66      double[] mins = new double[length];
67      double[][] sortedA = new double[A.Length][];
68
69      for (int i = 0; i < maxs.Length; i++) {
70        maxs[i] = double.MinValue;
71        mins[i] = double.MaxValue;
72      }
73
74      for (int i = 0; i < A.Length; i++) {
75        for (int j = 0; j < A[i].Length; j++) {
76          if (A[i][j] > maxs[j]) maxs[j] = A[i][j];
77          if (A[i][j] < mins[j]) mins[j] = A[i][j];
78        }
79      }
80
81      double[] d = new double[length];
82      double[] r = new double[length];
83
84      //calculate center
85      for (int i = 0; i < length; i++) {
86        d[i] = (maxs[i] + mins[i]) / 2.0;
87      }
88
89      //calculate length of sides of rectangle
90      for (int i = 0; i < length; i++) {
91        r[i] = maxs[i] - mins[i];
92      }
93
94      VertexComparer comparer = new VertexComparer();
95      comparer.d = d;
96      comparer.r = r;
97      sortedA = A.OrderByDescending(x => x, comparer).ToArray();
98
99      return sortedA;
100    }
101
102    /// <summary>
103    /// Checks if alpha is an extreme point (lies on convex hull) of A.
104    /// Returns true if alpha is an extreme point, else false.
105    /// </summary>
106    public static bool EXT(List<double[]> A, double[] alpha, int aIdx = -1) {
107      alglib.minbleicstate state;
108      int N = alpha.Length;
109      int K = A.Count;
110      double[] init = new double[K];
111      double[] lowerBound = new double[K];
112      double[] upperBound = new double[K];
113      double[] x;
114      alglib.minbleicreport rep;
115      double[,] c = new double[N + 1, K + 1];
116      int[] ct = new int[N + 1]; //init with 0, means equal
117
118      for (int i = 0; i < K; i++) {
119        init[i] = 0.0;
120        lowerBound[i] = 0.0;
121        upperBound[i] = double.MaxValue;
122      }
123      init[aIdx] = 1.0;
124
125      //last column gets b
126      for (int i = 0; i < N; i++) {
127        c[i, K] = alpha[i];
128      }
129      //sum(x) == 1 constraint
130      for (int i = 0; i < K + 1; i++) {
131        c[N, i] = 1.0;
132      }
133
134      //other constraints
135      for (int i = 0; i < K; i++) {
136        for (int j = 0; j < N; j++) {
137          c[j, i] = A[i][j];
138        }
139      }
140
141      alglib.minbleiccreate(init, out state);
142      alglib.minbleicsetbc(state, lowerBound, upperBound);
143      alglib.minbleicsetlc(state, c, ct);
144      alglib.minbleicsetcond(state, 0.0, 0.0, 0.0, 0);
145      alglib.minbleicoptimize(state, Func, null, aIdx);
146      alglib.minbleicresults(state, out x, out rep);
147
148      if (rep.terminationtype < 0) throw new ArgumentException("minbleic terminated with error");
149      if (rep.terminationtype == 5) Console.WriteLine("max number of iterations reached in minbleic");
150      return x[aIdx] >= 1.0;
151    }
152
153    private static void Func(double[] x, ref double func, double[] grad, object obj) {
154      int aIdx = (int)obj;
155
156      func = x[aIdx];
157      for (int i = 0; i < grad.Length; i++) {
158        grad[i] = 0;
159      }
160      grad[aIdx] = 1;
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.