Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10109


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

#1886 fixed other MDS and added more unit tests

Location:
branches/HeuristicLab.Analysis.AlgorithmBehavior
Files:
4 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++) {
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/PermutationConvexHullView.cs

    r10060 r10109  
    5454        }
    5555
    56         double[][] popPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(dm);
     56        double[][] popPoints = DistanceMatrixToPoints.ConvertDistanceMatrixToPoints(DistanceMatrixToPoints.TransformToDistances(dm));
    5757
    5858        var convexHull = HyperHull.CalculateUsingSMO(popPoints);
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DistanceMatrixToPoints.cs

    r10108 r10109  
    3333     *   
    3434     */
    35     public static double[][] ConvertDistanceMatrixToPoints(double[][] dm) {
     35    public static double[][] ConvertDistanceMatrixToPoints(double[][] dm, int k = 2) {
    3636      double[][] points = new double[dm.Length][];
    3737      double[,] m = new double[dm.Length, dm.Length];
     
    4545      }
    4646
    47       //QR decomposition to get the upper part for smatrixevd
    48       double[] tau;
    49       double[,] r;
    50       alglib.rmatrixqr(ref m, dm.Length, dm.Length, out tau);
    51       alglib.rmatrixqrunpackr(m, dm.Length, dm.Length, out r);
    52 
    53       bool res = alglib.smatrixevd(r, dm.Length, 1, true, out q, out v);
     47      bool res = alglib.smatrixevd(m, dm.Length, 1, true, out q, out v);
    5448      if (!res) throw new Exception("Eigenvalue computation did not converge!");
    5549
    56       int zeroCnt = q.Count(x => x.IsAlmost(0) || x < 0.0);
    57       for (int i = 0; i < dm.Length; i++) {
    58         points[i] = new double[dm.Length - zeroCnt];
    59       }
    60 
    61       int pi = 0;
    62       for (int i = 0; i < dm.Length; i++) {
    63         if (!q[i].IsAlmost(0.0) && q[i] > 0.0) {
    64           for (int j = 0; j < dm.Length; j++) {
    65             points[j][pi] = Math.Sqrt(q[i]) * v[j, i];
    66           }
    67           pi++;
     50      //TODO: this should also work without allocating memory for ev and evec
     51      double[] ev = new double[k];
     52      double[][] evec = new double[dm.Length][];
     53      AllocArray(evec, k);
     54      Array.Copy(q, q.Length - k, ev, 0, k);
     55      for (int i = 0; i < k; i++) {
     56        for (int j = 0; j < dm.Length; j++) {
     57          evec[j][i] = v[j, i + (q.Length - k)];
    6858        }
    6959      }
    7060
     61      double k1 = SumIfLZero(ev);
     62      if (k1 < k) {
     63        throw new Exception("Zero-eigenvalues detected. This leads to a degenerate point set. Use constants. ");
     64        //TODO: handling of this case; implement adding of constants
     65      }
     66
     67      AllocArray(points, k);
     68      for (int i = 0; i < k; i++) {
     69        for (int j = 0; j < dm.Length; j++) {
     70          points[j][i] = Math.Sqrt(ev[i]) * evec[j][i];
     71        }
     72      }
    7173      return points;
    7274    }
Note: See TracChangeset for help on using the changeset viewer.