Example #1
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
        }
Example #2
0
        // Plan Flight Path button is pressed
        private void btnExecute_Click(object sender, EventArgs e)
        {
            #region Sanity Check

            // Make sure there are tasks in queue
            if (lvQueue.Items.Count < 1)
            {
                System.Windows.Forms.MessageBox.Show("Please select a task first!");
                return;
            }

            // 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;
            }

            // Sanity check to 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

            // Generate path planning requests
            foreach (ListViewItem item in lvQueue.Items)
            {
                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);
                if (chkUseEndPoint.Checked)
                {
                    switch (item.Text)
                    {
                        case "CC":
                            newRequest.AlgToUse = AlgType.CC_E;
                            break;
                        case "LHC-GW-CONV":
                            newRequest.AlgToUse = AlgType.LHCGWCONV_E;
                            break;
                        case "LHC-GW-PF":
                            newRequest.AlgToUse = AlgType.LHCGWPF_E;
                            break;
                        case "LHC-Random":
                            newRequest.AlgToUse = AlgType.LHCRandom_E;
                            break;
                        case "Random":
                            newRequest.AlgToUse = AlgType.Random_E;
                            break;
                        case "CONV":
                            newRequest.AlgToUse = AlgType.CONV_E;
                            break;
                        case "PF":
                            newRequest.AlgToUse = AlgType.PF_E;
                            break;
                        case "TopTwo":
                            newRequest.AlgToUse = AlgType.TopTwo_E;
                            break;
                        case "TopN":
                            newRequest.AlgToUse = AlgType.TopN_E;
                            break;
                        case "TopTwoH":
                            newRequest.AlgToUse = AlgType.TopTwoH_E;
                            break;
                        case "TopNH":
                            newRequest.AlgToUse = AlgType.TopNH_E;
                            break;
                        case "EA":
                            newRequest.AlgToUse = AlgType.EA_E;
                            break;
                        case "RealTime":
                            newRequest.AlgToUse = AlgType.RealTime_E;
                            break;
                    }
                }
                else
                {
                    switch (item.Text)
                    {
                        case "CC":
                            newRequest.AlgToUse = AlgType.CC;
                            break;
                        case "LHC-GW-CONV":
                            newRequest.AlgToUse = AlgType.LHCGWCONV;
                            break;
                        case "LHC-GW-PF":
                            newRequest.AlgToUse = AlgType.LHCGWPF;
                            break;
                        case "LHC-Random":
                            newRequest.AlgToUse = AlgType.LHCRandom;
                            break;
                        case "Random":
                            newRequest.AlgToUse = AlgType.Random;
                            break;
                        case "CONV":
                            newRequest.AlgToUse = AlgType.CONV;
                            break;
                        case "PF":
                            newRequest.AlgToUse = AlgType.PF;
                            break;
                        case "TopTwo":
                            newRequest.AlgToUse = AlgType.TopTwo;
                            break;
                        case "TopN":
                            newRequest.AlgToUse = AlgType.TopN;
                            break;
                        case "TopTwoH":
                            newRequest.AlgToUse = AlgType.TopTwoH;
                            break;
                        case "TopNH":
                            newRequest.AlgToUse = AlgType.TopNH;
                            break;
                        case "EA":
                            newRequest.AlgToUse = AlgType.EA;
                            break;
                        case "RealTime":
                            newRequest.AlgToUse = AlgType.RealTime;
                            break;
                    }
                }
                newRequest.DrawPath = chkShowPath.Checked;
                if (chkBatchRun.Checked)
                {
                    newRequest.BatchRun = true;
                    newRequest.RunTimes = Convert.ToInt16(ntxtRunTimes.Value);
                }

                // 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;
                }

                // Remember TopN parameter for TopTwo and TopN algorithms
                if (newRequest.AlgToUse == AlgType.TopTwo || newRequest.AlgToUse == AlgType.TopTwo_E)
                {
                    newRequest.TopN = 2;
                }
                if (newRequest.AlgToUse == AlgType.TopN || newRequest.AlgToUse == AlgType.TopN_E)
                {
                    newRequest.TopN = Convert.ToInt32(ntxtTopNCount.Value);
                }

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

                // Debug: Test
                test = newRequest;

                // Add to server queue and pass alone the object
                frmParent.SubmitToRequestQueue(newRequest);
            }

            // Clear task queue
            lvQueue.Clear();
            lvQueue.Columns.Add("Task");
            lvQueue.Columns[0].Width = lvQueue.Width - 5;
        }