private void picMap_MouseDown(object sender, MouseEventArgs e)
 {
     if (bcapture == true)
     {
         Point p1 = new Point(e.X, e.Y);
         PointsList.Add(p1);
         Bitmap b1 = bMap;
         MyImageProc.DrawX(b1, ref bMap, p1);
         picMap.Image = bMap;
     }
 }
        private void timer1_Tick(object sender, EventArgs e)
        {
            lblBestResult.Text = "Fitness:" + ga.bestMember.fitness.ToString() + "\n" +
                                 ga.bestMember.ToString();

            Bitmap b1 = new Bitmap(bOrigMap);

            MyImageProc.DrawBestTour(b1, ref bMap, PointsList, ga.bestMember.mem);
            picMap.Image    = bMap;
            lblStatus2.Text = ("It,Fitness:" + ga.iterationCount.ToString() + " " + ga.pop[0].fitness.ToString() +
                               "  ");
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            PS.PathChangedClient pathClient = new PS.PathChangedClient();

            lblBestResult.Text = "Fitness:" + ga.bestMember.fitness.ToString() + "\n" +
                                 ga.bestMember.ToString();

            Bitmap b1 = new Bitmap(bOrigMap);

            MyImageProc.DrawBestTour(b1, ref bMap, PointsList, ga.bestMember.mem);
            picMap.Image    = bMap;
            lblStatus2.Text = ("It,Fitness:" + ga.iterationCount.ToString() + " " + ga.pop[0].fitness.ToString() +
                               "  ");


            bestPath    = int.Parse(ga.bestMember.fitness.ToString());
            currentPath = ga.bestMember.ToString();
            pathClient.ChangePath(bestPath, currentPath);
        }
        private void btnEndCapture_Click(object sender, EventArgs e)
        {
            bcapture         = false;
            btnRunGA.Enabled = true;
            timer1.Enabled   = false;
            Bitmap b1 = new Bitmap(bOrigMap);

            MyImageProc.DrawTour(b1, ref b1, PointsList);
            picMap.Image                = b1;
            TSGAConstants.NumCities     = PointsList.Count;
            lblNumCities.Text           = TSGAConstants.NumCities.ToString();
            TSGAConstants.MutationRate  = float.Parse(txtMutationRate.Text);
            TSGAConstants.CrossoverRate = float.Parse(txtCrossOverRate.Text);


            if (deserialized == false)
            {
                //-------------now compute the distance matrix---
                DistMat = new int[PointsList.Count, PointsList.Count];
                int dX = 0;
                int dY = 0;
                int dist;
                for (int i = 0; i < PointsList.Count; i++)
                {
                    for (int j = i; j < PointsList.Count; j++)
                    {
                        if (i == j)
                        {
                            DistMat[i, j] = 0;
                        }
                        else
                        {
                            dX            = Math.Abs(((Point)PointsList[i]).X - ((Point)PointsList[j]).X);
                            dY            = Math.Abs(((Point)PointsList[i]).Y - ((Point)PointsList[j]).Y);
                            dist          = (int)(Math.Sqrt(dX * dX + dY * dY));
                            DistMat[i, j] = dist;
                            DistMat[j, i] = dist;
                        }
                    }
                }
            }
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            lock (lockobj)
            {
                try
                {
                    int bestOne = 0;
                    lblBestResult0.Text = "Thread 1: It:" + GAs[0].iterationCount.ToString() + " " +
                                          "Fitness:" + GAs[0].bestMember.fitness.ToString() + " MutationRate:" + GAs[0].mutationRate.ToString() +
                                          "\n" + GAs[0].bestMember.ToString();
                    lblBestResult1.Text = "Thread 2: It:" + GAs[1].iterationCount.ToString() + " " +
                                          "Fitness:" + GAs[1].bestMember.fitness.ToString() + " MutationRate:" + GAs[1].mutationRate.ToString() +
                                          "\n" + GAs[1].bestMember.ToString();
                    if (numThreads >= 3)
                    {
                        lblBestResult2.Text = "Thread 3: It:" + GAs[2].iterationCount.ToString() + " " +
                                              "Fitness:" + GAs[2].bestMember.fitness.ToString() + " MutationRate:" + GAs[2].mutationRate.ToString() +
                                              "\n" + GAs[2].bestMember.ToString();
                        if (numThreads >= 4)
                        {
                            lblBestResult3.Text = "Thread 4: It:" + GAs[3].iterationCount.ToString() + " " +
                                                  "Fitness:" + GAs[3].bestMember.fitness.ToString() + " MutationRate:" + GAs[3].mutationRate.ToString() +
                                                  "\n" + GAs[3].bestMember.ToString();
                        }
                    }

                    int bfitness = 0;
                    if (GAs[0].bestMember.fitness < GAs[1].bestMember.fitness)
                    {
                        bestOne = 0;
                    }
                    else
                    {
                        bestOne = 1;
                    }
                    bfitness = GAs[bestOne].bestMember.fitness;

                    if (numThreads >= 3)
                    {
                        if (GAs[2].bestMember.fitness < bfitness)
                        {
                            bestOne  = 2;
                            bfitness = GAs[bestOne].bestMember.fitness;
                        }
                        if (numThreads >= 4)
                        {
                            if (GAs[3].bestMember.fitness < bfitness)
                            {
                                bestOne  = 3;
                                bfitness = GAs[bestOne].bestMember.fitness;
                            }
                        }
                    }

                    bMap = new Bitmap(bOrigMap);
                    Bitmap b1 = new Bitmap(bOrigMap);
                    MyImageProc.DrawBestTour(b1, ref bMap, PointsList, GAs[bestOne].bestMember.mem);
                    MyImageProc.ResizeImage(bMap, ref bMap,
                                            new Rectangle(0, 0, picMap0.Width, picMap0.Height));
                    picMap0.Image = bMap;
                    // lblBestThread.Text = "Current Best Thread: " + (bestOne + 1).ToString() + "\n" +
                    //     "Current Best Fitness: " + bfitness.ToString();

                    int ThreadMonitored = 1;
                    if (optT1.Checked == true)
                    {
                        ThreadMonitored = 0;
                    }
                    if (optT2.Checked == true)
                    {
                        ThreadMonitored = 1;
                    }
                    if (optT3.Checked == true)
                    {
                        ThreadMonitored = 2;
                    }
                    if (optT4.Checked == true)
                    {
                        ThreadMonitored = 3;
                    }

                    bMap = new Bitmap(bOrigMap);
                    MyImageProc.DrawBestTour(b1, ref bMap, PointsList, GAs[ThreadMonitored].bestMember.mem);
                    MyImageProc.ResizeImage(bMap, ref bMap,
                                            new Rectangle(0, 0, picMap1.Width, picMap1.Height));
                    picMap1.Image           = bMap;
                    lblThreadMonitored.Text = "Thread Monitored: " +
                                              (ThreadMonitored + 1).ToString() + " Fitness: " +
                                              GAs[ThreadMonitored].bestMember.fitness.ToString() +
                                              " MutationRate:" + GAs[ThreadMonitored].mutationRate.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }