Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/05/13 23:57:18 (11 years ago)
Author:
ascheibe
Message:

#1886 fixed other MDS and added more unit tests

Location:
branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/AlgorithmBehaviorUnitTests.csproj

    r10108 r10109  
    6161      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
    6262    </Reference>
     63    <Reference Include="HeuristicLab.Optimization.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     64      <SpecificVersion>False</SpecificVersion>
     65      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization.Operators-3.3.dll</HintPath>
     66    </Reference>
    6367    <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    6468      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     69    </Reference>
     70    <Reference Include="HeuristicLab.Problems.TravelingSalesman-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     71      <SpecificVersion>False</SpecificVersion>
     72      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.TravelingSalesman-3.3.dll</HintPath>
    6573    </Reference>
    6674    <Reference Include="HeuristicLab.Random-3.3">
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/DistanceMatrixToPointsTest.cs

    r10108 r10109  
    2323using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
    2424using HeuristicLab.Common;
     25using HeuristicLab.Encodings.PermutationEncoding;
     26using HeuristicLab.Problems.TravelingSalesman;
    2527using Microsoft.VisualStudio.TestTools.UnitTesting;
    2628
     
    2931  public class DistanceMatrixToPointsTest {
    3032    [TestMethod]
     33    public void TestMetricMDSStatic() {
     34      int nrOfPoints = 3;
     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);
     44      StaticPoints(orgPoints);
     45      CalculateDistanceMatrix(orgDm, orgPoints);
     46
     47      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm);
     48
     49      CalculateDistanceMatrix(newDm, newPoints);
     50      Console.WriteLine("orgDm:");
     51      PrintDM(orgDm);
     52      Console.WriteLine("newDm:");
     53      PrintDM(newDm);
     54
     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      }
     61    }
     62
     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]
    3195    public void TestDistanceMatrixConversionStatic() {
    3296      int nrOfPoints = 3;
     
    40104      AllocArray(orgDm, nrOfPoints);
    41105      AllocArray(newDm, nrOfPoints);
    42       //SamplePoints(orgPoints);
    43106      StaticPoints(orgPoints);
    44107      CalculateDistanceMatrix(orgDm, orgPoints);
    45108
    46       newPoints = DistanceMatrixToPoints.MetricMDS(orgDm);
     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() {
     158      int nrOfPoints = 30;
     159      int dim = 20;
     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
     170      newPoints = DistanceMatrixToPoints.MetricMDS(orgDm, dim);
    47171
    48172      CalculateDistanceMatrix(newDm, newPoints);
     
    79203    }
    80204
     205    private static void SamplePermutations(Permutation[] points, int dim) {
     206      var rand = new HeuristicLab.Random.FastRandom();
     207
     208      for (int i = 0; i < points.Length; i++) {
     209        var p = new Permutation(PermutationTypes.RelativeUndirected, dim, rand);
     210        points[i] = p;
     211      }
     212    }
     213
    81214    private static void StaticPoints(double[][] points) {
    82215      points[0][0] = 2;
     
    98231    }
    99232
     233    private static double[][] CalculateDistanceMatrixFromPermutations(Permutation[] points) {
     234      double[][] tmpDm = new double[points.Length][];
     235      AllocArray(tmpDm, points.Length);
     236
     237      for (int i = 0; i < points.Length; i++) {
     238        for (int j = 0; j < points.Length; j++) {
     239          double diversity = TSPSimilarityCalculator.CalculateSimilarity(points[i], points[j]);
     240          tmpDm[i][j] = diversity;
     241        }
     242      }
     243
     244      return DistanceMatrixToPoints.TransformToDistances(tmpDm);
     245    }
     246
    100247    private static void AllocArray(double[][] arr, int size) {
    101248      for (int i = 0; i < arr.Length; i++) {
Note: See TracChangeset for help on using the changeset viewer.