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
RevLine 
[5723]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5723]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
[5931]31    /// and the dissimilarities becomes minimal.
[5723]32    /// </summary>
[5871]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>
[5931]36    /// <param name="dissimilarities">A symmetric NxN matrix that specifies the dissimilarities between each element i and j. Diagonal elements are ignored.</param>
[5871]37    ///
[5723]38    /// <returns>A Nx2 matrix where the first column represents the x- and the second column the y coordinates.</returns>
[5871]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");
[5723]42
[5871]43      int dimension = dissimilarities.Rows;
[5723]44      if (dimension == 1) return new DoubleMatrix(new double[,] { { 0, 0 } });
[5931]45      else if (dimension == 2) return new DoubleMatrix(new double[,] { { 0, 0 }, { 0, dissimilarities[0, 1] } });
[5723]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
[5871]54      return KruskalShepard(dissimilarities, coordinates);
[5723]55    }
56
[5871]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
[5931]60    /// and the dissimilarities is minimal.
[5871]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>
[5931]65    /// <param name="dissimilarities">A symmetric NxN matrix that specifies the dissimilarities between each element i and j. Diagonal elements are ignored.</param>
[5871]66    /// <param name="coordinates">The Nx2 matrix of initial coordinates.</param>
[5931]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>
[5871]69    /// <returns>A Nx2 matrix where the first column represents the x- and the second column the y coordinates.</returns>
[6342]70    public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities, DoubleMatrix coordinates, int maximumIterations = 10) {
[5871]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.");
[5723]73
74      double epsx = 0;
[6342]75      int maxits = 0;
[5723]76
[6342]77      alglib.minlmstate state;
78      alglib.minlmreport rep;
[5931]79      for (int iterations = 0; iterations < maximumIterations; iterations++) {
80        bool changed = false;
[5723]81        for (int i = 0; i < dimension; i++) {
82          double[] c = new double[] { coordinates[i, 0], coordinates[i, 1] };
83
[5855]84          try {
[6342]85            alglib.minlmcreatevj(dimension - 1, c, out state);
[17931]86            alglib.minlmsetcond(state, epsx, maxits);
[6342]87            alglib.minlmoptimize(state, StressFitness, StressJacobian, null, new Info(coordinates, dissimilarities, i));
88            alglib.minlmresults(state, out c, out rep);
[5871]89          } catch (alglib.alglibexception) { }
[5855]90          if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) {
[5931]91            changed = changed || (coordinates[i, 0] != c[0]) || (coordinates[i, 1] != c[1]);
[5855]92            coordinates[i, 0] = c[0];
93            coordinates[i, 1] = c[1];
[5723]94          }
95        }
[5931]96        if (!changed) break;
[5723]97      }
98      return coordinates;
99    }
100
[6342]101    private static void StressFitness(double[] x, double[] fi, object obj) {
[5723]102      Info info = (obj as Info);
[7416]103      int idx = 0;
[5723]104      for (int i = 0; i < info.Coordinates.Rows; i++) {
[7416]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;
[6342]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++) {
[7416]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); ;
[6342]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]));
[7416]124        } else {
125          fi[idx] = jac[idx, 0] = jac[idx, 1] = 0;
[5723]126        }
[7416]127        idx++;
[5723]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
[5871]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.");
[5855]154      double stress = 0, normalization = 0;
[5723]155      for (int i = 0; i < dimension - 1; i++) {
156        for (int j = i + 1; j < dimension; j++) {
[7416]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])) {
[5871]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]);
[5723]162          }
163        }
164      }
[5871]165      return stress / normalization;
[5723]166    }
167
168    private class Info {
169      public DoubleMatrix Coordinates { get; set; }
[5931]170      public DoubleMatrix Dissimilarities { get; set; }
[5723]171      public int Row { get; set; }
172
173      public Info(DoubleMatrix c, DoubleMatrix d, int r) {
174        Coordinates = c;
[5931]175        Dissimilarities = d;
[5723]176        Row = r;
177      }
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.