[5723] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Data;
|
---|
| 3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 4 |
|
---|
| 5 | namespace HeuristicLab.Analysis.Tests {
|
---|
| 6 | [TestClass]
|
---|
| 7 | public class MultidimensionalScalingTest {
|
---|
| 8 | [TestMethod]
|
---|
| 9 | public void TestGoodnessOfFit() {
|
---|
| 10 | double stress;
|
---|
| 11 | DoubleMatrix distances3 = new DoubleMatrix(3, 3);
|
---|
| 12 | // Example 1: A right triangle
|
---|
| 13 | distances3[0, 1] = distances3[1, 0] = 3;
|
---|
| 14 | distances3[0, 2] = distances3[2, 0] = 4;
|
---|
| 15 | distances3[1, 2] = distances3[2, 1] = 5;
|
---|
| 16 | MultidimensionalScaling.MetricByDistance(distances3, out stress);
|
---|
| 17 | Assert.IsTrue(stress < 0.1);
|
---|
| 18 | // Example 2: An arbitrary triangle
|
---|
| 19 | distances3[0, 1] = distances3[1, 0] = 8;
|
---|
| 20 | distances3[0, 2] = distances3[2, 0] = 6.4;
|
---|
| 21 | distances3[1, 2] = distances3[2, 1] = 5;
|
---|
| 22 | MultidimensionalScaling.MetricByDistance(distances3, out stress);
|
---|
| 23 | Assert.IsTrue(stress < 0.1);
|
---|
| 24 | DoubleMatrix distances4 = new DoubleMatrix(4, 4);
|
---|
| 25 | // Example 3: A small square
|
---|
| 26 | distances4[0, 1] = distances4[1, 0] = 1;
|
---|
| 27 | distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2);
|
---|
| 28 | distances4[0, 3] = distances4[3, 0] = 1;
|
---|
| 29 | distances4[1, 2] = distances4[2, 1] = 1;
|
---|
| 30 | distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2);
|
---|
| 31 | distances4[2, 3] = distances4[3, 2] = 1;
|
---|
| 32 | MultidimensionalScaling.MetricByDistance(distances4, out stress);
|
---|
| 33 | Assert.IsTrue(stress < 0.1);
|
---|
| 34 | // Example 4: A large square
|
---|
| 35 | distances4[0, 1] = distances4[1, 0] = 1000;
|
---|
| 36 | distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2000000);
|
---|
| 37 | distances4[0, 3] = distances4[3, 0] = 1000;
|
---|
| 38 | distances4[1, 2] = distances4[2, 1] = 1000;
|
---|
| 39 | distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2000000);
|
---|
| 40 | distances4[2, 3] = distances4[3, 2] = 1000;
|
---|
| 41 | MultidimensionalScaling.MetricByDistance(distances4, out stress);
|
---|
| 42 | Assert.IsTrue(stress < 0.1);
|
---|
| 43 | // Example 5: An arbitrary cloud of 8 points in a plane
|
---|
| 44 | DoubleMatrix distancesK = GetDistances(new double[,] { { 2, 1 }, { 5, 2 }, { 7, 1 }, { 4, 0 }, { 3, 3 }, { 4, 2 }, { 1, 8 }, { 6, 3 } });
|
---|
| 45 | MultidimensionalScaling.MetricByDistance(distancesK, out stress);
|
---|
| 46 | Assert.IsTrue(stress < 0.1);
|
---|
| 47 | // Example 6: A tetrahedron
|
---|
| 48 | distancesK = GetDistances(new double[,] { { 0, 0, 0 }, { 4, 0, 0 }, { 2, 3.4641, 0 }, { 2, 1.1547, 3.2660 } });
|
---|
| 49 | MultidimensionalScaling.MetricByDistance(distancesK, out stress);
|
---|
| 50 | Assert.IsTrue(stress < 0.2);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | internal DoubleMatrix GetDistances(double[,] coordinates) {
|
---|
| 54 | int dimension = coordinates.GetLength(0);
|
---|
| 55 | DoubleMatrix distances = new DoubleMatrix(dimension, dimension);
|
---|
| 56 | for (int i = 0; i < dimension - 1; i++)
|
---|
| 57 | for (int j = i + 1; j < dimension; j++) {
|
---|
| 58 | double sum = 0;
|
---|
| 59 | for (int k = 0; k < coordinates.GetLength(1); k++)
|
---|
| 60 | sum += (coordinates[i, k] - coordinates[j, k]) * (coordinates[i, k] - coordinates[j, k]);
|
---|
| 61 | distances[i, j] = distances[j, i] = Math.Sqrt(sum);
|
---|
| 62 | }
|
---|
| 63 | return distances;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|