コード例 #1
0
ファイル: Form1.cs プロジェクト: luosz/ai-algorithmplatform
        private void button1_Click(object sender, EventArgs e)
        {
            //Point[] nPoints = getPoints((Bitmap)pictureBox1.Image);
            List <IPosition> al = new List <IPosition>();

            al = pointList;
            //for (int i = 0; i < nPoints.Length; i++)
            //{
            //   al.Add(new KD2DPoint(nPoints[i]));
            //   // all.Add(new KD2DPoint(nPoints[i]));
            //}
            // KDTree Ktree = KDTree.CreateKDTree(all);
            INearestNeighbor M2Mnn = new M2M_NN();
            INearestNeighbor KDTnn = new KDT_NN();
            //M2Mnn.Init(400, 400, 100, 5, 5);
            int runTimes = 10000;

            M2Mnn.PreProcess(al);

            KDTnn.PreProcess(al);
            Graphics g = Graphics.FromImage(pictureBox1.Image);

            int       px = int.Parse(textBox1.Text);
            int       py = int.Parse(textBox2.Text);
            KD2DPoint AskPoint = new KD2DPoint(px, py);
            long      timeMM, timeKD;
            Stopwatch sW       = new Stopwatch();
            KD2DPoint nearest  = null;
            KD2DPoint nearest2 = null;

            sW.Start();
            for (int i = 0; i < runTimes; i++)
            {
                nearest = (KD2DPoint)M2Mnn.NearestNeighbor(AskPoint);
            }
            sW.Stop();
            timeMM = sW.ElapsedTicks / runTimes;
            sW.Reset();
            sW.Start();
            for (int i = 0; i < runTimes; i++)
            {
                nearest2 = (KD2DPoint)KDTnn.NearestNeighbor(AskPoint);
            }
            if (!nearest.Equals(nearest2))
            {
                MessageBox.Show("结果不符!");
            }
            sW.Stop();
            timeKD = sW.ElapsedTicks / runTimes;
            sW.Reset();
            M2Mlabel.Text = timeMM.ToString();
            KDTlabel.Text = timeKD.ToString();

            //   label4.Text = "最近距离:" + dis.ToString();
            g.DrawLine(new Pen(Color.SteelBlue), new PointF(AskPoint.X, AskPoint.Y), new PointF(nearest.X, nearest.Y));


            pictureBox1.Refresh();
        }
コード例 #2
0
        static public bool NearestNeighbourTest()
        {
            int              range  = 4000;
            int              Pnum   = 100000;
            int              Tnum   = 1000;
            Random           r      = new Random();
            Stopwatch        sWatch = new Stopwatch();
            List <IPosition> pl     = new List <IPosition>();

            for (int i = 0; i < Pnum; i++)
            {
                // KD2DPoint pp = new KD2DPoint((float)r.NextDouble() * (range - 1), (float)r.NextDouble() * (range - 1));

                float     x  = (float)r.NextDouble() * r.Next((range - 1));
                KD2DPoint pp = new KD2DPoint(x, ((range - 1) - x));//(float)(200+Math.Sin(x)* 200));


                pl.Add(pp);
            }


            INearestNeighbor SearchNearestNeighborEngine = new M2M_NN();

            //SearchNearestNeighborEngine.Init(range, range, 8000, 5, 4);

            SearchNearestNeighborEngine.PreProcess(pl);

            INearestNeighbor KDTSearchNN = new KDT_NN();

            KDTSearchNN.PreProcess(pl);
            KD2DPoint expected, actual;
            long      timeKD, timeMM;

            timeKD = 0;
            timeMM = 0;

            List <IPosition> testPointList = new List <IPosition>();

            for (int i = 0; i < Tnum; i++)
            {
                KD2DPoint NearestNeighbour_target2 = new KD2DPoint((float)r.NextDouble() * (range - 1), (float)r.NextDouble() * (range - 1)); // TODO: 初始化为适当的值

                testPointList.Add(NearestNeighbour_target2);
            }

            //Console.WriteLine("x: " + NearestNeighbour_target2.GetX().ToString() + "   y: " + NearestNeighbour_target2.GetY().ToString());

            sWatch.Reset();
            sWatch.Start();
            KD2DPoint p1, p2;
            int       err = 0;

            foreach (IPosition testPoint in testPointList)
            {
                p1 = (KD2DPoint)
                     SearchNearestNeighborEngine.NearestNeighbor(testPoint);
                //   double b= Math.Sqrt(Math.Pow(p1.X - testPoint.GetX(), 2) + Math.Pow(p1.X - testPoint.GetX(), 2));
                //}
                //sWatch.Stop();

                //Console.WriteLine("M2M:" + sWatch.ElapsedMilliseconds);

                //sWatch.Reset();
                //sWatch.Start();
                //foreach (Position testPoint in testPointList)
                //{
                p2 = (KD2DPoint)
                     KDTSearchNN.NearestNeighbor(testPoint);
                if (!p2.Equals(p1))
                {
                    Console.WriteLine("不符!");

                    float dist1 = (float)Math.Sqrt((double)((testPoint.GetX() - p1.GetX()) * (testPoint.GetX() - p1.GetX()) + (testPoint.GetY() - p1.GetY()) * (testPoint.GetY() - p1.GetY())));

                    float dist2 = (float)Math.Sqrt((double)((testPoint.GetX() - p2.GetX()) * (testPoint.GetX() - p2.GetX()) + (testPoint.GetY() - p2.GetY()) * (testPoint.GetY() - p2.GetY())));

                    Console.WriteLine("m2mnn: " + dist1.ToString());
                    Console.WriteLine("kdnn: " + dist2.ToString());

                    for (int i = 0; i < 100; i++)
                    {
                        SearchNearestNeighborEngine.NearestNeighbor(testPoint);
                        KDTSearchNN.NearestNeighbor(testPoint);
                    }

                    err++;
                }
            }
            sWatch.Stop();
            Console.WriteLine("测试点数:" + Tnum.ToString() + ";不准确数:" + err.ToString());
            Console.WriteLine("KD:" + sWatch.ElapsedMilliseconds);



            //timeMM+=sWatch.ElapsedMilliseconds;
            //sWatch.Reset();
            //sWatch.Start();
            //expected = (KD2DPoint)KDTSearchNN.NearestNeighbor((Position)NearestNeighbour_target2);
            //sWatch.Stop();
            //timeKD += sWatch.ElapsedMilliseconds;
            //sWatch.Reset();
            //if (!expected.Equals(actual))
            //    Console.WriteLine("不符合!");


            //Console.WriteLine("测试完毕!");
            //Console.WriteLine("kd:" + timeKD.ToString());
            //Console.WriteLine("M2M:"+timeMM.ToString());


            return(true);
        }
コード例 #3
0
ファイル: Analyse.cs プロジェクト: luosz/ai-algorithmplatform
 //运行KDTNN算法预处理过程
 private void runKDTNNPP(double n, Object o)
 {
     kdt_nn.PreProcess((List <IPosition>)o);
 }