Beispiel #1
0
        public void Run()
        {
            int BatchCount = 1;

            if (curRequest.BatchRun)
            {
                BatchCount = curRequest.RunTimes;
            }

            // First do reachable area (Dist and Diff)
            RtwMatrix mDistReachable;
            RtwMatrix mDiffReachable;

            if (curRequest.T < curRequest.DistMap.Rows + curRequest.DistMap.Columns)
            {
                mDistReachable = curRequest.DistMap.Clone();
                mDiffReachable = curRequest.DiffMap.Clone();
                if (!ComputeReachableArea(mDistReachable, mDiffReachable))
                {
                    // Cannot plan path.
                    return;
                }
            }
            else
            {
                mDistReachable = curRequest.DistMap.Clone();
                mDiffReachable = curRequest.DiffMap.Clone();
            }

            // Then do mode count (If Diff map is used, multiply first)
            CountDistModes myCount;
            if (curRequest.UseTaskDifficultyMap)
            {

                RtwMatrix mRealModes = new RtwMatrix(mDistReachable.Rows, mDistReachable.Columns);
                for (int i = 0; i < mRealModes.Rows; i++)
                {
                    for (int j = 0; j < mRealModes.Columns; j++)
                    {
                        mRealModes[i, j] = mDistReachable[i, j] *
                            (float)curRequest.DiffRates[Convert.ToInt32(mDiffReachable[i, j])];
                    }
                }
                myCount = new CountDistModes(mRealModes);
            }
            else
            {
                myCount = new CountDistModes(mDistReachable);
            }

            int ModeCount = myCount.GetCount();
            RtwMatrix mModes = myCount.GetModes();
            myCount = null;
            //Console.WriteLine("ModeCount = " + ModeCount);

            // Then do efficiency lower bound
            ComputeEfficiencyUB myELB = new ComputeEfficiencyUB(curRequest, mDistReachable, mDiffReachable);
            double Efficiency_UB = myELB.GetEfficiency_UB();
            myELB = null;

            // Set sample rate based on max value in mdistReachable.
            float max = mDistReachable.MinMaxValue()[1];
            ProjectConstants.DownSample_Rate = max / 10f;

            // Do the batch run of Path Planning
            List<double> AllRunTimes = new List<double>();
            List<double> AllEfficiencies = new List<double>();
            for (int i = 0; i < BatchCount; i++)
            {
                // Run them sequencially and don't use multiple threads.
                PathPlanningTask curTask = new PathPlanningTask(curRequest, ModeCount, mModes, mDistReachable, mDiffReachable, Efficiency_UB);
                curTask.Run();
                AllRunTimes.Add(curTask.GetRunTime());
                AvgRunTime += curTask.GetRunTime();
                AllEfficiencies.Add(curTask.GetEfficiency());
                AvgEfficiency += curTask.GetEfficiency();
                Path = curTask.GetPath();
                curTask = null;
            }
            AvgRunTime = AvgRunTime / BatchCount;
            StdRunTime = ComputeStDev(AllRunTimes, AvgRunTime);
            AvgEfficiency = AvgEfficiency / BatchCount;
            StdEfficiency = ComputeStDev(AllEfficiencies, AvgEfficiency);

            // Log path planning activities
            if (ProjectConstants.DebugMode)
            {
                curRequest.SetLog("----------------------------------------------\n");
                curRequest.SetLog("Average run time: " + AvgRunTime.ToString() + "\n");
                curRequest.SetLog("Standard deviation: " + StdRunTime.ToString() + "\n");
                curRequest.SetLog("Average efficiency: " + AvgEfficiency.ToString() + "\n");
                curRequest.SetLog("Standard deviation: " + StdEfficiency.ToString() + "\n");
                curRequest.SetLog("----------------------------------------------");
                curRequest.SetLog("----------------------------------------------\n");
            }
        }
