Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DistanceMatrixToPoints.cs @ 10097

Last change on this file since 10097 was 10097, checked in by ascheibe, 11 years ago

#1886 added metric mds for testing purposes

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 System.Linq;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
27  public static class DistanceMatrixToPoints {
28    /*
29     *  Calculates a matrix of n-dimensional points from the distance matrix dm as described in
30     *  http://math.stackexchange.com/questions/156161/finding-the-coordinates-of-points-from-distance-matrix/423898#423898
31     *  and
32     *  http://stackoverflow.com/questions/10963054/finding-the-coordinates-of-points-from-distance-matrix/17177833#17177833
33     *   
34     */
35    public static double[][] ConvertDistanceMatrixToPoints(double[][] dm) {
36      double[][] points = new double[dm.Length][];
37      double[,] m = new double[dm.Length, dm.Length];
38      double[] q = new double[dm.Length]; //eigenvalues
39      double[,] v = new double[dm.Length, dm.Length]; //eigenvectors
40
41      for (int i = 0; i < dm.Length; i++) {
42        for (int j = 0; j < dm.Length; j++) {
43          m[i, j] = 0.5 * (Math.Pow(dm[0][j], 2) + Math.Pow(dm[i][0], 2) - Math.Pow(dm[i][j], 2));
44        }
45      }
46
47      //QR decomposition to get the upper part for smatrixevd
48      double[] tau;
49      double[,] r;
50      alglib.rmatrixqr(ref m, dm.Length, dm.Length, out tau);
51      alglib.rmatrixqrunpackr(m, dm.Length, dm.Length, out r);
52
53      bool res = alglib.smatrixevd(r, dm.Length, 1, true, out q, out v);
54      if (!res) throw new Exception("Eigenvalue computation did not converge!");
55
56      int zeroCnt = q.Count(x => x.IsAlmost(0) || x < 0.0);
57      for (int i = 0; i < dm.Length; i++) {
58        points[i] = new double[dm.Length - zeroCnt];
59      }
60
61      int pi = 0;
62      for (int i = 0; i < dm.Length; i++) {
63        if (!q[i].IsAlmost(0.0) && q[i] > 0.0) {
64          for (int j = 0; j < dm.Length; j++) {
65            points[j][pi] = Math.Sqrt(q[i]) * v[j, i];
66          }
67          pi++;
68        }
69      }
70
71      return points;
72    }
73
74    public static double[][] MetricMDS(double[][] dm) {
75      double[][] points = new double[dm.Length][];
76      double[,] a = new double[dm.Length, dm.Length];
77      double[,] b = new double[dm.Length, dm.Length];
78      double[] ai = new double[dm.Length];
79      double[] aj = new double[dm.Length];
80      double aavg = 0.0;
81      double[] q = new double[dm.Length];
82      double[,] v = new double[dm.Length, dm.Length];
83
84
85      for (int i = 0; i < dm.Length; i++) {
86        for (int j = 0; j < dm.Length; j++) {
87          a[i, j] = -0.5 * Math.Pow(dm[i][j], 2);
88          aavg += a[i, j];
89        }
90      }
91
92      aavg /= dm.Length * dm.Length;
93
94      for (int i = 0; i < dm.Length; i++) {
95        ai[i] = dm[i].Average();
96      }
97
98      for (int i = 0; i < dm.Length; i++) {
99        for (int j = 0; j < dm.Length; j++) {
100          aj[i] += dm[j][i];
101        }
102        aj[i] /= dm.Length;
103      }
104
105      for (int i = 0; i < dm.Length; i++) {
106        for (int j = 0; j < dm.Length; j++) {
107          b[i, j] = a[i, j] - ai[i] - aj[j] + aavg;
108        }
109      }
110
111      //TODO: check b for negative values and make it positiv
112
113      double[] tau;
114      double[,] r;
115      alglib.rmatrixqr(ref b, dm.Length, dm.Length, out tau);
116      alglib.rmatrixqrunpackr(b, dm.Length, dm.Length, out r);
117
118      bool res = alglib.smatrixevd(r, dm.Length, 1, true, out q, out v);
119      if (!res) throw new Exception("Eigenvalue computation did not converge!");
120
121      int zeroCnt = q.Count(x => x.IsAlmost(0) || x < 0.0);
122      for (int i = 0; i < dm.Length; i++) {
123        points[i] = new double[dm.Length - zeroCnt];
124      }
125
126      int pi = 0;
127      for (int i = 0; i < dm.Length; i++) {
128        if (!q[i].IsAlmost(0.0) && q[i] > 0.0) {
129          for (int j = 0; j < dm.Length; j++) {
130            points[j][pi] = Math.Sqrt(q[i]) * v[j, i];
131          }
132          pi++;
133        }
134      }
135
136      return points;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.