Changeset 6338
- Timestamp:
- 05/30/11 17:21:30 (13 years ago)
- Location:
- branches/histogram/HeuristicLab.Analysis/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/histogram/HeuristicLab.Analysis/3.3/MultidimensionalScaling/MultidimensionalScaling.cs
r5933 r6338 68 68 /// In every iteration it tries to find the best location for every item.</param> 69 69 /// <returns>A Nx2 matrix where the first column represents the x- and the second column the y coordinates.</returns> 70 public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities, DoubleMatrix coordinates, int maximumIterations = 20) {70 public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities, DoubleMatrix coordinates, int maximumIterations = 10) { 71 71 int dimension = dissimilarities.Rows; 72 72 if (dimension != dissimilarities.Columns || coordinates.Rows != dimension) throw new ArgumentException("The number of coordinates and the number of rows and columns in the dissimilarities matrix do not match."); … … 75 75 double epsf = 0; 76 76 double epsx = 0; 77 int maxits = 100; 78 alglib.mincgstate state = null; 79 alglib.mincgreport rep; 77 int maxits = 0; 80 78 79 alglib.minlmstate state; 80 alglib.minlmreport rep; 81 81 for (int iterations = 0; iterations < maximumIterations; iterations++) { 82 82 bool changed = false; … … 85 85 86 86 try { 87 if ((iterations == 0 && i == 0)) { 88 alglib.mincgcreate(c, out state); 89 alglib.mincgsetcond(state, epsg, epsf, epsx, maxits); 90 } else { 91 alglib.mincgrestartfrom(state, c); 92 } 93 alglib.mincgoptimize(state, StressGradient, null, new Info(coordinates, dissimilarities, i)); 94 alglib.mincgresults(state, out c, out rep); 87 alglib.minlmcreatevj(dimension - 1, c, out state); 88 alglib.minlmsetcond(state, epsg, epsf, epsx, maxits); 89 alglib.minlmoptimize(state, StressFitness, StressJacobian, null, new Info(coordinates, dissimilarities, i)); 90 alglib.minlmresults(state, out c, out rep); 95 91 } catch (alglib.alglibexception) { } 96 92 if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) { … … 105 101 } 106 102 107 // computes the function and the gradient of the raw stress function. 108 private static void StressGradient(double[] x, ref double func, double[] grad, object obj) { 109 func = 0; grad[0] = 0; grad[1] = 0; 103 private static void StressFitness(double[] x, double[] fi, object obj) { 110 104 Info info = (obj as Info); 111 105 for (int i = 0; i < info.Coordinates.Rows; i++) { 112 double c = info.Dissimilarities[info.Row, i]; 106 double f = Stress(x, info.Dissimilarities[info.Row, i], info.Coordinates[i, 0], info.Coordinates[i, 1]); 107 if (i < info.Row) fi[i] = f; 108 else if (i > info.Row) fi[i - 1] = f; 109 } 110 } 111 112 private static void StressJacobian(double[] x, double[] fi, double[,] jac, object obj) { 113 Info info = (obj as Info); 114 int idx = 0; 115 for (int i = 0; i < info.Coordinates.Rows; i++) { 113 116 if (i != info.Row) { 117 double c = info.Dissimilarities[info.Row, i]; 114 118 double a = info.Coordinates[i, 0]; 115 119 double b = info.Coordinates[i, 1]; 116 func += Stress(x, c, a, b); 117 grad[0] += ((2 * x[0] - 2 * a) * Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a) - 2 * c * x[0] + 2 * a * c) / Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a); 118 grad[1] += ((2 * x[1] - 2 * b) * Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a) - 2 * c * x[1] + 2 * b * c) / Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a); 120 double f = Stress(x, c, a, b); 121 fi[idx] = f; 122 jac[idx, 0] = 2 * (x[0] - a) * (Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])) - c) / Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])); 123 jac[idx, 1] = 2 * (x[1] - b) * (Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])) - c) / Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])); 124 idx++; 119 125 } 120 126 } -
branches/histogram/HeuristicLab.Analysis/3.3/Tests/MultidimensionalScalingTest.cs
r5938 r6338 21 21 22 22 using System; 23 using System.Globalization; 23 24 using HeuristicLab.Data; 24 25 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 42 43 distances3[0, 2] = distances3[2, 0] = 6.4; 43 44 distances3[1, 2] = distances3[2, 1] = 5; 44 stress = MultidimensionalScaling.CalculateNormalizedStress(distances3, 45 MultidimensionalScaling.KruskalShepard(distances3)); 45 DoubleMatrix coords3 = MultidimensionalScaling.KruskalShepard(distances3); 46 Console.WriteLine("Coordinates: "); 47 Console.WriteLine("A = ({0}, {1}), B = ({2}, {3}), C = ({4}, {5})", coords3[0, 0], coords3[0, 1], coords3[1, 0], coords3[1, 1], coords3[2, 0], coords3[2, 1]); 48 stress = MultidimensionalScaling.CalculateNormalizedStress(distances3, coords3); 49 Console.WriteLine("Stress = " + stress.ToString(CultureInfo.InvariantCulture.NumberFormat)); 46 50 Assert.IsTrue(stress < 0.1); 47 51 DoubleMatrix distances4 = new DoubleMatrix(4, 4);
Note: See TracChangeset
for help on using the changeset viewer.