/// <summary>
        /// Layouts a connected graph with Multidimensional Scaling, using
        /// shortest-path distances as Euclidean target distances.
        /// </summary>
        /// <param name="geometryGraph">A graph.</param>
        /// <param name="settings">The settings for the algorithm.</param>
        /// <param name="x">Coordinate vector.</param>
        /// <param name="y">Coordinate vector.</param>
        internal static void LayoutGraphWithMds(GeometryGraph geometryGraph, MdsLayoutSettings settings, out double[] x, out double[] y)
        {
            x = new double[geometryGraph.Nodes.Count];
            y = new double[geometryGraph.Nodes.Count];
            if (geometryGraph.Nodes.Count == 0)
            {
                return;
            }
            if (geometryGraph.Nodes.Count == 1)
            {
                x[0] = y[0] = 0;
                return;
            }
            int    k        = Math.Min(settings.PivotNumber, geometryGraph.Nodes.Count);
            int    iter     = settings.GetNumberOfIterationsWithMajorization(geometryGraph.Nodes.Count);
            double exponent = settings.Exponent;

            var            pivotArray     = new int[k];
            PivotDistances pivotDistances = new PivotDistances(geometryGraph, false, pivotArray);

            pivotDistances.Run();
            double[][] c = pivotDistances.Result;
            MultidimensionalScaling.LandmarkClassicalScaling(c, out x, out y, pivotArray);
            ScaleToAverageEdgeLength(geometryGraph, x, y);

            if (iter > 0)
            {
                AllPairsDistances apd = new AllPairsDistances(geometryGraph, false);
                apd.Run();
                double[][] d = apd.Result;
                double[][] w = MultidimensionalScaling.ExponentialWeightMatrix(d, exponent);
                // MultidimensionalScaling.DistanceScaling(d, x, y, w, iter);
                MultidimensionalScaling.DistanceScalingSubset(d, x, y, w, iter);
            }
        }
         void SetNodePositionsAndMovedBoundaries() {
            
            int pivotNumber = Math.Min(graph.Nodes.Count,settings.PivotNumber);
            double scaleX = settings.ScaleX;
            double scaleY = settings.ScaleY;
          
            int[] pivotArray = new int[pivotNumber];
            PivotDistances pivotDistances = new PivotDistances(graph, false, pivotArray);
            pivotDistances.Run();
            double[][] c = pivotDistances.Result;
            double[] x, y;
            MultidimensionalScaling.LandmarkClassicalScaling(c, out x, out y, pivotArray);

            Standardize(x);
            double[] p = Centrality.PageRank(graph, .85, false);
            // double[] q = Centrality.PageRank(graph, .85, true);
            Standardize(p);
            // Standardize(q);

            int index = 0;
            foreach (Node node in graph.Nodes) {
                node.Center = new Point((int) (x[index]*scaleX), (int) (Math.Sqrt(p[index])*scaleY));
                index++;
            }

            OverlapRemoval.RemoveOverlaps(graph.Nodes.ToArray(), settings.NodeSeparation);
        }
	public static void Main() {
		modshogun.init_shogun_with_defaults();

		double[,] data = Load.load_numbers("../data/fm_train_real.dat");

		RealFeatures features = new RealFeatures(data);
		MultidimensionalScaling mds = new MultidimensionalScaling();
		mds.set_target_dim(1);
		mds.set_landmark(false);

		mds.apply(features);

	}
Beispiel #4
0
    public static void Main()
    {
        modshogun.init_shogun_with_defaults();

        double[,] data = Load.load_numbers("../data/fm_train_real.dat");

        RealFeatures            features = new RealFeatures(data);
        MultidimensionalScaling mds      = new MultidimensionalScaling();

        mds.set_target_dim(1);
        mds.set_landmark(false);

        mds.apply(features);
    }
Beispiel #5
0
        public void MdsReturnsCorrectResult()
        {
            ILArray <double> dist = ILMath.zeros(3, 3);

            dist[1, 0] = 4.94760097137838;
            dist[2, 0] = 3.48822665076897;

            dist[0, 1] = 4.94760097137838;
            dist[2, 1] = 5.07828347170446;

            dist[0, 2] = 3.48822665076897;
            dist[1, 2] = 5.07828347170446;

            MultidimensionalScaling.Scale(dist);
            Assert.IsTrue(false);
        }
