Changeset 10081
- Timestamp:
- 10/23/13 15:56:50 (11 years ago)
- Location:
- branches/HeuristicLab.Analysis.AlgorithmBehavior
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/ConvexHullTest.cs
r9945 r10081 28 28 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 29 30 namespace Visualization{30 namespace AlgorithmBehaviorUnitTests { 31 31 [TestClass] 32 32 public class ConvexHullTest { -
branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/LPConvexHullTest.cs
r10060 r10081 28 28 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 29 30 namespace Visualization{30 namespace AlgorithmBehaviorUnitTests { 31 31 [TestClass] 32 32 public class LPConvexHullTest { 33 33 [TestMethod] 34 34 public void TestMethod1() { 35 int nrOfSamples = 10;35 int nrOfSamples = 70; 36 36 int sampleSize = 2; 37 37 double[][] inputs = CreateRandomData(nrOfSamples, sampleSize); … … 70 70 } 71 71 72 [TestMethod] 73 public void TestExt() { 74 var inputs = CreateDataExtremePoint1().ToList(); 75 double[] alpha = inputs.Last(); 76 bool result = LPHull.EXT(inputs, alpha, inputs.Count() - 1); 77 Assert.IsTrue(result); 78 79 inputs = CreateDataExtremePoint2().ToList(); 80 alpha = inputs.Last(); 81 result = LPHull.EXT(inputs, alpha, inputs.Count() - 1); 82 Assert.IsTrue(result); 83 84 inputs = CreateDataNonExtremePoint1().ToList(); 85 alpha = inputs.Last(); 86 result = LPHull.EXT(inputs, alpha, inputs.Count() - 1); 87 Assert.IsFalse(result); 88 89 inputs = CreateDataOnHull().ToList(); 90 alpha = inputs.Last(); 91 result = LPHull.EXT(inputs, alpha, inputs.Count() - 1); 92 Assert.IsFalse(result); 93 } 94 95 private double[][] CreateDataExtremePoint1() { 96 double[][] result = new double[5][]; 97 98 result[0] = new double[] { 0.1, 0.1 }; 99 result[1] = new double[] { 1, 1 }; 100 result[2] = new double[] { 1, 0 }; 101 result[3] = new double[] { 0, 1 }; 102 result[4] = new double[] { 2.0, 1.4 }; 103 104 return result; 105 } 106 107 private double[][] CreateDataExtremePoint2() { 108 double[][] result = new double[5][]; 109 110 result[0] = new double[] { 0.1, 0.1 }; 111 result[1] = new double[] { 1, 1 }; 112 result[2] = new double[] { 1, 0 }; 113 result[3] = new double[] { 0, 1 }; 114 result[4] = new double[] { 1.0, 1.4 }; 115 116 return result; 117 } 118 119 private double[][] CreateDataNonExtremePoint1() { 120 double[][] result = new double[5][]; 121 122 result[0] = new double[] { 0.1, 0.1 }; 123 result[1] = new double[] { 1, 1 }; 124 result[2] = new double[] { 1, 0 }; 125 result[3] = new double[] { 0, 1 }; 126 result[4] = new double[] { 0.8, 0.4 }; 127 128 return result; 129 } 130 131 private double[][] CreateDataOnHull() { 132 double[][] result = new double[5][]; 133 134 result[0] = new double[] { 0.1, 0.1 }; 135 result[1] = new double[] { 1, 1 }; 136 result[2] = new double[] { 1, 0 }; 137 result[3] = new double[] { 0, 1 }; 138 result[4] = new double[] { 1.0, 0.5 }; 139 140 return result; 141 } 142 72 143 private List<DefaultVertex> ConvertPermutationToVertex(double[][] data) { 73 144 List<DefaultVertex> result = new List<DefaultVertex>(); -
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/LPHull.cs
r10060 r10081 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Common; 25 26 26 27 namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { … … 44 45 C.Add(A[0]); 45 46 for (int i = 1; i < A.Length; i++) { 46 if (EXT(C, A[i])) { 47 C.Add(A[i]); 47 C.Add(A[i]); 48 if (!EXT(C, A[i], C.Count - 1)) { 49 C.Remove(A[i]); 48 50 } 49 51 } … … 51 53 //Phase 3 52 54 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 if (EXT(C, C[i], i)) { 55 56 E.Add(C[i]); 56 57 } … … 60 61 } 61 62 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 63 //sort A decreasing by distance to center of polytope 79 64 public static double[][] SortA(double[][] A) { 80 //TODO: sort inplace81 65 int length = A[0].Length; 82 66 double[] maxs = new double[length]; … … 121 105 /// Returns true if alpha is an extreme point, else false. 122 106 /// </summary> 123 p rivatestatic bool EXT(List<double[]> A, double[] alpha, int aIdx = -1) {107 public static bool EXT(List<double[]> A, double[] alpha, int aIdx = -1) { 124 108 alglib.minbleicstate state; 125 double diffstep = 1.0e-6;126 109 int N = alpha.Length; 127 110 int K = A.Count; 128 129 130 double[] init = new double[N]; 131 double[] lowerBound = new double[N]; 132 double[] upperBound = new double[N]; 111 double[] init = new double[K]; 112 double[] lowerBound = new double[K]; 113 double[] upperBound = new double[K]; 133 114 double[] x; 134 115 alglib.minbleicreport rep; 135 double[,] c = new double[ K + 1, N+ 1];136 int[] ct = new int[ K+ 1]; //init with 0, means equal116 double[,] c = new double[N + 1, K + 1]; 117 int[] ct = new int[N + 1]; //init with 0, means equal 137 118 138 for (int i = 0; i < N; i++) { 139 //TODO: this should be a feasible solution 140 init[i] = 1.0; 119 for (int i = 0; i < K; i++) { 120 init[i] = 1.0 / K; 141 121 lowerBound[i] = 0.0; 142 122 upperBound[i] = double.MaxValue; … … 144 124 145 125 //last column gets b 146 for (int i = 0; i < K; i++) {147 c[i, N] = alpha[i];126 for (int i = 0; i < N; i++) { 127 c[i, K] = alpha[i]; 148 128 } 149 129 //sum(x) == 1 constraint 150 for (int i = 0; i < N+ 1; i++) {151 c[ K, i] = 1.0;130 for (int i = 0; i < K + 1; i++) { 131 c[N, i] = 1.0; 152 132 } 153 133 134 //other constraints 154 135 for (int i = 0; i < K; i++) { 155 136 for (int j = 0; j < N; j++) { … … 158 139 } 159 140 160 alglib.minbleiccreate f(init, diffstep, out state);141 alglib.minbleiccreate(init, out state); 161 142 alglib.minbleicsetbc(state, lowerBound, upperBound); 162 143 alglib.minbleicsetlc(state, c, ct); 163 144 alglib.minbleicsetcond(state, 0.0, 0.0, 0.0, 0); 164 alglib.minbleicoptimize(state, Func, null, new Tuple<List<double[]>, int>(A, aIdx));145 alglib.minbleicoptimize(state, Func, null, new Tuple<List<double[]>, double[], int>(A, alpha, aIdx)); 165 146 alglib.minbleicresults(state, out x, out rep); 166 147 167 return x. Sum() > 0;148 return x.Count(y => y.IsAlmost(1.0)) == 1; 168 149 } 169 150 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; 151 private static void Func(double[] x, ref double func, double[] grad, object obj) { 152 Tuple<List<double[]>, double[], int> data = obj as Tuple<List<double[]>, double[], int>; 153 int aIdx = data.Item3; 154 double[] alpha = data.Item2; 173 155 List<double[]> A = data.Item1; 174 156 175 //TODO: I have no idea what I'm doing... 157 //well, this seems to work but is probably not correct 158 for (int i = 0; i < grad.Length; i++) { 159 grad[i] = 0; 160 } 161 grad[aIdx] = 1; 162 176 163 func = 0.0; 177 164 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 } 165 for (int j = 0; j < A[i].Length; j++) { 166 func += A[i][j] * x[i]; 182 167 } 183 168 }
Note: See TracChangeset
for help on using the changeset viewer.