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 HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
|
---|
24 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
25 |
|
---|
26 | namespace AlgorithmBehaviorUnitTests {
|
---|
27 | [TestClass]
|
---|
28 | public class DistanceMatrixToPointsTest {
|
---|
29 | [TestMethod]
|
---|
30 | public void TestDistanceMatrixConversion() {
|
---|
31 | int nrOfPoints = 4;
|
---|
32 | int dim = 2;
|
---|
33 | double[][] orgPoints = new double[nrOfPoints][];
|
---|
34 | double[][] orgDm = new double[nrOfPoints][];
|
---|
35 | double[][] newDm = new double[nrOfPoints][];
|
---|
36 | double[][] newPoints = null;
|
---|
37 |
|
---|
38 |
|
---|
39 | AllocArray(orgPoints, dim);
|
---|
40 | AllocArray(orgDm, nrOfPoints);
|
---|
41 | AllocArray(newDm, nrOfPoints);
|
---|
42 | SamplePoints(orgPoints);
|
---|
43 | CalculateDistanceMatrix(orgDm, orgPoints);
|
---|
44 |
|
---|
45 | newPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(orgDm);
|
---|
46 |
|
---|
47 | CalculateDistanceMatrix(newDm, newPoints);
|
---|
48 | //TODO
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | private static void SamplePoints(double[][] points) {
|
---|
53 | Random rand = new Random();
|
---|
54 |
|
---|
55 | for (int i = 0; i < points.Length; i++) {
|
---|
56 | for (int j = 0; j < points[i].Length; j++) {
|
---|
57 | points[i][j] = rand.NextDouble() * 100;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static void CalculateDistanceMatrix(double[][] dm, double[][] points) {
|
---|
63 | for (int i = 0; i < points.Length; i++) {
|
---|
64 | for (int j = 0; j < points.Length; j++) {
|
---|
65 | dm[i][j] = CalcEuclideanDistance(points[i], points[j]);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static double CalcEuclideanDistance(double[] p1, double[] p2) {
|
---|
71 | double result = 0.0;
|
---|
72 | for (int i = 0; i < p1.Length; i++) {
|
---|
73 | result += Math.Pow(p1[i] - p2[i], 2);
|
---|
74 | }
|
---|
75 | return Math.Sqrt(result);
|
---|
76 | }
|
---|
77 |
|
---|
78 | private static void AllocArray(double[][] arr, int size) {
|
---|
79 | for (int i = 0; i < arr.Length; i++) {
|
---|
80 | arr[i] = new double[size];
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|