Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/07/11 12:49:03 (13 years ago)
Author:
mkommend
Message:

#1479: Merged trunk changes into branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Analysis/3.3/MultidimensionalScaling/MultidimensionalScaling.cs

    r5933 r6377  
    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      }
Note: See TracChangeset for help on using the changeset viewer.