Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6338


Ignore:
Timestamp:
05/30/11 17:21:30 (14 years ago)
Author:
abeham
Message:

#1539

  • changed MDS optimization algorithm to Levenberg-Marquardt
Location:
branches/histogram/HeuristicLab.Analysis/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/histogram/HeuristicLab.Analysis/3.3/MultidimensionalScaling/MultidimensionalScaling.cs

    r5933 r6338  
    6868    /// In every iteration it tries to find the best location for every item.</param>
    6969    /// <returns>A Nx2 matrix where the first column represents the x- and the second column the y coordinates.</returns>
    70     public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities, DoubleMatrix coordinates, int maximumIterations = 20) {
     70    public static DoubleMatrix KruskalShepard(DoubleMatrix dissimilarities, DoubleMatrix coordinates, int maximumIterations = 10) {
    7171      int dimension = dissimilarities.Rows;
    7272      if (dimension != dissimilarities.Columns || coordinates.Rows != dimension) throw new ArgumentException("The number of coordinates and the number of rows and columns in the dissimilarities matrix do not match.");
     
    7575      double epsf = 0;
    7676      double epsx = 0;
    77       int maxits = 100;
    78       alglib.mincgstate state = null;
    79       alglib.mincgreport rep;
     77      int maxits = 0;
    8078
     79      alglib.minlmstate state;
     80      alglib.minlmreport rep;
    8181      for (int iterations = 0; iterations < maximumIterations; iterations++) {
    8282        bool changed = false;
     
    8585
    8686          try {
    87             if ((iterations == 0 && i == 0)) {
    88               alglib.mincgcreate(c, out state);
    89               alglib.mincgsetcond(state, epsg, epsf, epsx, maxits);
    90             } else {
    91               alglib.mincgrestartfrom(state, c);
    92             }
    93             alglib.mincgoptimize(state, StressGradient, null, new Info(coordinates, dissimilarities, i));
    94             alglib.mincgresults(state, out c, out rep);
     87            alglib.minlmcreatevj(dimension - 1, c, out state);
     88            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);
     89            alglib.minlmoptimize(state, StressFitness, StressJacobian, null, new Info(coordinates, dissimilarities, i));
     90            alglib.minlmresults(state, out c, out rep);
    9591          } catch (alglib.alglibexception) { }
    9692          if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) {
     
    105101    }
    106102
    107     // computes the function and the gradient of the raw stress function.
    108     private static void StressGradient(double[] x, ref double func, double[] grad, object obj) {
    109       func = 0; grad[0] = 0; grad[1] = 0;
     103    private static void StressFitness(double[] x, double[] fi, object obj) {
    110104      Info info = (obj as Info);
    111105      for (int i = 0; i < info.Coordinates.Rows; i++) {
    112         double c = info.Dissimilarities[info.Row, i];
     106        double f = Stress(x, info.Dissimilarities[info.Row, i], info.Coordinates[i, 0], info.Coordinates[i, 1]);
     107        if (i < info.Row) fi[i] = f;
     108        else if (i > info.Row) fi[i - 1] = f;
     109      }
     110    }
     111
     112    private static void StressJacobian(double[] x, double[] fi, double[,] jac, object obj) {
     113      Info info = (obj as Info);
     114      int idx = 0;
     115      for (int i = 0; i < info.Coordinates.Rows; i++) {
    113116        if (i != info.Row) {
     117          double c = info.Dissimilarities[info.Row, i];
    114118          double a = info.Coordinates[i, 0];
    115119          double b = info.Coordinates[i, 1];
    116           func += Stress(x, c, a, b);
    117           grad[0] += ((2 * x[0] - 2 * a) * Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a) - 2 * c * x[0] + 2 * a * c) / Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a);
    118           grad[1] += ((2 * x[1] - 2 * b) * Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a) - 2 * c * x[1] + 2 * b * c) / Math.Sqrt(x[1] * x[1] - 2 * b * x[1] + x[0] * x[0] - 2 * a * x[0] + b * b + a * a);
     120          double f = Stress(x, c, a, b);
     121          fi[idx] = f;
     122          jac[idx, 0] = 2 * (x[0] - a) * (Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])) - c) / Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1]));
     123          jac[idx, 1] = 2 * (x[1] - b) * (Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1])) - c) / Math.Sqrt((a - x[0]) * (a - x[0]) + (b - x[1]) * (b - x[1]));
     124          idx++;
    119125        }
    120126      }
  • branches/histogram/HeuristicLab.Analysis/3.3/Tests/MultidimensionalScalingTest.cs

    r5938 r6338  
    2121
    2222using System;
     23using System.Globalization;
    2324using HeuristicLab.Data;
    2425using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    4243      distances3[0, 2] = distances3[2, 0] = 6.4;
    4344      distances3[1, 2] = distances3[2, 1] = 5;
    44       stress = MultidimensionalScaling.CalculateNormalizedStress(distances3,
    45         MultidimensionalScaling.KruskalShepard(distances3));
     45      DoubleMatrix coords3 = MultidimensionalScaling.KruskalShepard(distances3);
     46      Console.WriteLine("Coordinates: ");
     47      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]);
     48      stress = MultidimensionalScaling.CalculateNormalizedStress(distances3, coords3);
     49      Console.WriteLine("Stress = " + stress.ToString(CultureInfo.InvariantCulture.NumberFormat));
    4650      Assert.IsTrue(stress < 0.1);
    4751      DoubleMatrix distances4 = new DoubleMatrix(4, 4);
Note: See TracChangeset for help on using the changeset viewer.