[5932] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5932] | 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;
|
---|
[6342] | 23 | using System.Globalization;
|
---|
[5723] | 24 | using HeuristicLab.Data;
|
---|
| 25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 26 |
|
---|
[9764] | 27 | namespace HeuristicLab.Analysis.Tests {
|
---|
[5723] | 28 | [TestClass]
|
---|
| 29 | public class MultidimensionalScalingTest {
|
---|
| 30 | [TestMethod]
|
---|
[9783] | 31 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
| 32 | [TestProperty("Time", "short")]
|
---|
[5723] | 33 | public void TestGoodnessOfFit() {
|
---|
| 34 | double stress;
|
---|
| 35 | DoubleMatrix distances3 = new DoubleMatrix(3, 3);
|
---|
| 36 | // Example 1: A right triangle
|
---|
| 37 | distances3[0, 1] = distances3[1, 0] = 3;
|
---|
| 38 | distances3[0, 2] = distances3[2, 0] = 4;
|
---|
| 39 | distances3[1, 2] = distances3[2, 1] = 5;
|
---|
[5871] | 40 | stress = MultidimensionalScaling.CalculateNormalizedStress(distances3,
|
---|
| 41 | MultidimensionalScaling.KruskalShepard(distances3));
|
---|
[5723] | 42 | Assert.IsTrue(stress < 0.1);
|
---|
| 43 | // Example 2: An arbitrary triangle
|
---|
| 44 | distances3[0, 1] = distances3[1, 0] = 8;
|
---|
| 45 | distances3[0, 2] = distances3[2, 0] = 6.4;
|
---|
| 46 | distances3[1, 2] = distances3[2, 1] = 5;
|
---|
[6342] | 47 | DoubleMatrix coords3 = MultidimensionalScaling.KruskalShepard(distances3);
|
---|
| 48 | Console.WriteLine("Coordinates: ");
|
---|
| 49 | Console.WriteLine("A = ({0}, {1}), B = ({2}, {3}), C = ({4}, {5})", coords3[0, 0], coords3[0, 1], coords3[1, 0], coords3[1, 1], coords3[2, 0], coords3[2, 1]);
|
---|
| 50 | stress = MultidimensionalScaling.CalculateNormalizedStress(distances3, coords3);
|
---|
| 51 | Console.WriteLine("Stress = " + stress.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
[5723] | 52 | Assert.IsTrue(stress < 0.1);
|
---|
| 53 | DoubleMatrix distances4 = new DoubleMatrix(4, 4);
|
---|
| 54 | // Example 3: A small square
|
---|
| 55 | distances4[0, 1] = distances4[1, 0] = 1;
|
---|
| 56 | distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2);
|
---|
| 57 | distances4[0, 3] = distances4[3, 0] = 1;
|
---|
| 58 | distances4[1, 2] = distances4[2, 1] = 1;
|
---|
| 59 | distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2);
|
---|
| 60 | distances4[2, 3] = distances4[3, 2] = 1;
|
---|
[5871] | 61 | stress = MultidimensionalScaling.CalculateNormalizedStress(distances4,
|
---|
| 62 | MultidimensionalScaling.KruskalShepard(distances4));
|
---|
[5723] | 63 | Assert.IsTrue(stress < 0.1);
|
---|
| 64 | // Example 4: A large square
|
---|
| 65 | distances4[0, 1] = distances4[1, 0] = 1000;
|
---|
| 66 | distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2000000);
|
---|
| 67 | distances4[0, 3] = distances4[3, 0] = 1000;
|
---|
| 68 | distances4[1, 2] = distances4[2, 1] = 1000;
|
---|
| 69 | distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2000000);
|
---|
| 70 | distances4[2, 3] = distances4[3, 2] = 1000;
|
---|
[5871] | 71 | stress = MultidimensionalScaling.CalculateNormalizedStress(distances4,
|
---|
| 72 | MultidimensionalScaling.KruskalShepard(distances4));
|
---|
[5723] | 73 | Assert.IsTrue(stress < 0.1);
|
---|
| 74 | // Example 5: An arbitrary cloud of 8 points in a plane
|
---|
| 75 | DoubleMatrix distancesK = GetDistances(new double[,] { { 2, 1 }, { 5, 2 }, { 7, 1 }, { 4, 0 }, { 3, 3 }, { 4, 2 }, { 1, 8 }, { 6, 3 } });
|
---|
[5871] | 76 | stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
|
---|
| 77 | MultidimensionalScaling.KruskalShepard(distancesK));
|
---|
[5723] | 78 | Assert.IsTrue(stress < 0.1);
|
---|
| 79 | // Example 6: A tetrahedron
|
---|
| 80 | distancesK = GetDistances(new double[,] { { 0, 0, 0 }, { 4, 0, 0 }, { 2, 3.4641, 0 }, { 2, 1.1547, 3.2660 } });
|
---|
[5871] | 81 | stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
|
---|
| 82 | MultidimensionalScaling.KruskalShepard(distancesK));
|
---|
| 83 | Assert.IsTrue(stress < 0.1);
|
---|
[5938] | 84 | // Example 7: A matrix of perceived dissimilarities between 14 colors, published in the literature
|
---|
| 85 | distancesK = new DoubleMatrix(new double[,] {
|
---|
| 86 | { 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 },
|
---|
| 87 | { 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 },
|
---|
| 88 | { 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 },
|
---|
| 89 | { 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 },
|
---|
| 90 | { 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 },
|
---|
| 91 | { 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 },
|
---|
| 92 | { 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 },
|
---|
| 93 | { 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 },
|
---|
| 94 | { 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 },
|
---|
| 95 | { 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 },
|
---|
| 96 | { 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 },
|
---|
| 97 | { 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 },
|
---|
| 98 | { 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 },
|
---|
| 99 | { 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 }});
|
---|
| 100 | stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
|
---|
| 101 | MultidimensionalScaling.KruskalShepard(distancesK));
|
---|
| 102 | Assert.IsTrue(stress < 0.1);
|
---|
[5723] | 103 | }
|
---|
| 104 |
|
---|
| 105 | internal DoubleMatrix GetDistances(double[,] coordinates) {
|
---|
| 106 | int dimension = coordinates.GetLength(0);
|
---|
| 107 | DoubleMatrix distances = new DoubleMatrix(dimension, dimension);
|
---|
| 108 | for (int i = 0; i < dimension - 1; i++)
|
---|
| 109 | for (int j = i + 1; j < dimension; j++) {
|
---|
| 110 | double sum = 0;
|
---|
| 111 | for (int k = 0; k < coordinates.GetLength(1); k++)
|
---|
| 112 | sum += (coordinates[i, k] - coordinates[j, k]) * (coordinates[i, k] - coordinates[j, k]);
|
---|
| 113 | distances[i, j] = distances[j, i] = Math.Sqrt(sum);
|
---|
| 114 | }
|
---|
| 115 | return distances;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|