示例#1
0
        public static void Main()
        {
            const double epsilon      = 0.5;
            const int    minNeighbors = 2;
            var          points       = new List <Point>();

            points.AddRange(MakeGaussianCircle(1, 100));
            points.AddRange(MakeGaussianCircle(2, 100));
            points.AddRange(MakeGaussianCircle(3, 100));

            double EuclideanDistance(Point p1, Point p2)
            {
                var(x1, y1) = p1;
                var(x2, y2) = p2;
                return(Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)));
            }

            var clustered = new Dbscan <Point, double>(EuclideanDistance, epsilon, minNeighbors);
            var clusters  = clustered.Cluster(points);

            foreach (var cluster in clusters)
            {
                Console.WriteLine($"Cluster no. {cluster.Key} has {cluster.Value.Count} points");
            }
        }
示例#2
0
        public void ClusterTest()
        {
            var vehicles = new List <Vehicle>
            {
                CreateVehicle(1, 1, 1),
                CreateVehicle(2, 2, 1),
                CreateVehicle(3, 1, 2),
                CreateVehicle(4, 15, 1),

                CreateVehicle(5, 100, 100),
                CreateVehicle(6, 101, 110),
                CreateVehicle(7, 105, 110),
                CreateVehicle(8, 101, 110),

                CreateVehicle(9, 401, 410),
                CreateVehicle(10, 401, 410),
                CreateVehicle(11, 401, 410),

                CreateVehicle(12, 501, 510),
                CreateVehicle(13, 501, 510),
            };
            var clusters = Dbscan.Cluster(vehicles, 15, 3);

            Assert.AreEqual(3, clusters.Count);

            var firstCluster = clusters.ElementAt(0).Select(v => v.Id).ToList();

            Assert.AreEqual(4, firstCluster.Count);
            Assert.True(firstCluster.Contains(1));
            Assert.True(firstCluster.Contains(2));
            Assert.True(firstCluster.Contains(3));
            Assert.True(firstCluster.Contains(4));

            var secondCluster = clusters.ElementAt(1).Select(v => v.Id).ToList();

            Assert.AreEqual(4, secondCluster.Count);
            Assert.True(secondCluster.Contains(5));
            Assert.True(secondCluster.Contains(6));
            Assert.True(secondCluster.Contains(7));
            Assert.True(secondCluster.Contains(8));

            var thirdCluster = clusters.ElementAt(2).Select(v => v.Id).ToList();

            Assert.AreEqual(3, thirdCluster.Count);
            Assert.True(thirdCluster.Contains(9));
            Assert.True(thirdCluster.Contains(10));
            Assert.True(thirdCluster.Contains(11));
        }
示例#3
0
        public void BenchmarkTest(int unitsCount, double radius, int minimumClusterSize, int runsCount, double threshold)
        {
            var stopwatch = new Stopwatch();

            for (var i = 0; i < runsCount; i++)
            {
                var vehicles = GenerateVehicles(unitsCount);
                stopwatch.Start();
                var clusters = Dbscan.Cluster(vehicles, radius, minimumClusterSize);
                stopwatch.Stop();
                Console.WriteLine($"Clusters found: {clusters.Count}, current time spent: {stopwatch.Elapsed}");
            }
            var average = TimeSpan.FromMilliseconds((double)stopwatch.ElapsedMilliseconds / runsCount);

            Console.WriteLine($"Avarage: {average}");
            Assert.Less(average, TimeSpan.FromMilliseconds(threshold));
        }
示例#4
0
        private static void TryIntroduceNewFormations()
        {
            var newVehicleIds = VehicleRegistry
                                .GetNewCreatedVehicleIds()
                                .ToList();

            if (!newVehicleIds.Any())
            {
                return;
            }
            var newVehicles = VehicleRegistry.GetVehiclesByIds(newVehicleIds);
            var clusters    = Dbscan.Cluster(newVehicles, 80, 33);

            if (!clusters.Any())
            {
                return;
            }
            foreach (var cluster in clusters)
            {
                var ids = cluster.Select(v => v.Id).ToList();
                formations.Add(new NewVehiclesFormation(MagicConstants.NextFormationId++, ids, CommandManager, VehicleRegistry));
                VehicleRegistry.RemoveFromNewVehicles(ids);
            }
        }