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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace 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 | //based on R's cmdscale
|
---|
75 | public static double[][] MetricMDS(double[][] dm, int k = 2, bool add = false) {
|
---|
76 | double[][] points = new double[dm.Length][];
|
---|
77 | double[,] b = new double[dm.Length, dm.Length];
|
---|
78 | double[] q; //eigenvalues
|
---|
79 | double[,] v; //eigenvectors
|
---|
80 |
|
---|
81 | double[][] x = SquareMatrix(dm);
|
---|
82 | CenterMatrix(x);
|
---|
83 | ChangeSignAndHalve(x);
|
---|
84 |
|
---|
85 | //TODO: optimize memory consumption
|
---|
86 | for (int i = 0; i < dm.Length; i++) {
|
---|
87 | for (int j = 0; j < dm.Length; j++) {
|
---|
88 | b[i, j] = x[i][j];
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | bool res = alglib.smatrixevd(b, dm.Length, 1, true, out q, out v);
|
---|
93 | if (!res) throw new Exception("Eigenvalue computation did not converge!");
|
---|
94 |
|
---|
95 | //TODO: this should also work without allocating memory for ev and evec
|
---|
96 | double[] ev = new double[k];
|
---|
97 | double[][] evec = new double[dm.Length][];
|
---|
98 | AllocArray(evec, k);
|
---|
99 | Array.Copy(q, q.Length - k, ev, 0, k);
|
---|
100 | for (int i = 0; i < k; i++) {
|
---|
101 | for (int j = 0; j < dm.Length; j++) {
|
---|
102 | evec[j][i] = v[j, i + (q.Length - k)];
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | double k1 = SumIfLZero(ev);
|
---|
107 | if (k1 < k) {
|
---|
108 | throw new Exception("Zero-eigenvalues detected. This leads to a degenerate point set. Use constants. ");
|
---|
109 | //TODO: handling of this case; implement adding of constants
|
---|
110 | }
|
---|
111 |
|
---|
112 | AllocArray(points, k);
|
---|
113 | for (int i = 0; i < k; i++) {
|
---|
114 | for (int j = 0; j < dm.Length; j++) {
|
---|
115 | points[j][i] = Math.Sqrt(ev[i]) * evec[j][i];
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return points;
|
---|
119 | }
|
---|
120 |
|
---|
121 | //TODO: refactor the following methods into something sane
|
---|
122 | private static double[][] SquareMatrix(double[][] a) {
|
---|
123 | int n = a.Length;
|
---|
124 | double[][] newA = new double[a.Length][];
|
---|
125 |
|
---|
126 | for (int i = 0; i < n; i++) {
|
---|
127 | newA[i] = new double[a.Length];
|
---|
128 | for (int j = 0; j < n; j++) {
|
---|
129 | newA[i][j] = Math.Pow(a[i][j], 2.0);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return newA;
|
---|
133 | }
|
---|
134 |
|
---|
135 | //based on R's DoubleCentre
|
---|
136 | private static void CenterMatrix(double[][] a) {
|
---|
137 | int n = a.Length;
|
---|
138 |
|
---|
139 | //reduce lines by line avg
|
---|
140 | for (int i = 0; i < n; i++) {
|
---|
141 | double sum = 0;
|
---|
142 | for (int j = 0; j < n; j++) sum += a[i][j];
|
---|
143 | sum /= n;
|
---|
144 | for (int j = 0; j < n; j++) a[i][j] -= sum;
|
---|
145 | }
|
---|
146 |
|
---|
147 | //reduce cols by col avg
|
---|
148 | for (int j = 0; j < n; j++) {
|
---|
149 | double sum = 0;
|
---|
150 | for (int i = 0; i < n; i++) sum += a[i][j];
|
---|
151 | sum /= n;
|
---|
152 | for (int i = 0; i < n; i++) a[i][j] -= sum;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static void ChangeSignAndHalve(double[][] a) {
|
---|
157 | int n = a.Length;
|
---|
158 |
|
---|
159 | for (int i = 0; i < n; i++) {
|
---|
160 | for (int j = 0; j < n; j++) {
|
---|
161 | a[i][j] = (-1.0 * a[i][j]) / 2;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | private static double SumIfLZero(double[] a) {
|
---|
167 | return a.Where(x => x > 0.0 && !x.IsAlmost(0.0)).Sum();
|
---|
168 | }
|
---|
169 |
|
---|
170 | private static void AllocArray(double[][] arr, int size) {
|
---|
171 | for (int i = 0; i < arr.Length; i++) {
|
---|
172 | arr[i] = new double[size];
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static double[][] TransformToDistances(double[][] similarityMatrix) {
|
---|
177 | double[][] dm = new double[similarityMatrix.Length][];
|
---|
178 |
|
---|
179 | for (int i = 0; i < dm.Length; i++) {
|
---|
180 | dm[i] = new double[similarityMatrix.Length];
|
---|
181 | for (int j = 0; j < dm.Length; j++) {
|
---|
182 | dm[i][j] = Math.Sqrt(similarityMatrix[i][i] + similarityMatrix[j][j] - 2 * similarityMatrix[i][j]);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | return dm;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|