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, int k = 2) {
|
---|
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 | bool res = alglib.smatrixevd(m, dm.Length, 1, true, out q, out v);
|
---|
48 | if (!res) throw new Exception("Eigenvalue computation did not converge!");
|
---|
49 |
|
---|
50 | //TODO: this should also work without allocating memory for ev and evec
|
---|
51 | double[] ev = new double[k];
|
---|
52 | double[][] evec = new double[dm.Length][];
|
---|
53 | AllocArray(evec, k);
|
---|
54 | Array.Copy(q, q.Length - k, ev, 0, k);
|
---|
55 | for (int i = 0; i < k; i++) {
|
---|
56 | for (int j = 0; j < dm.Length; j++) {
|
---|
57 | evec[j][i] = v[j, i + (q.Length - k)];
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | double k1 = SumIfLZero(ev);
|
---|
62 | if (k1 < k) {
|
---|
63 | throw new Exception("Zero-eigenvalues detected. This leads to a degenerate point set. Use constants. ");
|
---|
64 | //TODO: handling of this case; implement adding of constants
|
---|
65 | }
|
---|
66 |
|
---|
67 | AllocArray(points, k);
|
---|
68 | for (int i = 0; i < k; i++) {
|
---|
69 | for (int j = 0; j < dm.Length; j++) {
|
---|
70 | points[j][i] = Math.Sqrt(ev[i]) * evec[j][i];
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return points;
|
---|
74 | }
|
---|
75 |
|
---|
76 | //based on R's cmdscale
|
---|
77 | public static double[][] MetricMDS(double[][] dm, int k = 2, bool add = false) {
|
---|
78 | int n = dm.Length;
|
---|
79 | double[][] points = new double[n][];
|
---|
80 | double[,] b = new double[n, n];
|
---|
81 | double[] q; //eigenvalues
|
---|
82 | double[,] v; //eigenvectors
|
---|
83 |
|
---|
84 | if (n < k)
|
---|
85 | throw new ArgumentException("Distance matrix length must be greater than dimension", "k");
|
---|
86 |
|
---|
87 | double[][] x = SquareMatrix(dm);
|
---|
88 | CenterMatrix(x);
|
---|
89 |
|
---|
90 | // solve additive constant problem
|
---|
91 | if (add) {
|
---|
92 | int[] i = Enumerable.Range(0, n).ToArray();
|
---|
93 | int[] i2 = Enumerable.Range(0, n).Select(y => y + n).ToArray();
|
---|
94 |
|
---|
95 | double[,] Z = new double[n * 2, n * 2];
|
---|
96 |
|
---|
97 | for (int j = 0; j < i.Length; j++) {
|
---|
98 | Z[i2[j], i[j]] = -1.0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | for (int j = 0; j < n; j++) {
|
---|
102 | for (int l = 0; l < n; l++) {
|
---|
103 | Z[i[j], i2[l]] = -1.0 * x[j][l];
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | double[][] centeredD = DoubleMatrix(dm);
|
---|
108 | CenterMatrix(centeredD);
|
---|
109 | for (int j = 0; j < n; j++) {
|
---|
110 | for (int l = 0; l < n; l++) {
|
---|
111 | Z[i2[j], i2[l]] = centeredD[j][l];
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | double[] wr;
|
---|
116 | double[] wi;
|
---|
117 | double[,] vl;
|
---|
118 | double[,] vr;
|
---|
119 | bool ret = alglib.rmatrixevd(Z, 2 * n, 0, out wr, out wi, out vl, out vr);
|
---|
120 | double c = wr.Max();
|
---|
121 |
|
---|
122 | x = new double[n][];
|
---|
123 | AllocArray(x, n);
|
---|
124 |
|
---|
125 | for (int j = 0; j < n; j++) {
|
---|
126 | for (int l = 0; l < n; l++) {
|
---|
127 | if (j != l) {
|
---|
128 | x[j][l] = Math.Pow(dm[j][l] + c, 2);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | CenterMatrix(x);
|
---|
133 | }
|
---|
134 |
|
---|
135 | ChangeSignAndHalve(x);
|
---|
136 |
|
---|
137 | //TODO: optimize memory consumption
|
---|
138 | for (int i = 0; i < n; i++) {
|
---|
139 | for (int j = 0; j < n; j++) {
|
---|
140 | b[i, j] = x[i][j];
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | bool res = alglib.smatrixevd(b, n, 1, true, out q, out v);
|
---|
145 | if (!res) throw new Exception("Eigenvalue computation did not converge!");
|
---|
146 |
|
---|
147 | //TODO: this should also work without allocating memory for ev and evec
|
---|
148 | double[] ev = new double[k];
|
---|
149 | double[][] evec = new double[n][];
|
---|
150 | AllocArray(evec, k);
|
---|
151 | Array.Copy(q, q.Length - k, ev, 0, k);
|
---|
152 | for (int i = 0; i < k; i++) {
|
---|
153 | for (int j = 0; j < n; j++) {
|
---|
154 | evec[j][i] = v[j, i + (q.Length - k)];
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | int k1 = SumIfLZero(ev);
|
---|
159 | if (k1 < k) {
|
---|
160 | throw new Exception("Zero-eigenvalues detected. This leads to a degenerate point set. Use constants. ");
|
---|
161 | }
|
---|
162 |
|
---|
163 | AllocArray(points, k);
|
---|
164 | for (int i = 0; i < k; i++) {
|
---|
165 | for (int j = 0; j < n; j++) {
|
---|
166 | points[j][i] = Math.Sqrt(ev[i]) * evec[j][i];
|
---|
167 | }
|
---|
168 | }
|
---|
169 | return points;
|
---|
170 | }
|
---|
171 |
|
---|
172 | //TODO: refactor the following methods into something sane
|
---|
173 | private static double[][] SquareMatrix(double[][] a) {
|
---|
174 | int n = a.Length;
|
---|
175 | double[][] newA = new double[a.Length][];
|
---|
176 |
|
---|
177 | for (int i = 0; i < n; i++) {
|
---|
178 | newA[i] = new double[a.Length];
|
---|
179 | for (int j = 0; j < n; j++) {
|
---|
180 | newA[i][j] = Math.Pow(a[i][j], 2.0);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | return newA;
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static double[][] DoubleMatrix(double[][] a) {
|
---|
187 | int n = a.Length;
|
---|
188 | double[][] newA = new double[a.Length][];
|
---|
189 |
|
---|
190 | for (int i = 0; i < n; i++) {
|
---|
191 | newA[i] = new double[a.Length];
|
---|
192 | for (int j = 0; j < n; j++) {
|
---|
193 | newA[i][j] = a[i][j] * 2.0;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | return newA;
|
---|
197 | }
|
---|
198 |
|
---|
199 | //based on R's DoubleCentre
|
---|
200 | private static void CenterMatrix(double[][] a) {
|
---|
201 | int n = a.Length;
|
---|
202 |
|
---|
203 | //reduce lines by line avg
|
---|
204 | for (int i = 0; i < n; i++) {
|
---|
205 | double sum = 0;
|
---|
206 | for (int j = 0; j < n; j++) sum += a[i][j];
|
---|
207 | sum /= n;
|
---|
208 | for (int j = 0; j < n; j++) a[i][j] -= sum;
|
---|
209 | }
|
---|
210 |
|
---|
211 | //reduce cols by col avg
|
---|
212 | for (int j = 0; j < n; j++) {
|
---|
213 | double sum = 0;
|
---|
214 | for (int i = 0; i < n; i++) sum += a[i][j];
|
---|
215 | sum /= n;
|
---|
216 | for (int i = 0; i < n; i++) a[i][j] -= sum;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private static void ChangeSignAndHalve(double[][] a) {
|
---|
221 | int n = a.Length;
|
---|
222 |
|
---|
223 | for (int i = 0; i < n; i++) {
|
---|
224 | for (int j = 0; j < n; j++) {
|
---|
225 | a[i][j] = (-1.0 * a[i][j]) / 2;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | private static int SumIfLZero(double[] a) {
|
---|
231 | return a.Count(x => x > 0.0 && !x.IsAlmost(0.0));
|
---|
232 | }
|
---|
233 |
|
---|
234 | private static void AllocArray(double[][] arr, int size) {
|
---|
235 | for (int i = 0; i < arr.Length; i++) {
|
---|
236 | arr[i] = new double[size];
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | public static double[][] TransformToDistances(double[][] similarityMatrix) {
|
---|
241 | double[][] dm = new double[similarityMatrix.Length][];
|
---|
242 |
|
---|
243 | for (int i = 0; i < dm.Length; i++) {
|
---|
244 | dm[i] = new double[similarityMatrix.Length];
|
---|
245 | for (int j = 0; j < dm.Length; j++) {
|
---|
246 | dm[i][j] = Math.Sqrt(similarityMatrix[i][i] + similarityMatrix[j][j] - 2 * similarityMatrix[i][j]);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | return dm;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | } |
---|