Beispiel #6
0
        private static Point eigenSystem2(Point[] B, out Point[] Q)
        {
            double[][] b = new double[B.Length][];
            for (int i = 0; i < B.Length; i++)
            {
                b[i]    = new double[2];
                b[i][0] = B[i].X;
                b[i][1] = B[i].Y;
            }
            double lambda1;

            double[] q1;
            double   lambda2;

            double[] q2;
            MultidimensionalScaling.SpectralDecomposition(b, out q1, out lambda1, out q2, out lambda2, 300, 1e-8);
            Q = new Point[] { new Point(q1[0], q1[1]), new Point(q2[0], q2[1]) };
            return(new Point(lambda1, lambda2));
        }
        public void TestGoodnessOfFit()
        {
            double       stress;
            DoubleMatrix distances3 = new DoubleMatrix(3, 3);

            // Example 1: A right triangle
            distances3[0, 1] = distances3[1, 0] = 3;
            distances3[0, 2] = distances3[2, 0] = 4;
            distances3[1, 2] = distances3[2, 1] = 5;
            stress           = MultidimensionalScaling.CalculateNormalizedStress(distances3,
                                                                                 MultidimensionalScaling.KruskalShepard(distances3));
            Assert.IsTrue(stress < 0.1);
            // Example 2: An arbitrary triangle
            distances3[0, 1] = distances3[1, 0] = 8;
            distances3[0, 2] = distances3[2, 0] = 6.4;
            distances3[1, 2] = distances3[2, 1] = 5;
            DoubleMatrix coords3 = MultidimensionalScaling.KruskalShepard(distances3);

            Console.WriteLine("Coordinates: ");
            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]);
            stress = MultidimensionalScaling.CalculateNormalizedStress(distances3, coords3);
            Console.WriteLine("Stress = " + stress.ToString(CultureInfo.InvariantCulture.NumberFormat));
            Assert.IsTrue(stress < 0.1);
            DoubleMatrix distances4 = new DoubleMatrix(4, 4);

            // Example 3: A small square
            distances4[0, 1] = distances4[1, 0] = 1;
            distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2);
            distances4[0, 3] = distances4[3, 0] = 1;
            distances4[1, 2] = distances4[2, 1] = 1;
            distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2);
            distances4[2, 3] = distances4[3, 2] = 1;
            stress           = MultidimensionalScaling.CalculateNormalizedStress(distances4,
                                                                                 MultidimensionalScaling.KruskalShepard(distances4));
            Assert.IsTrue(stress < 0.1);
            // Example 4: A large square
            distances4[0, 1] = distances4[1, 0] = 1000;
            distances4[0, 2] = distances4[2, 0] = Math.Sqrt(2000000);
            distances4[0, 3] = distances4[3, 0] = 1000;
            distances4[1, 2] = distances4[2, 1] = 1000;
            distances4[1, 3] = distances4[3, 1] = Math.Sqrt(2000000);
            distances4[2, 3] = distances4[3, 2] = 1000;
            stress           = MultidimensionalScaling.CalculateNormalizedStress(distances4,
                                                                                 MultidimensionalScaling.KruskalShepard(distances4));
            Assert.IsTrue(stress < 0.1);
            // Example 5: An arbitrary cloud of 8 points in a plane
            DoubleMatrix distancesK = GetDistances(new double[, ] {
                { 2, 1 }, { 5, 2 }, { 7, 1 }, { 4, 0 }, { 3, 3 }, { 4, 2 }, { 1, 8 }, { 6, 3 }
            });

            stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
                                                                       MultidimensionalScaling.KruskalShepard(distancesK));
            Assert.IsTrue(stress < 0.1);
            // Example 6: A tetrahedron
            distancesK = GetDistances(new double[, ] {
                { 0, 0, 0 }, { 4, 0, 0 }, { 2, 3.4641, 0 }, { 2, 1.1547, 3.2660 }
            });
            stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
                                                                       MultidimensionalScaling.KruskalShepard(distancesK));
            Assert.IsTrue(stress < 0.1);
            // Example 7: A matrix of perceived dissimilarities between 14 colors, published in the literature
            distancesK = new DoubleMatrix(new double[, ] {
                { 0.00, 0.14, 0.58, 0.58, 0.82, 0.94, 0.93, 0.96, 0.98, 0.93, 0.91, 0.88, 0.87, 0.84 },
                { 0.14, 0.00, 0.50, 0.56, 0.78, 0.91, 0.93, 0.93, 0.98, 0.96, 0.93, 0.89, 0.87, 0.86 },
                { 0.58, 0.50, 0.00, 0.19, 0.53, 0.83, 0.90, 0.92, 0.98, 0.99, 0.98, 0.99, 0.95, 0.97 },
                { 0.58, 0.56, 0.19, 0.00, 0.46, 0.75, 0.90, 0.91, 0.98, 0.99, 1.00, 0.99, 0.98, 0.96 },
                { 0.82, 0.78, 0.53, 0.46, 0.00, 0.39, 0.69, 0.74, 0.93, 0.98, 0.98, 0.99, 0.98, 1.00 },
                { 0.94, 0.91, 0.83, 0.75, 0.39, 0.00, 0.38, 0.55, 0.86, 0.92, 0.98, 0.98, 0.98, 0.99 },
                { 0.93, 0.93, 0.90, 0.90, 0.69, 0.38, 0.00, 0.27, 0.78, 0.86, 0.95, 0.98, 0.98, 1.00 },
                { 0.96, 0.93, 0.92, 0.91, 0.74, 0.55, 0.27, 0.00, 0.67, 0.81, 0.96, 0.97, 0.98, 0.98 },
                { 0.98, 0.98, 0.98, 0.98, 0.93, 0.86, 0.78, 0.67, 0.00, 0.42, 0.63, 0.73, 0.80, 0.77 },
                { 0.93, 0.96, 0.99, 0.99, 0.98, 0.92, 0.86, 0.81, 0.42, 0.00, 0.26, 0.50, 0.59, 0.72 },
                { 0.91, 0.93, 0.98, 1.00, 0.98, 0.98, 0.95, 0.96, 0.63, 0.26, 0.00, 0.24, 0.38, 0.45 },
                { 0.88, 0.89, 0.99, 0.99, 0.99, 0.98, 0.98, 0.97, 0.73, 0.50, 0.24, 0.00, 0.15, 0.32 },
                { 0.87, 0.87, 0.95, 0.98, 0.98, 0.98, 0.98, 0.98, 0.80, 0.59, 0.38, 0.15, 0.00, 0.24 },
                { 0.84, 0.86, 0.97, 0.96, 1.00, 0.99, 1.00, 0.98, 0.77, 0.72, 0.45, 0.32, 0.24, 0.00 }
            });
            stress = MultidimensionalScaling.CalculateNormalizedStress(distancesK,
                                                                       MultidimensionalScaling.KruskalShepard(distancesK));
            Assert.IsTrue(stress < 0.1);
        }