1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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 


22  using System;


23  using HeuristicLab.Data;


24 


25  namespace HeuristicLab.Analysis {


26  public static class MultidimensionalScaling {


27 


28  /// <summary>


29  /// Performs the KruskalShepard 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 KruskalShepard 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 preinitialized x,ycoordinates 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 epsg = 1e7;


75  double epsf = 0;


76  double epsx = 0;


77  int maxits = 0;


78 


79  alglib.minlmstate state;


80  alglib.minlmreport rep;


81  for (int iterations = 0; iterations < maximumIterations; iterations++) {


82  bool changed = false;


83  for (int i = 0; i < dimension; i++) {


84  double[] c = new double[] { coordinates[i, 0], coordinates[i, 1] };


85 


86  try {


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);


91  } catch (alglib.alglibexception) { }


92  if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) {


93  changed = changed  (coordinates[i, 0] != c[0])  (coordinates[i, 1] != c[1]);


94  coordinates[i, 0] = c[0];


95  coordinates[i, 1] = c[1];


96  }


97  }


98  if (!changed) break;


99  }


100  return coordinates;


101  }


102 


103  private static void StressFitness(double[] x, double[] fi, object obj) {


104  Info info = (obj as Info);


105  for (int i = 0; i < info.Coordinates.Rows; 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++) {


116  if (i != info.Row) {


117  double c = info.Dissimilarities[info.Row, i];


118  double a = info.Coordinates[i, 0];


119  double b = info.Coordinates[i, 1];


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++;


125  }


126  }


127  }


128 


129  private static double Stress(double[] x, double distance, double xCoord, double yCoord) {


130  return Stress(x[0], x[1], distance, xCoord, yCoord);


131  }


132 


133  private static double Stress(double x, double y, double distance, double otherX, double otherY) {


134  double d = Math.Sqrt((x  otherX) * (x  otherX)


135  + (y  otherY) * (y  otherY));


136  return (d  distance) * (d  distance);


137  }


138 


139  /// <summary>


140  /// This method computes the normalized rawstress value according to Groenen and van de Velden 2004. "Multidimensional Scaling". Technical report EI 200415.


141  /// </summary>


142  /// <remarks>


143  /// Throws an ArgumentException when the <paramref name="dissimilarities"/> matrix is not symmetric.


144  /// </remarks>


145  ///


146  /// <param name="dissimilarities">The matrix with the dissimilarities.</param>


147  /// <param name="coordinates">The actual location of the points.</param>


148  /// <returns>The normalized rawstress value that describes the goodnessoffit between the distances in the points and the size of the dissimilarities. If the value is < 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>


149  public static double CalculateNormalizedStress(DoubleMatrix dissimilarities, DoubleMatrix coordinates) {


150  int dimension = dissimilarities.Rows;


151  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.");


152  double stress = 0, normalization = 0;


153  for (int i = 0; i < dimension  1; i++) {


154  for (int j = i + 1; j < dimension; j++) {


155  if (dissimilarities[i, j] != dissimilarities[j, i]) throw new ArgumentException("Dissimilarities is not a symmetric matrix.", "dissimilarities");


156  if (dissimilarities[i, j] != 0) {


157  stress += Stress(coordinates[i, 0], coordinates[i, 1], dissimilarities[i, j], coordinates[j, 0], coordinates[j, 1]);


158  normalization += (dissimilarities[i, j] * dissimilarities[i, j]);


159  }


160  }


161  }


162  return stress / normalization;


163  }


164 


165  private class Info {


166  public DoubleMatrix Coordinates { get; set; }


167  public DoubleMatrix Dissimilarities { get; set; }


168  public int Row { get; set; }


169 


170  public Info(DoubleMatrix c, DoubleMatrix d, int r) {


171  Coordinates = c;


172  Dissimilarities = d;


173  Row = r;


174  }


175  }


176  }


177  } 
