Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Analysis/3.3/MultidimensionalScaling/MultidimensionalScaling.cs @ 18190

Last change on this file since 18190 was 17931, checked in by gkronber, 4 years ago

#3117: update alglib to version 3.17

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HeuristicLab.Data;
24
25namespace HeuristicLab.Analysis {
26  public static class MultidimensionalScaling {
27
28    /// <summary>
29    /// Performs the Kruskal-Shepard algorithm and applies a gradient descent method
30    /// to fit the coordinates such that the difference between the fit distances
31    /// and the dissimilarities becomes minimal.
32    /// </summary>
33    /// <remarks>
34    /// It will initialize the coordinates in a deterministic fashion such that all initial points are equally spaced on a circle.
35    /// </remarks>
36    /// <param name="dissimilarities">A symmetric NxN matrix that specifies the dissimilarities between each element i and j. Diagonal elements are ignored.</param>
37    ///
38    /// <returns>A Nx2 matrix where the first column represents the x- and the second column the y coordinates.</returns>
39    public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities) {
40      if (dissimilarities == null) throw new ArgumentNullException("dissimilarities");
41      if (dissimilarities.Rows != dissimilarities.Columns) throw new ArgumentException("Dissimilarities must be a square matrix.", "dissimilarities");
42
43      int dimension = dissimilarities.Rows;
44      if (dimension == 1) return new DoubleMatrix(new double[,] { { 0, 0 } });
45      else if (dimension == 2) return new DoubleMatrix(new double[,] { { 0, 0 }, { 0, dissimilarities[0, 1] } });
46
47      DoubleMatrix coordinates = new DoubleMatrix(dimension, 2);
48      double rad = (2 * Math.PI) / coordinates.Rows;
49      for (int i = 0; i < dimension; i++) {
50        coordinates[i, 0] = 10 * Math.Cos(rad * i);
51        coordinates[i, 1] = 10 * Math.Sin(rad * i);
52      }
53
54      return KruskalShepard(dissimilarities, coordinates);
55    }
56
57    /// <summary>
58    /// Performs the Kruskal-Shepard algorithm and applies a gradient descent method
59    /// to fit the coordinates such that the difference between the fit distances
60    /// and the dissimilarities is minimal.
61    /// </summary>
62    /// <remarks>
63    /// It will use a pre-initialized x,y-coordinates matrix as a starting point of the gradient descent.
64    /// </remarks>
65    /// <param name="dissimilarities">A symmetric NxN matrix that specifies the dissimilarities between each element i and j. Diagonal elements are ignored.</param>
66    /// <param name="coordinates">The Nx2 matrix of initial coordinates.</param>
67    /// <param name="maximumIterations">The number of iterations for which the algorithm should run.
68    /// In every iteration it tries to find the best location for every item.</param>
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 = 10) {
71      int dimension = dissimilarities.Rows;
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.");
73
74      double epsx = 0;
75      int maxits = 0;
76
77      alglib.minlmstate state;
78      alglib.minlmreport rep;
79      for (int iterations = 0; iterations < maximumIterations; iterations++) {
80        bool changed = false;
81        for (int i = 0; i < dimension; i++) {
82          double[] c = new double[] { coordinates[i, 0], coordinates[i, 1] };
83
84          try {
85            alglib.minlmcreatevj(dimension - 1, c, out state);
86            alglib.minlmsetcond(state, epsx, maxits);
87            alglib.minlmoptimize(state, StressFitness, StressJacobian, null, new Info(coordinates, dissimilarities, i));
88            alglib.minlmresults(state, out c, out rep);
89          } catch (alglib.alglibexception) { }
90          if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) {
91            changed = changed || (coordinates[i, 0] != c[0]) || (coordinates[i, 1] != c[1]);
92            coordinates[i, 0] = c[0];
93            coordinates[i, 1] = c[1];
94          }
95        }
96        if (!changed) break;
97      }
98      return coordinates;
99    }
100
101    private static void StressFitness(double[] x, double[] fi, object obj) {
102      Info info = (obj as Info);
103      int idx = 0;
104      for (int i = 0; i < info.Coordinates.Rows; i++) {
105        if (i == info.Row) continue;
106        if (!double.IsNaN(info.Dissimilarities[info.Row, i]))
107          fi[idx++] = Stress(x, info.Dissimilarities[info.Row, i], info.Coordinates[i, 0], info.Coordinates[i, 1]);
108        else fi[idx++] = 0.0;
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++) {
116        if (i == info.Row) continue;
117        double c = info.Dissimilarities[info.Row, i];
118        double a = info.Coordinates[i, 0];
119        double b = info.Coordinates[i, 1];
120        if (!double.IsNaN(c)) {
121          fi[idx] = Stress(x, c, a, b); ;
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        } else {
125          fi[idx] = jac[idx, 0] = jac[idx, 1] = 0;
126        }
127        idx++;
128      }
129    }
130
131    private static double Stress(double[] x, double distance, double xCoord, double yCoord) {
132      return Stress(x[0], x[1], distance, xCoord, yCoord);
133    }
134
135    private static double Stress(double x, double y, double distance, double otherX, double otherY) {
136      double d = Math.Sqrt((x - otherX) * (x - otherX)
137                         + (y - otherY) * (y - otherY));
138      return (d - distance) * (d - distance);
139    }
140
141    /// <summary>
142    /// This method computes the normalized raw-stress value according to Groenen and van de Velden 2004. "Multidimensional Scaling". Technical report EI 2004-15.
143    /// </summary>
144    /// <remarks>
145    /// Throws an ArgumentException when the <paramref name="dissimilarities"/> matrix is not symmetric.
146    /// </remarks>
147    ///
148    /// <param name="dissimilarities">The matrix with the dissimilarities.</param>
149    /// <param name="coordinates">The actual location of the points.</param>
150    /// <returns>The normalized raw-stress value that describes the goodness-of-fit between the distances in the points and the size of the dissimilarities. If the value is &lt; 0.1 the fit is generally considered good. If between 0.1 and 0.2 it is considered acceptable, but the usefulness of the scaling with higher values is doubtful.</returns>
151    public static double CalculateNormalizedStress(DoubleMatrix dissimilarities, DoubleMatrix coordinates) {
152      int dimension = dissimilarities.Rows;
153      if (dimension != dissimilarities.Columns || dimension != coordinates.Rows) throw new ArgumentException("The number of coordinates and the number of rows and columns in the dissimilarities matrix do not match.");
154      double stress = 0, normalization = 0;
155      for (int i = 0; i < dimension - 1; i++) {
156        for (int j = i + 1; j < dimension; j++) {
157          if (dissimilarities[i, j] != dissimilarities[j, i] && !(double.IsNaN(dissimilarities[i, j]) && double.IsNaN(dissimilarities[j, i])))
158            throw new ArgumentException("Dissimilarities is not a symmetric matrix.", "dissimilarities");
159          if (!double.IsNaN(dissimilarities[i, j])) {
160            stress += Stress(coordinates[i, 0], coordinates[i, 1], dissimilarities[i, j], coordinates[j, 0], coordinates[j, 1]);
161            normalization += (dissimilarities[i, j] * dissimilarities[i, j]);
162          }
163        }
164      }
165      return stress / normalization;
166    }
167
168    private class Info {
169      public DoubleMatrix Coordinates { get; set; }
170      public DoubleMatrix Dissimilarities { get; set; }
171      public int Row { get; set; }
172
173      public Info(DoubleMatrix c, DoubleMatrix d, int r) {
174        Coordinates = c;
175        Dissimilarities = d;
176        Row = r;
177      }
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.