Esempio n. 1
0
    // Called by LandMap.GetZones (), returns number of subregions
    public int ClusterLocationsAccordKMedoidsVoronoi(MapPoint[,] points, TerrainVerticesDatabase vertDatabase)
    {
        // K-means cluster algorithm to separate locations in the regions

        int regionId = 0;

        for (int isleId = 0; isleId < regions.Count; isleId++)
        {
            MapRegion region = regions[isleId];

            double[][] tileLocations = new double[region.turf.Count][];

            for (int i = 0; i < tileLocations.Length; i++)
            {
                tileLocations[i] = new double[3];

                TerrainVertData vertData = vertDatabase.GetVertDataFromRegionTile(region.turf[i], isleId);
                if (vertData != null)
                {
                    tileLocations[i][0] = region.turf[i].x;
                    tileLocations[i][1] = vertData.inlandPosition;
                    tileLocations[i][2] = region.turf[i].y;
                }
                else
                {
                    LoggerTool.Post("Null from VertDB for " + region.turf[i].ToString());
                }
            }

            int k = InitializeNumOfK(tileLocations.Length);
            k = Mathf.Max(1, k);
            Debug.Log(k + " centroid(s)");

            VoronoiIteration kmedoidsVoronoi = new VoronoiIteration(k);
            kmedoidsVoronoi.MaxIterations = 100;

            KMedoidsClusterCollection <double> clusters = kmedoidsVoronoi.Learn(tileLocations);

            int[] labels = clusters.Decide(tileLocations);

            Debug.Log("Number of labels = " + labels.Length);
            for (int i = 0; i < labels.Length; i++)
            {
                points[(int)tileLocations[i][0], (int)tileLocations[i][2]].areaValue = regionId + labels[i];
            }

            regionId += k;
        }

        return(regionId);
    }
        public void doc_learn()
        {
            #region doc_learn
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some observations
            int[][] observations = new int[][]
            {
                new[] { 2, 6 }, // a
                new[] { 3, 4 }, // a
                new[] { 3, 8 }, // a
                new[] { 4, 7 }, // a
                new[] { 6, 2 }, // b
                new[] { 6, 4 }, // b
                new[] { 7, 3 }, // b
                new[] { 7, 4 }, // b
                new[] { 8, 5 }, // b
                new[] { 7, 6 }  // b
            };

            // Create a new 2-Medoids algorithm.
            var kmedoidsVi = new VoronoiIteration <int>(2, new Manhattan());
            kmedoidsVi.MaxIterations = 100;

            // Set initial medoids
            kmedoidsVi.Clusters.Centroids[0] = observations[1];
            kmedoidsVi.Clusters.Centroids[1] = observations[7];

            // Compute and retrieve the data centroids
            var clusters = kmedoidsVi.Learn(observations);

            // Use the centroids to parition all the data
            int[] labels = clusters.Decide(observations);
            #endregion

            Assert.AreEqual(labels[0], labels[1]);
            Assert.AreEqual(labels[0], labels[2]);
            Assert.AreEqual(labels[0], labels[3]);

            Assert.AreEqual(labels[4], labels[5]);
            Assert.AreEqual(labels[4], labels[6]);
            Assert.AreEqual(labels[4], labels[7]);
            Assert.AreEqual(labels[4], labels[8]);
            Assert.AreEqual(labels[4], labels[9]);
        }
Esempio n. 3
0
    public int ClusterLocationsAccordKMedoidsVoronoi(MapPoint[,] points)
    {
        // K-medoids cluster algorithm to separate locations in the regions

        int regionId = 0;

        for (int isleId = 0; isleId < regions.Count; isleId++)
        {
            MapRegion region = regions[isleId];

            double[][] tileLocations = new double[region.turf.Count][];

            for (int i = 0; i < tileLocations.Length; i++)
            {
                tileLocations[i]    = new double[2];
                tileLocations[i][0] = region.turf[i].x;
                tileLocations[i][1] = region.turf[i].y;
            }

            int k = InitializeNumOfK(region.turf.Count);
            Debug.Log(k + " centroid(s)");

            VoronoiIteration kmedoidsVoronoi = new VoronoiIteration(k);
            kmedoidsVoronoi.MaxIterations = 100;

            KMedoidsClusterCollection <double> clusters = kmedoidsVoronoi.Learn(tileLocations);

            int[] labels = clusters.Decide(tileLocations);

            Debug.Log("Number of labels = " + labels.Length);
            for (int i = 0; i < labels.Length; i++)
            {
                points[(int)tileLocations[i][0], (int)tileLocations[i][1]].areaValue = regionId + labels[i];
            }

            regionId += k;
        }

        return(regionId);
    }