Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Analysis/3.3/Tests/MultidimensionalScalingTest.cs @ 6514

Last change on this file since 6514 was 5938, checked in by abeham, 14 years ago

#1330

  • Added HeuristicLab.Problems.QuadraticAssignment.Tests-3.3 project to solution
  • Fixed assembly name in HeuristicLab.Analysis.Tests-3.3
  • Added another test case to the MDS tests
  • Referenced the two QAP plugins in the tests project
  • Removed plugin dependency in HeuristicLab.Problems.QuadraticAssignment.Views to HeuristicLab.Optimization as the unit test complained about it
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Data;
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25
26namespace HeuristicLab.Analysis.Tests {
27  [TestClass]
28  public class MultidimensionalScalingTest {
29    [TestMethod]
30    public void TestGoodnessOfFit() {
31      double stress;
32      DoubleMatrix distances3 = new DoubleMatrix(3, 3);
33      // Example 1: A right triangle
34      distances3[0, 1] = distances3[1, 0] = 3;
35      distances3[0, 2] = distances3[2, 0] = 4;
36      distances3[1, 2] = distances3[2, 1] = 5;
37      stress = MultidimensionalScaling.CalculateNormalizedStress(distances3,
38        MultidimensionalScaling.KruskalShepard(distances3));
39      Assert.IsTrue(stress < 0.1);
40      // Example 2: An arbitrary triangle
41      distances3[0, 1] = distances3[1, 0] = 8;
42      distances3[0, 2] = distances3[2, 0] = 6.4;
43      distances3[1, 2] = distances3[2, 1] = 5;
44      stress = MultidimensionalScaling.CalculateNormalizedStress(distances3,
45        MultidimensionalScaling.KruskalShepard(distances3));
46      Assert.IsTrue(stress < 0.1);
47      DoubleMatrix distances4 = new DoubleMatrix(4, 4);
48      // Example 3: A small square
49      distances4[0, 1] = distances4[1, 0] = 1;
50      distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2);
51      distances4[0, 3] = distances4[3, 0] = 1;
52      distances4[1, 2] = distances4[2, 1] = 1;
53      distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2);
54      distances4[2, 3] = distances4[3, 2] = 1;
55      stress = MultidimensionalScaling.CalculateNormalizedStress(distances4,
56        MultidimensionalScaling.KruskalShepard(distances4));
57      Assert.IsTrue(stress < 0.1);
58      // Example 4: A large square
59      distances4[0, 1] = distances4[1, 0] = 1000;
60      distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2000000);
61      distances4[0, 3] = distances4[3, 0] = 1000;
62      distances4[1, 2] = distances4[2, 1] = 1000;
63      distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2000000);
64      distances4[2, 3] = distances4[3, 2] = 1000;
65      stress = MultidimensionalScaling.CalculateNormalizedStress(distances4,
66        MultidimensionalScaling.KruskalShepard(distances4));
67      Assert.IsTrue(stress < 0.1);
68      // Example 5: An arbitrary cloud of 8 points in a plane
69      DoubleMatrix distancesK = GetDistances(new double[,] { { 2, 1 }, { 5, 2 }, { 7, 1 }, { 4, 0 }, { 3, 3 }, { 4, 2 }, { 1, 8 }, { 6, 3 } });
70      stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
71        MultidimensionalScaling.KruskalShepard(distancesK));
72      Assert.IsTrue(stress < 0.1);
73      // Example 6: A tetrahedron
74      distancesK = GetDistances(new double[,] { { 0, 0, 0 }, { 4, 0, 0 }, { 2, 3.4641, 0 }, { 2, 1.1547, 3.2660 } });
75      stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
76        MultidimensionalScaling.KruskalShepard(distancesK));
77      Assert.IsTrue(stress < 0.1);
78      // Example 7: A matrix of perceived dissimilarities between 14 colors, published in the literature
79      distancesK = new DoubleMatrix(new double[,] {
80{ 0.00, 0.14, 0.58, 0.58, 0.82, 0.94, 0.93, 0.96, 0.98, 0.93, 0.91, 0.88, 0.87, 0.84 },
81{ 0.14, 0.00, 0.50, 0.56, 0.78, 0.91, 0.93, 0.93, 0.98, 0.96, 0.93, 0.89, 0.87, 0.86 },
82{ 0.58, 0.50, 0.00, 0.19, 0.53, 0.83, 0.90, 0.92, 0.98, 0.99, 0.98, 0.99, 0.95, 0.97 },
83{ 0.58, 0.56, 0.19, 0.00, 0.46, 0.75, 0.90, 0.91, 0.98, 0.99, 1.00, 0.99, 0.98, 0.96 },
84{ 0.82, 0.78, 0.53, 0.46, 0.00, 0.39, 0.69, 0.74, 0.93, 0.98, 0.98, 0.99, 0.98, 1.00 },
85{ 0.94, 0.91, 0.83, 0.75, 0.39, 0.00, 0.38, 0.55, 0.86, 0.92, 0.98, 0.98, 0.98, 0.99 },
86{ 0.93, 0.93, 0.90, 0.90, 0.69, 0.38, 0.00, 0.27, 0.78, 0.86, 0.95, 0.98, 0.98, 1.00 },
87{ 0.96, 0.93, 0.92, 0.91, 0.74, 0.55, 0.27, 0.00, 0.67, 0.81, 0.96, 0.97, 0.98, 0.98 },
88{ 0.98, 0.98, 0.98, 0.98, 0.93, 0.86, 0.78, 0.67, 0.00, 0.42, 0.63, 0.73, 0.80, 0.77 },
89{ 0.93, 0.96, 0.99, 0.99, 0.98, 0.92, 0.86, 0.81, 0.42, 0.00, 0.26, 0.50, 0.59, 0.72 },
90{ 0.91, 0.93, 0.98, 1.00, 0.98, 0.98, 0.95, 0.96, 0.63, 0.26, 0.00, 0.24, 0.38, 0.45 },
91{ 0.88, 0.89, 0.99, 0.99, 0.99, 0.98, 0.98, 0.97, 0.73, 0.50, 0.24, 0.00, 0.15, 0.32 },
92{ 0.87, 0.87, 0.95, 0.98, 0.98, 0.98, 0.98, 0.98, 0.80, 0.59, 0.38, 0.15, 0.00, 0.24 },
93{ 0.84, 0.86, 0.97, 0.96, 1.00, 0.99, 1.00, 0.98, 0.77, 0.72, 0.45, 0.32, 0.24, 0.00 }});
94      stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
95        MultidimensionalScaling.KruskalShepard(distancesK));
96      Assert.IsTrue(stress < 0.1);
97    }
98
99    internal DoubleMatrix GetDistances(double[,] coordinates) {
100      int dimension = coordinates.GetLength(0);
101      DoubleMatrix distances = new DoubleMatrix(dimension, dimension);
102      for (int i = 0; i < dimension - 1; i++)
103        for (int j = i + 1; j < dimension; j++) {
104          double sum = 0;
105          for (int k = 0; k < coordinates.GetLength(1); k++)
106            sum += (coordinates[i, k] - coordinates[j, k]) * (coordinates[i, k] - coordinates[j, k]);
107          distances[i, j] = distances[j, i] = Math.Sqrt(sum);
108        }
109      return distances;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.