Beispiel #2
0
        // When the test button is pressed (Count how many modes)
        private void btnTest_Click(object sender, EventArgs e)
        {
            #region Test Mode Count
            //DateTime startTime = DateTime.Now;
            //CountDistModes myCount;
            //if (chkUseDiff.Checked)
            //{
            //    RtwMatrix mDistReachable = test.DistMap.Clone();
            //    RtwMatrix mDiffReachable = test.DiffMap.Clone();

            //    RtwMatrix mRealModes = new RtwMatrix(CurDiffMap.Rows, CurDiffMap.Columns);
            //    for (int i = 0; i < mRealModes.Rows; i++)
            //    {
            //        for (int j = 0; j < mRealModes.Columns; j++)
            //        {
            //            mRealModes[i, j] = mDistReachable[i, j] *
            //                (float)test.DiffRates[Convert.ToInt32(mDiffReachable[i, j])];
            //        }
            //    }
            //    myCount = new CountDistModes(mRealModes);

            //    // Debug code: Showing the product of dist and diff
            //    frmMap mapReal = new frmMap();
            //    Bitmap CurBMPReal = new Bitmap(mRealModes.Columns, mRealModes.Rows);
            //    ImgLib.MatrixToImage(ref mRealModes, ref CurBMPReal);
            //    mapReal.Text = "Real probability distribution with respect to difficulty map";
            //    mapReal.setImage(CurBMPReal);
            //    mapReal.Show();
            //    mapReal.resetImage();
            //}
            //else
            //{
            //    myCount = new CountDistModes(CurDistMap);
            //}

            //Log(myCount.GetCount().ToString() + "\n");
            //DateTime stopTime = DateTime.Now;
            //TimeSpan duration = stopTime - startTime;
            //Log("Computation took " + duration.ToString() + " seconds.\n");
            //// Show mode nodes
            //RtwMatrix myModes = myCount.GetModes().Clone();
            //for (int i = 0; i < myModes.Rows; i++)
            //{
            //    for (int j = 0; j < myModes.Columns; j++)
            //    {
            //        if (myModes[i, j] > 0)
            //        {
            //            myModes[i, j] = 255;
            //        }
            //    }
            //}
            //// Convert matrix to image
            //Bitmap CurBMP = new Bitmap(myModes.Columns, myModes.Rows);
            //ImgLib.MatrixToImage(ref myModes, ref CurBMP);
            //// Showing map in map form
            //frmMap myModesForm = new frmMap(this);
            //myModesForm.Text = "Modes Map";
            //myModesForm.setImage(CurBMP);
            //myModesForm.Show();

            //myCount = null;
            #endregion

            #region Test permutation

            //int[] intInput = { 1, 2, 3, 4};
            //Log(ShowPermutations<int>(intInput, 3));

            //string[] stringInput = { "Hello", "World", "Foo" };
            //Log(ShowPermutations<string>(stringInput, 2));

            #endregion

            #region Test MATLAB
            //////////////////////
            //// Input Parameters
            //////////////////////

            //// create an array ar for the real part of "a"
            //System.Array ar = new double[2];
            //ar.SetValue(11, 0);
            //ar.SetValue(12, 1);

            //// create an array ai for the imaginary part of "a"
            //System.Array ai = new double[2];
            //ai.SetValue(1, 0);
            //ai.SetValue(2, 1);

            //// create an array br for the real part of "b"
            //System.Array br = new double[2];
            //br.SetValue(21, 0);
            //br.SetValue(22, 1);

            //// create an array bi for the imaginary part of "b"
            //System.Array bi = new double[2];
            //bi.SetValue(3, 0);
            //bi.SetValue(4, 1);

            ///////////////////////
            //// Output Parameters
            ///////////////////////

            //// initialize variables for return value from ML
            //System.Array cr = new double[2];
            //System.Array ci = new double[2];
            //System.Array dr = new double[2];
            //System.Array di = new double[2];

            //////////////////////////
            //// Call MATLAB function
            //////////////////////////
            //// call appropriate function/method based on Mode
            //// use MATLAB engine
            //UseEngine(ar, ai, br, bi, ref cr, ref ci, ref dr, ref di);

            //Log("ar = " + ar.GetValue(0).ToString() + " " + ar.GetValue(1).ToString() + "\n");
            //Log("ai = " + ai.GetValue(0).ToString() + " " + ai.GetValue(1).ToString() + "\n");
            //Log("br = " + br.GetValue(0).ToString() + " " + br.GetValue(1).ToString() + "\n");
            //Log("bi = " + bi.GetValue(0).ToString() + " " + bi.GetValue(1).ToString() + "\n");
            //Log("cr = " + cr.GetValue(0).ToString() + " " + cr.GetValue(1).ToString() + "\n");
            //Log("ci = " + ci.GetValue(0).ToString() + " " + ci.GetValue(1).ToString() + "\n");
            //Log("dr = " + dr.GetValue(0).ToString() + " " + dr.GetValue(1).ToString() + "\n");
            //Log("di = " + di.GetValue(0).ToString() + " " + di.GetValue(1).ToString() + "\n");
            #endregion

            #region Test MATH.NET EVD

            //DenseMatrix m = new DenseMatrix(new[,] { { 81.1887, -18.4630 }, { -18.4630, 115.9033 } });
            //System.Numerics.Complex[] d = m.Evd().EigenValues().ToArray();
            //double a = d[0].Real;
            //double b = d[1].Real;
            //Log(a + " " + b);

            #endregion

            #region Test Arrays

            //int n = 3;
            //Array arrModes = new double[n];
            //Array arrMUs = new double[n, 2];
            //Array arrSigmaXSigmaY = new double[n];

            //for (int i = 0; i < n; i++)
            //{
            //    // Means
            //    arrMUs.SetValue(21, i, 0);
            //    arrMUs.SetValue(22, i, 1);
            //}

            #endregion

            #region Compute Efficiency with existing path

            #region Sanity Check
            // Make sure maps are loaded
            if (chkUseDist.Checked && CurDistMap == null)
            {
                System.Windows.Forms.MessageBox.Show("Please load a probability distribution map first!");
                return;
            }
            if (chkUseDiff.Checked && CurDiffMap == null)
            {
                System.Windows.Forms.MessageBox.Show("Please load a task-difficulty map first!");
                return;
            }

            // Make sure distribution map and task-difficulty map have same size
            if (chkUseDiff.Checked && chkUseDist.Checked)
            {
                if (CurDistMap.Rows != CurDiffMap.Rows || CurDistMap.Columns != CurDiffMap.Columns)
                {
                    System.Windows.Forms.MessageBox.Show("Please make sure the distribution map and the " +
                    "task-difficulty map must be the same size!");
                    return;
                }
            }

            // Use default distribution map or task-difficulty map if only one is checked
            if (chkUseDiff.Checked && !chkUseDist.Checked)
            {
                CurDistMap = new RtwMatrix(CurDiffMap.Rows, CurDiffMap.Columns);
            }
            if (!chkUseDiff.Checked && chkUseDist.Checked)
            {
                CurDiffMap = new RtwMatrix(CurDistMap.Rows, CurDistMap.Columns);
            }

            #endregion

            // Create request object
            PathPlanningRequest newRequest = new PathPlanningRequest();

            #region Setting Request Object Properties

            newRequest.UseDistributionMap = chkUseDist.Checked;
            newRequest.UseTaskDifficultyMap = chkUseDiff.Checked;
            newRequest.UseHierarchy = chkHierarchy.Checked;
            newRequest.UseCoarseToFineSearch = chkCoaseToFine.Checked;
            newRequest.UseParallelProcessing = chkParallel.Checked;
            if (rbtnFixWing.Checked)
            {
                newRequest.VehicleType = UAVType.FixWing;
            }
            if (rbtnCopter.Checked)
            {
                newRequest.VehicleType = UAVType.Copter;
            }
            if (rbtnFixedAmount.Checked)
            {
                newRequest.DetectionType = DType.FixAmount;
            }
            if (rbtnFixedAmountPercent.Checked)
            {
                newRequest.DetectionType = DType.FixAmountInPercentage;
            }
            if (rbtnFixedPercent.Checked)
            {
                newRequest.DetectionType = DType.FixPercentage;
            }
            newRequest.DetectionRate = Convert.ToDouble(ntxtDetectionRate.Value);
            newRequest.DistMap = CurDistMap;
            newRequest.DiffMap = CurDiffMap;
            newRequest.UseEndPoint = chkUseEndPoint.Checked;
            newRequest.T = trbFlightTime.Value;
            newRequest.pStart.column = Convert.ToInt16(ntxtSX.Value);
            newRequest.pStart.row = Convert.ToInt16(ntxtSY.Value);
            newRequest.pEnd.column = Convert.ToInt16(ntxtEX.Value);
            newRequest.pEnd.row = Convert.ToInt16(ntxtEY.Value);
            newRequest.AlgToUse = AlgType.LHCGWCONV;
            newRequest.DrawPath = chkShowPath.Checked;

            #endregion

            #region Find max task-difficulty and compute diff rates only once

            if (chkUseDiff.Checked)
            {
                newRequest.MaxDifficulty = Convert.ToInt32(CurDiffMap.MinMaxValue()[1]);
                // Set task-difficulty rates
                double[] DiffRates = new double[newRequest.MaxDifficulty + 1];
                double rate = 1.0 / (newRequest.MaxDifficulty + 1);
                for (int i = 0; i < newRequest.MaxDifficulty + 1; i++)
                {
                    DiffRates[i] = 1 - i * rate;
                }
                newRequest.DiffRates = DiffRates;
            }

            if (!newRequest.SanityCheck())
            {
                System.Windows.Forms.MessageBox.Show(newRequest.GetLog());
                return;
            }

            #endregion

            // Read in existing path using hard-coded file path
            string BAPathFileName = @"H:\Research\20 New IPPAs for Partial Detection Conference Paper\Selected Cases Maps\BAPath.csv";
            RtwMatrix BAPath = MISCLib.ReadInMap(BAPathFileName);

            #region First do reachable area (Dist and Diff)

            RtwMatrix mDistReachable;
            RtwMatrix mDiffReachable;

            if (newRequest.T < newRequest.DistMap.Rows + newRequest.DistMap.Columns)
            {
                mDistReachable = newRequest.DistMap.Clone();
                mDiffReachable = newRequest.DiffMap.Clone();
                if (!ComputeReachableArea(newRequest, mDistReachable, mDiffReachable))
                {
                    // Cannot plan path.
                    return;
                }
            }
            else
            {
                mDistReachable = newRequest.DistMap.Clone();
                mDiffReachable = newRequest.DiffMap.Clone();
            }

            #endregion

            // Then do efficiency lower bound
            ComputeEfficiencyUB myELB = new ComputeEfficiencyUB(newRequest, mDistReachable, mDiffReachable);
            double Efficiency_UB = myELB.GetEfficiency_UB();
            List<Point> TeleportPath = myELB.GetTeleportPath();
            myELB = null;

            // Creating path planning object
            AlgPathPlanning curAlg = new AlgGlobalWarming(newRequest, 1, mDistReachable, mDiffReachable, Efficiency_UB);

            // Call method to compute existing path CDF
            List<Point> Path = new List<Point>();
            for(int i=0; i<BAPath.Rows; i++)
            {
                Point p = new Point(Convert.ToInt16(BAPath[i,0]), Convert.ToInt16(BAPath[i,1]));
                Path.Add(p);
            }

            float CDF = curAlg.GetTrueCDF(Path);

            // Compute efficiency
            double Efficiency = 0;
            if (Efficiency_UB == 0)
            {
                Efficiency = 1;
            }
            else
            {
                Efficiency = CDF / Efficiency_UB;
            }

            // Compute CDF for graph
            Console.WriteLine("Print Existing Path CDF Graph:");
            curAlg.PrintCDFGraph(TeleportPath, newRequest.DistMap);
            Console.WriteLine("Print BAPath CDF Graph:");
            curAlg.PrintCDFGraph(Path, newRequest.DistMap);
            curAlg.SetPath(Path);

            // Show results
            Log("----------------------------------------------\n");
            Log("Efficiency: " + Efficiency.ToString() + "\n");
            Log("----------------------------------------------");
            Log("----------------------------------------------\n");

            #region Show path

            if (newRequest.DrawPath)
            {
                // Draw path with map remains
                Bitmap CurBMP3 = new Bitmap(mDistReachable.Columns, mDistReachable.Rows);
                ImgLib.MatrixToImage(ref mDistReachable, ref CurBMP3);
                frmMap map3 = new frmMap();
                map3.Text = "UAV trajectory and coverage";
                map3.setImage(CurBMP3);
                map3.Show();
                map3.resetImage();
                List<float> remains = curAlg.ShowCoverage();
                Color c = Color.FromArgb(255, 0, 0);
                for (int i = 0; i < Path.Count; i++)
                {
                    Point p = Path[i];
                    map3.setPointColor(p, c);
                    map3.Refresh();
                    map3.setPointColor(p, remains[i]);
                    map3.Refresh();
                }

                // Drawing real path
                MISCLib.ShowImage(MISCLib.DrawPath(Path), "Real Path");
            }

            #endregion

            #endregion
        }