// Clone self with deep copy public PathPlanningRequest DeepClone() { PathPlanningRequest clonedRequest = new PathPlanningRequest(); clonedRequest.UseTaskDifficultyMap = this.UseTaskDifficultyMap; clonedRequest.UseHierarchy = this.UseHierarchy; clonedRequest.UseCoarseToFineSearch = this.UseCoarseToFineSearch; clonedRequest.UseParallelProcessing = this.UseParallelProcessing; clonedRequest.VehicleType = this.VehicleType; clonedRequest.DetectionType = this.DetectionType; clonedRequest.DetectionRate = this.DetectionRate; clonedRequest.DistMap = this.DistMap.Clone(); clonedRequest.DiffMap = this.DiffMap.Clone(); clonedRequest.UseEndPoint = this.UseEndPoint; clonedRequest.T = this.T; clonedRequest.pStart = new DistPoint(pStart.row, pStart.column); clonedRequest.pEnd = new DistPoint(pEnd.row, pEnd.column); clonedRequest.AlgToUse = this.AlgToUse; clonedRequest.BatchRun = this.BatchRun; clonedRequest.RunTimes = this.RunTimes; clonedRequest.MaxDifficulty = this.MaxDifficulty; if (this.DiffRates != null) { double[] DiffRatesCopy = new double[DiffRates.Length]; Array.Copy(DiffRates, 0, DiffRatesCopy, 0, DiffRates.Length); clonedRequest.DiffRates = DiffRatesCopy; } clonedRequest.DrawPath = this.DrawPath; clonedRequest.d = this.d; clonedRequest.TopN = this.TopN; return clonedRequest; }
// Building the path planning request object and does sanity check private bool BuildRequest(PathPlanningRequest newRequest) { bool goodRequest = true; ListViewItem item = lvQueue.Items[0]; 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; // 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()); goodRequest = false; } // Clear task queue lvQueue.Clear(); lvQueue.Columns.Add("Task"); lvQueue.Columns[0].Width = lvQueue.Width - 5; return goodRequest; }
// Construct byte array for Server Queue Item private byte[] RequestToByteArray(PathPlanningRequest newRequest) { byte[] bytes; ProtoBuffer.PathPlanningRequest.Builder newPBRequest = ProtoBuffer.PathPlanningRequest.CreateBuilder(); newPBRequest.SetUseDistributionMap(newRequest.UseDistributionMap) .SetUseTaskDifficultyMap(newRequest.UseTaskDifficultyMap) .SetUseHierarchy(newRequest.UseHierarchy) .SetUseCoarseToFineSearch(newRequest.UseCoarseToFineSearch) .SetUseParallelProcessing(newRequest.UseParallelProcessing) .SetVehicleType((ProtoBuffer.PathPlanningRequest.Types.UAVType)newRequest.VehicleType) .SetDetectionType((ProtoBuffer.PathPlanningRequest.Types.DType)newRequest.DetectionType) .SetDetectionRate(newRequest.DetectionRate) .SetUseEndPoint(newRequest.UseEndPoint) .SetT(newRequest.T) .SetPStart(DistPointToPBDistPoint(newRequest.pStart)) .SetPEnd(DistPointToPBDistPoint(newRequest.pEnd)) .SetAlgToUse((ProtoBuffer.PathPlanningRequest.Types.AlgType)newRequest.AlgToUse) .SetBatchRun(newRequest.BatchRun) .SetRunTimes(newRequest.RunTimes) .SetMaxDifficulty(newRequest.MaxDifficulty) .SetDrawPath(newRequest.DrawPath) .SetD(newRequest.d) .SetTopNCount(newRequest.TopN); // Have to deal with DiffRates array separately if (newRequest.DiffRates != null) { for (int i = 0; i < newRequest.DiffRates.Length; i++) { newPBRequest.AddDiffRate(newRequest.DiffRates[i]); } } //Here do the matrix thing //// Debug Code //// Test with small matrix //// int d = 40; //int d = 100; //int v = 1; //RtwMatrix testM = new RtwMatrix(d, d); //for (int i = 0; i < d; i++) //{ // for (int j = 0; j < d; j++) // { // testM[i, j] = v; // } //} //newPBRequest.SetDistMap(RtwMatrixToPBMatrix(testM)); if (newRequest.DistMap != null) { newPBRequest.SetDistMap(RtwMatrixToPBMatrix(newRequest.DistMap)); } if (newRequest.DiffMap != null) { newPBRequest.SetDiffMap(RtwMatrixToPBMatrix(newRequest.DiffMap)); } ProtoBuffer.PathPlanningRequest Request = newPBRequest.Build(); newRequest = null; // Finally the ServerQueueItem ProtoBuffer.ServerQueueItem.Builder newServerQueueItem = new ProtoBuffer.ServerQueueItem.Builder(); newServerQueueItem.SetCallerIP("127.0.0.1") .SetCurRequest(Request); ProtoBuffer.ServerQueueItem ServerQueueItem = newServerQueueItem.Build(); newServerQueueItem = null; bytes = ServerQueueItem.ToByteArray(); return bytes; }
// Plan Flight Path button is pressed private void btnExecute_Click(object sender, EventArgs e) { // Sanity check #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 PathPlanningRequest newRequest = new PathPlanningRequest(); if (!BuildRequest(newRequest)) { return; } // Build Path Request into Object // Object via protocol buffer byte[] outStream = RequestToByteArray(newRequest); // Add header to indicate data size byte[] outStreamFinal = outStreamFinal = ProtoBuffer.TCPHandler.AddDataSizeHeader(outStream); // Send request try { TcpClient clientSocket = new TcpClient(); clientSocket.Connect("127.0.0.1", 8888); NetworkStream serverStream = clientSocket.GetStream(); // Send data over socket connection serverStream.Write(outStreamFinal, 0, outStreamFinal.Length); serverStream.Flush(); // Get server response byte[] inStream = ProtoBuffer.TCPHandler.RecieveData(clientSocket, serverStream); // Convert byte array to things we want if (inStream.Length == 0) { System.Windows.Forms.MessageBox.Show("Server exception occurred. No data received!"); } else { Log("Efficiency = " + BitConverter.ToDouble(inStream, 0) + "\n"); Log("Run Time = " + BitConverter.ToDouble(inStream, 8) + "\n"); int[] curP = new int[inStream.Length - 16]; Log("Path = "); for (int i = 16; i < inStream.Length; i = i + 4) { Log(BitConverter.ToInt32(inStream, i).ToString() + " "); if ((i / 4) % 2 == 1) { Log(", "); } } } } catch (System.Net.Sockets.SocketException ex) { if (ex.ErrorCode == 10061) { System.Windows.Forms.MessageBox.Show("Server not available!"); } else { Log(ex.Message); } } //TODO Deal with path response // First 4 bytes size of data // Next 4 bytes Efficiency // Rest path }