コード例 #1
0
        public void EnumeratorTest()
        {
            ClusterPoint p = new ClusterPoint(1, 2, Guid.NewGuid());
            MemoryStream stream = new MemoryStream();
            const int NumElements = 5;
            for (int i = 0; i < NumElements; i++)
            {
                stream.Write(p.ToByteArray(), 0, ClusterPoint.Size);
            }

            ObjectStreamReader<ClusterPoint> pointStream = new ObjectStreamReader<ClusterPoint>(new MemoryStream(stream.ToArray()), ClusterPoint.FromByteArray, ClusterPoint.Size);
            Assert.AreEqual(p.CentroidID, pointStream.First().CentroidID);

            DateTime serialStart = DateTime.Now;
            int[] serialOutput = pointStream.Select(point =>
            {
                System.Threading.Thread.Sleep(200);
                return 1;
            }).ToArray();
            DateTime serialEnd = DateTime.Now;
            Assert.AreEqual(NumElements, serialOutput.Length);

            DateTime parallelStart = DateTime.Now;
            int[] parallelOutput = pointStream.AsParallel().Select(point =>
            {
                System.Threading.Thread.Sleep(200);
                return 1;
            }).ToArray();
            DateTime parallelEnd = DateTime.Now;
            Assert.AreEqual(NumElements, parallelOutput.Length);

            System.Diagnostics.Trace.WriteLine(string.Format("serial: {0}, parallel: {1}",
                (serialEnd - serialStart).TotalSeconds,
                (parallelEnd - parallelStart).TotalSeconds));
        }
コード例 #2
0
        private ClusterPointProcessingResult AssignClusterPointToNearestCentroid(ClusterPoint clusterPoint)
        {
            // TODO: Try in-place modification to see the performance impact
            ClusterPoint result = new ClusterPoint(clusterPoint);
            result.CentroidID = centroids.MinElement(centroid => Point.Distance(clusterPoint, centroid)).ID;

            return new ClusterPointProcessingResult
            {
                Point = result,
                PointWasChanged = clusterPoint.CentroidID != result.CentroidID
            };
        }
コード例 #3
0
        public void ClusterPointConstructorTest()
        {
            Point p = new Point
            {
                X = 3.25F,
                Y = 4.12F
            };
            Guid centroidID = Guid.NewGuid();

            ClusterPoint target = new ClusterPoint(p, centroidID);

            Assert.AreEqual(p.X, target.X);
            Assert.AreEqual(p.Y, target.Y);
            Assert.AreEqual(centroidID, target.CentroidID);
        }
コード例 #4
0
        public void AssignClusterPointToNearestCentroidTest()
        {
            KMeansTaskData task = new KMeansTaskData(Guid.NewGuid(), Guid.NewGuid(), 1, null, 2, 3, 10, 0, null, DateTime.Now, DateTime.Now, 0, null);
            KMeansTaskProcessor_Accessor target = new KMeansTaskProcessor_Accessor(task);

            target.centroids = new List<Centroid>();
            target.centroids.Add(new Centroid
            {
                ID = Guid.NewGuid(),
                X = 0.0F,
                Y = -1.0F
            });
            target.centroids.Add(new Centroid
            {
                ID = Guid.NewGuid(),
                X = 10.0F,
                Y = 10.0F
            });

            ClusterPoint clusterPoint = new ClusterPoint
            {
                CentroidID = Guid.Empty,
                X = 1.0F,
                Y = 2.0F
            };

            ClusterPoint expected = new ClusterPoint
            {
                CentroidID = target.centroids[0].ID,
                X = 1.0F,
                Y = 2.0F
            };
            ClusterPointProcessingResult_Accessor actual;
            actual = target.AssignClusterPointToNearestCentroid(clusterPoint);

            Assert.AreEqual(expected.CentroidID, actual.Point.CentroidID);
        }
