Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/DistanceMatrixToPointsTest.cs @ 10573

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

#1886 updated unit test to take account for rounding errors

File size: 14.3 KB
RevLine 
[10060]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 HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
[10108]24using HeuristicLab.Common;
[10109]25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Problems.TravelingSalesman;
[10060]27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace AlgorithmBehaviorUnitTests {
30  [TestClass]
31  public class DistanceMatrixToPointsTest {
32    [TestMethod]
[10109]33    public void TestMetricMDSStatic() {
[10108]34      int nrOfPoints = 3;
[10060]35      int dim = 2;
36      double[][] orgPoints = new double[nrOfPoints][];
37      double[][] orgDm = new double[nrOfPoints][];
38      double[][] newDm = new double[nrOfPoints][];
39      double[][] newPoints = null;
40
41      AllocArray(orgPoints, dim);
42      AllocArray(orgDm, nrOfPoints);
43      AllocArray(newDm, nrOfPoints);
[10108]44      StaticPoints(orgPoints);
[10060]45      CalculateDistanceMatrix(orgDm, orgPoints);
46
[10118]47      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim, true);
[10078]48
[10060]49      CalculateDistanceMatrix(newDm, newPoints);
[10108]50      Console.WriteLine("orgDm:");
51      PrintDM(orgDm);
52      Console.WriteLine("newDm:");
53      PrintDM(newDm);
[10060]54
[10108]55      for (int i = 0; i < orgDm.Length; i++) {
56        for (int j = 0; j < orgDm.Length; j++) {
57          double diff = orgDm[i][j] - newDm[i][j];
58          Assert.IsTrue(diff.IsAlmost(0.0));
59        }
60      }
[10060]61    }
62
[10109]63    [TestMethod]
64    public void TestMetricMDSRandom() {
65      int nrOfPoints = 30;
66      int dim = 10;
67      double[][] orgPoints = new double[nrOfPoints][];
68      double[][] orgDm = new double[nrOfPoints][];
69      double[][] newDm = new double[nrOfPoints][];
70      double[][] newPoints = null;
71
72      AllocArray(orgPoints, dim);
73      AllocArray(orgDm, nrOfPoints);
74      AllocArray(newDm, nrOfPoints);
75      SamplePoints(orgPoints);
76      CalculateDistanceMatrix(orgDm, orgPoints);
77
78      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim);
79
80      CalculateDistanceMatrix(newDm, newPoints);
81      Console.WriteLine("orgDm:");
82      PrintDM(orgDm);
83      Console.WriteLine("newDm:");
84      PrintDM(newDm);
85
86      for (int i = 0; i < orgDm.Length; i++) {
87        for (int j = 0; j < orgDm.Length; j++) {
88          double diff = orgDm[i][j] - newDm[i][j];
89          Assert.IsTrue(diff.IsAlmost(0.0));
90        }
91      }
92    }
93
94    [TestMethod]
95    public void TestDistanceMatrixConversionStatic() {
96      int nrOfPoints = 3;
97      int dim = 2;
98      double[][] orgPoints = new double[nrOfPoints][];
99      double[][] orgDm = new double[nrOfPoints][];
100      double[][] newDm = new double[nrOfPoints][];
101      double[][] newPoints = null;
102
103      AllocArray(orgPoints, dim);
104      AllocArray(orgDm, nrOfPoints);
105      AllocArray(newDm, nrOfPoints);
106      StaticPoints(orgPoints);
107      CalculateDistanceMatrix(orgDm, orgPoints);
108
109      newPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(orgDm);
110
111      CalculateDistanceMatrix(newDm, newPoints);
112      Console.WriteLine("orgDm:");
113      PrintDM(orgDm);
114      Console.WriteLine("newDm:");
115      PrintDM(newDm);
116
117      for (int i = 0; i < orgDm.Length; i++) {
118        for (int j = 0; j < orgDm.Length; j++) {
119          double diff = orgDm[i][j] - newDm[i][j];
120          Assert.IsTrue(diff.IsAlmost(0.0));
121        }
122      }
123    }
124
125    [TestMethod]
126    public void TestDistanceMatrixConversionDynamic() {
127      int nrOfPoints = 30;
128      int dim = 20;
129      double[][] orgPoints = new double[nrOfPoints][];
130      double[][] orgDm = new double[nrOfPoints][];
131      double[][] newDm = new double[nrOfPoints][];
132      double[][] newPoints = null;
133
134      AllocArray(orgPoints, dim);
135      AllocArray(orgDm, nrOfPoints);
136      AllocArray(newDm, nrOfPoints);
137      SamplePoints(orgPoints);
138      CalculateDistanceMatrix(orgDm, orgPoints);
139
140      newPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(orgDm, dim);
141
142      CalculateDistanceMatrix(newDm, newPoints);
143      Console.WriteLine("orgDm:");
144      PrintDM(orgDm);
145      Console.WriteLine("newDm:");
146      PrintDM(newDm);
147
148      for (int i = 0; i < orgDm.Length; i++) {
149        for (int j = 0; j < orgDm.Length; j++) {
150          double diff = orgDm[i][j] - newDm[i][j];
151          Assert.IsTrue(diff.IsAlmost(0.0));
152        }
153      }
154    }
155
156    [TestMethod]
157    public void TestMetricMDSForPermutations() {
[10132]158      int nrOfPoints = 30;
[10118]159      int dim = 5;
[10109]160      Permutation[] orgPoints = new Permutation[nrOfPoints];
161      double[][] orgDm;
162      double[][] newDm = new double[nrOfPoints][];
163      double[][] newPoints = null;
164
165      AllocArray(newDm, nrOfPoints);
166
167      SamplePermutations(orgPoints, dim);
168      orgDm = CalculateDistanceMatrixFromPermutations(orgPoints);
169
[10132]170      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim);
[10109]171
172      CalculateDistanceMatrix(newDm, newPoints);
173      Console.WriteLine("orgDm:");
174      PrintDM(orgDm);
175      Console.WriteLine("newDm:");
176      PrintDM(newDm);
177
178      for (int i = 0; i < orgDm.Length; i++) {
179        for (int j = 0; j < orgDm.Length; j++) {
[10132]180          double diff = Math.Abs(orgDm[i][j] - newDm[i][j]);
181          if (diff < 0.0000001) diff = 0.0;
[10109]182          Assert.IsTrue(diff.IsAlmost(0.0));
183        }
184      }
185    }
186
[10118]187    [TestMethod]
188    public void TestMetricMDSForPermutationsStatic() {
[10132]189      int nrOfPoints = 10;
[10118]190      int dim = 5;
191
192      double[][] orgDm = StaticPermutationDM();
193      double[][] newDm = new double[nrOfPoints][];
194      double[][] newPoints = null;
195
196      AllocArray(newDm, nrOfPoints);
197
[10132]198      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim, false);
[10118]199
200      CalculateDistanceMatrix(newDm, newPoints);
201      Console.WriteLine("orgDm:");
202      PrintDM(orgDm);
203      Console.WriteLine("newDm:");
204      PrintDM(newDm);
205
[10132]206      double[][] resultDMFromR = StaticPermutationDMResult();
207
[10118]208      for (int i = 0; i < orgDm.Length; i++) {
209        for (int j = 0; j < orgDm.Length; j++) {
[10132]210          double diff = Math.Abs(resultDMFromR[i][j] - Math.Round(newDm[i][j], 7));
[10118]211          if (diff < 0.000001) diff = 0.0;
212          Assert.IsTrue(diff.IsAlmost(0.0));
[10132]213
214          diff = Math.Abs(orgDm[i][j] - newDm[i][j]);
215          if (diff < 0.00000001) diff = 0.0;
216          Assert.IsTrue(diff.IsAlmost(0.0));
[10118]217        }
218      }
219    }
220
[10127]221    [TestMethod]
222    public void TestMetricMDSForSmallDistances() {
223      int nrOfPoints = 4;
224      int dim = 2;
225      double[][] orgPoints = new double[nrOfPoints][];
226      double[][] orgDm = new double[nrOfPoints][];
227      double[][] newDm = new double[nrOfPoints][];
228      double[][] newPoints = null;
229
230      AllocArray(orgPoints, dim);
231      AllocArray(orgDm, nrOfPoints);
232      AllocArray(newDm, nrOfPoints);
233      SmallDiffStaticPoints(orgPoints);
234      CalculateDistanceMatrix(orgDm, orgPoints);
235
236      Console.WriteLine("orgDm:");
237      PrintDM(orgDm);
238
[10132]239      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim, false);
[10127]240
241      CalculateDistanceMatrix(newDm, newPoints);
242      Console.WriteLine("newDm:");
243      PrintDM(newDm);
244
245      for (int i = 0; i < orgDm.Length; i++) {
246        for (int j = 0; j < orgDm.Length; j++) {
[10132]247          double diff = Math.Abs(orgDm[i][j] - newDm[i][j]);
248          if (diff < 0.00000001) diff = 0.0;
[10127]249          Assert.IsTrue(diff.IsAlmost(0.0));
250        }
251      }
252    }
253
254    private static void SmallDiffStaticPoints(double[][] points) {
255      points[0][0] = 1;
256      points[0][1] = 1;
257
258      points[1][0] = 1.03;
259      points[1][1] = 1.2;
260
261      points[2][0] = 1.05;
262      points[2][1] = 1.01;
263
264      points[3][0] = 1.5;
265      points[3][1] = 1.1;
266    }
267
[10118]268    private static double[][] StaticPermutationDM() {
[10132]269      double[][] dm = new double[10][];
270      AllocArray(dm, 10);
[10118]271
[10132]272      dm[0] = new[]
273      {
274        0, 1.09544511501033, 1.09544511501033, 0.894427190999916, 0.894427190999916, 1.09544511501033, 0.894427190999916,
275        1.09544511501033, 0.894427190999916, 0.894427190999916
276      };
[10118]277      dm[1] = new[]
278      {
[10132]279        1.09544511501033, 0, 1.09544511501033, 1.4142135623731, 0.894427190999916, 1.09544511501033, 0.894427190999916,
280        1.09544511501033, 0.894427190999916, 0.894427190999916
[10118]281      };
282      dm[2] = new[]
283      {
[10132]284        1.09544511501033, 1.09544511501033, 0, 0.894427190999916, 0.894427190999916, 1.09544511501033, 0.894427190999916,
285        1.09544511501033, 0.894427190999916, 0.894427190999916
[10118]286      };
287      dm[3] = new[]
288      {
[10132]289        0.894427190999916, 1.4142135623731, 0.894427190999916, 0, 1.09544511501033, 0.894427190999916, 1.09544511501033,
290        0.894427190999916, 1.09544511501033, 1.09544511501033
[10118]291      };
292      dm[4] = new[]
293      {
[10132]294        0.894427190999916, 0.894427190999916, 0.894427190999916, 1.09544511501033, 0, 0.894427190999916, 0,
295        0.894427190999916, 1.09544511501033, 1.09544511501033
[10118]296      };
297      dm[5] = new[]
298      {
[10132]299        1.09544511501033, 1.09544511501033, 1.09544511501033, 0.894427190999916, 0.894427190999916, 0, 0.894427190999916,
300        1.09544511501033, 0.894427190999916, 1.4142135623731
[10118]301      };
302      dm[6] = new[]
303      {
[10132]304        0.894427190999916, 0.894427190999916, 0.894427190999916, 1.09544511501033, 0, 0.894427190999916, 0,
305        0.894427190999916, 1.09544511501033, 1.09544511501033
[10118]306      };
307      dm[7] = new[]
308      {
[10132]309        1.09544511501033, 1.09544511501033, 1.09544511501033, 0.894427190999916, 0.894427190999916, 1.09544511501033,
310        0.894427190999916, 0, 1.4142135623731, 0.894427190999916
[10118]311      };
312      dm[8] = new[]
313      {
[10132]314        0.894427190999916, 0.894427190999916, 0.894427190999916, 1.09544511501033, 1.09544511501033, 0.894427190999916,
315        1.09544511501033, 1.4142135623731, 0, 1.09544511501033
[10118]316      };
317      dm[9] = new[]
318      {
[10132]319        0.894427190999916, 0.894427190999916, 0.894427190999916, 1.09544511501033, 1.09544511501033, 1.4142135623731,
320        1.09544511501033, 0.894427190999916, 1.09544511501033, 0
[10118]321      };
[10132]322
323      return dm;
324    }
325
326    private static double[][] StaticPermutationDMResult() {
327      double[][] dm = new double[10][];
328      AllocArray(dm, 10);
329
330      dm[0] = new[]
[10118]331      {
[10132]332        0.0000000,1.0954451,1.0954451,0.8944272,8.944272e-01,1.0954451,8.944272e-01,1.0954451,0.8944272,0.8944272
[10118]333      };
[10132]334      dm[1] = new[]
[10118]335      {
[10132]336        1.0954451,0.0000000,1.0954451,1.4142136,8.944272e-01,1.0954451,8.944272e-01,1.0954451,0.8944272,0.8944272       
[10118]337      };
[10132]338      dm[2] = new[]
[10118]339      {
[10132]340        1.0954451,1.0954451,0.0000000,0.8944272,8.944272e-01,1.0954451,8.944272e-01,1.0954451,0.8944272,0.8944272
[10118]341      };
[10132]342      dm[3] = new[]
[10118]343      {
[10132]344        0.8944272,1.4142136,0.8944272,0.0000000,1.095445e+00,0.8944272,1.095445e+00,0.8944272,1.0954451,1.0954451       
[10118]345      };
[10132]346      dm[4] = new[]
[10118]347      {
[10132]348        0.8944272,0.8944272,0.8944272,1.0954451,0.000000e+00,0.8944272,4.331115e-16,0.8944272,1.0954451,1.0954451
[10118]349      };
[10132]350      dm[5] = new[]
351      {
352        1.0954451,1.0954451,1.0954451,0.8944272,8.944272e-01,0.0000000,8.944272e-01,1.0954451,0.8944272,1.4142136 
353      };
354      dm[6] = new[]
355      {
356        0.8944272,0.8944272,0.8944272,1.0954451,4.331115e-16,0.8944272,0.000000e+00,0.8944272,1.0954451,1.0954451       
357      };
358      dm[7] = new[]
359      {
360        1.0954451,1.0954451,1.0954451,0.8944272,8.944272e-01,1.0954451,8.944272e-01,0.0000000,1.4142136,0.8944272
361      };
362      dm[8] = new[]
363      {
364        0.8944272,0.8944272,0.8944272,1.0954451,1.095445e+00,0.8944272,1.095445e+00,1.4142136,0.0000000,1.0954451       
365      };
366      dm[9] = new[]
367      {
368        0.8944272,0.8944272,0.8944272,1.0954451,1.095445e+00,1.4142136,1.095445e+00,0.8944272,1.0954451,0.0000000       
369      };
370
[10118]371      return dm;
372    }
373
[10108]374    private static void PrintDM(double[][] dm) {
375      for (int i = 0; i < dm.Length; i++) {
376        for (int j = 0; j < dm.Length; j++) {
377          Console.Write(dm[i][j] + " ");
378        }
379        Console.WriteLine();
380      }
381    }
382
[10060]383    private static void SamplePoints(double[][] points) {
384      Random rand = new Random();
385
386      for (int i = 0; i < points.Length; i++) {
387        for (int j = 0; j < points[i].Length; j++) {
388          points[i][j] = rand.NextDouble() * 100;
389        }
390      }
391    }
392
[10109]393    private static void SamplePermutations(Permutation[] points, int dim) {
394      var rand = new HeuristicLab.Random.FastRandom();
395
396      for (int i = 0; i < points.Length; i++) {
397        var p = new Permutation(PermutationTypes.RelativeUndirected, dim, rand);
398        points[i] = p;
399      }
400    }
401
[10108]402    private static void StaticPoints(double[][] points) {
403      points[0][0] = 2;
404      points[0][1] = 1;
405
406      points[1][0] = 5;
407      points[1][1] = 5;
408
409      points[2][0] = 3;
410      points[2][1] = 7;
411    }
412
[10060]413    private static void CalculateDistanceMatrix(double[][] dm, double[][] points) {
414      for (int i = 0; i < points.Length; i++) {
415        for (int j = 0; j < points.Length; j++) {
[10108]416          dm[i][j] = points[i].EuclideanDistance(points[j]);
[10060]417        }
418      }
419    }
420
[10109]421    private static double[][] CalculateDistanceMatrixFromPermutations(Permutation[] points) {
422      double[][] tmpDm = new double[points.Length][];
423      AllocArray(tmpDm, points.Length);
424
425      for (int i = 0; i < points.Length; i++) {
426        for (int j = 0; j < points.Length; j++) {
427          double diversity = TSPSimilarityCalculator.CalculateSimilarity(points[i], points[j]);
428          tmpDm[i][j] = diversity;
429        }
430      }
431
432      return DistanceMatrixToPoints.TransformToDistances(tmpDm);
433    }
434
[10060]435    private static void AllocArray(double[][] arr, int size) {
436      for (int i = 0; i < arr.Length; i++) {
437        arr[i] = new double[size];
438      }
439    }
440  }
[10118]441}
Note: See TracBrowser for help on using the repository browser.