コード例 #5
0
        public void FromByteArrayTest()
        {
            Guid centroidID = Guid.NewGuid();

            MemoryStream stream = new MemoryStream();
            stream.Write(BitConverter.GetBytes(2.53), 0, sizeof(double));
            stream.Write(BitConverter.GetBytes(4.56), 0, sizeof(double));
            stream.Write(centroidID.ToByteArray(), 0, 16);
            byte[] bytes = stream.ToArray();

            ClusterPoint expected = new ClusterPoint
            {
                X = 2.53F,
                Y = 4.56F,
                CentroidID = centroidID
            };
            ClusterPoint actual;
            actual = ClusterPoint.FromByteArray(bytes);

            const double Epsilon = 0.0001;
            Assert.IsTrue(Math.Abs(expected.X - actual.X) < Epsilon);
            Assert.IsTrue(Math.Abs(expected.Y - actual.Y) < Epsilon);
            Assert.AreEqual(expected.CentroidID, actual.CentroidID);
        }
コード例 #6
0
        public void ToByteArrayTest()
        {
            Guid centroidID = Guid.NewGuid();
            ClusterPoint target = new ClusterPoint
            {
                X = 1.23,
                Y = 3.45,
                CentroidID = centroidID
            };

            MemoryStream stream = new MemoryStream();
            stream.Write(BitConverter.GetBytes(1.23), 0, sizeof(double));
            stream.Write(BitConverter.GetBytes(3.45), 0, sizeof(double));
            stream.Write(centroidID.ToByteArray(), 0, 16);
            byte[] expected = stream.ToArray();

            byte[] actual;
            actual = target.ToByteArray();
            Assert.AreEqual(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }
        }
コード例 #7
0
 public ClusterPoint(ClusterPoint other)
     : base(other)
 {
     this.CentroidID = other.CentroidID;
 }
コード例 #8
0
        public void ProcessWorkerResponseTest()
        {
            KMeansJobData jobData = new KMeansJobData(Guid.NewGuid(), 4, null, 2, 10, DateTime.Now);
            KMeansJob_Accessor target = new KMeansJob_Accessor(jobData, "server");
            target.InitializeStorage();

            // Upload a block with an arbitrary ClusterPoint, so we can verify it gets copied
            ClusterPoint arbitraryPoint = new ClusterPoint(1, 2, Guid.NewGuid());
            List<string> blockList;
            using (ObjectCachedBlockWriter<ClusterPoint> pointPartitionWriteStream = new ObjectCachedBlockWriter<ClusterPoint>(target.Points, point => point.ToByteArray(), ClusterPoint.Size,
                Environment.GetEnvironmentVariable("TEMP") + @"\" + Guid.NewGuid().ToString()))
            {
                pointPartitionWriteStream.Write(arbitraryPoint);
                pointPartitionWriteStream.FlushBlock();
                blockList = pointPartitionWriteStream.BlockList;
            }

            KMeansTaskData taskData = new KMeansTaskData(jobData, Guid.NewGuid(), 0, 1, target.Centroids.Uri, DateTime.Now, 0, null);

            target.tasks.Clear();
            target.tasks.Add(new KMeansTask(taskData));

            KMeansTaskResult taskResult = new KMeansTaskResult(taskData);
            CloudBlob pointsBlockListBlob = AzureHelper.CreateBlob(jobData.JobID.ToString(), Guid.NewGuid().ToString());
            using (Stream stream = pointsBlockListBlob.OpenWrite())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, blockList);
            }
            taskResult.PointsBlockListBlob = pointsBlockListBlob.Uri;
            taskResult.NumPointsChanged = 2;
            Guid centroidID = Guid.NewGuid();
            taskResult.PointsProcessedDataByCentroid = new Dictionary<Guid, PointsProcessedData> {
                { centroidID, new PointsProcessedData() {
                        NumPointsProcessed = 2,
                        PartialPointSum = new Point(1, 2)
                    }
                }
            };
            target.ProcessWorkerResponse(taskResult, new List<Worker>());

            // Verify that the first ClusterPoint in Points is indeed equal to arbitraryPoint
            using (ObjectStreamReader<ClusterPoint> pointsStream = new ObjectStreamReader<ClusterPoint>(target.Points, ClusterPoint.FromByteArray, ClusterPoint.Size))
            {
                ClusterPoint point = pointsStream.First();
                Assert.AreEqual(arbitraryPoint.X, point.X);
                Assert.AreEqual(arbitraryPoint.Y, point.Y);
                Assert.AreEqual(arbitraryPoint.CentroidID, point.CentroidID);
            }
        }