2

特定の座標で 4 つのオブジェクトを並行して移動するには、4 つのバックグラウンド ワーカーを使用する必要があります。つまり、それらを一緒に開始し、一緒に停止します。

50 回ループするループを作成し、ワーカーを開始する必要があるたびに、Do_Work()メソッドを停止して次の反復で再開するようにワーカーが作業を完了した後、ワーカーを呼び出す次のメソッドを作成しました。

public void Genetic_Algorithm(List<int[,]> population)
        {


            DateTime startT = DateTime.Now.Date;

            double[,] FitnessValue = new double[6, 2]; // for all five chromosome we store two Values the Fitness Value and the Fitness Ratio

            int[] RouletteWheel = new int[6];

            int round = 0;



            for (geneticIteration = 0; geneticIteration < 50; geneticIteration++)
            {



                round = geneticIteration + 1;


                // Calculate the fitness Function and the Fitness Ratio

                FitnessFunction(population); // Fitness Function


            }



            MessageBox.Show("Press Again");


        } 




 public void FitnessFunction(List<int[,]> population)
        {

            extractPath(population, geneticIteration);

            auv0Genetic.RunWorkerAsync(); // start obj # 1
            auv1Genetic.RunWorkerAsync(); // start obj # 2
            auv2Genetic.RunWorkerAsync(); // start obj # 3
            auv3Genetic.RunWorkerAsync(); // start obj # 4


        }

Do_Work()4 つのバックグラウンド ワーカー用の 4つのメソッドがあります。以下はその 1 つです。

private void auv0Genetic_DoWork(object sender, DoWorkEventArgs e)
        {

            List<PointF> genetic2DLayerPath1 = new List<PointF>(); //  from chromosome 1

            List<PointF> genetic2DLayerPath2 = new List<PointF>(); //  from chromosome 2

            List<PointF> genetic2DLayerPath3 = new List<PointF>(); //  from chromosome 3

            List<PointF> genetic2DLayerPath4 = new List<PointF>(); //  from chromosome 4

            List<PointF> genetic2DLayerPath5 = new List<PointF>(); //  from chromosome 5

            List<PointF> genetic2DLayerPath6 = new List<PointF>(); //  from chromosome 6

            countNumOfPaths = 0;

            float[] xPoints = new float[1];

            float[] yPoints = new float[1]; 


            foreach (int[,] arr in pathChromosom1)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath1.Add(pointIn2D);

            }


            foreach (int[,] arr in pathChromosom2)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath2.Add(pointIn2D);

            }


            foreach (int[,] arr in pathChromosom3)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath3.Add(pointIn2D);

            }


            foreach (int[,] arr in pathChromosom4)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath4.Add(pointIn2D);

            }



            foreach (int[,] arr in pathChromosom5)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath5.Add(pointIn2D);

            }

            foreach (int[,] arr in pathChromosom6)
            {

                Point3D pointIn3D = new Point3D(cellsCenters[0, arr[0, 0]], cellsCenters[1, arr[1, 0]], 700);

                PointF pointIn2D = Project(pointIn3D); // convert to 2D

                genetic2DLayerPath6.Add(pointIn2D);

            }

            int counter = 0;

            for (int i = 0; i < 6; i++)
            {


                if (i == 0) // first chromosome
                {
                     xPoints = new float[genetic2DLayerPath1.Count()];

                     yPoints = new float[genetic2DLayerPath1.Count()];



                    auv[0].auvDepth = 700;


                    foreach(PointF p in genetic2DLayerPath1)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }

                    counter = 0;

                }


                if (i == 1) // second chromosome
                {
                     xPoints = new float[genetic2DLayerPath2.Count()];

                     yPoints = new float[genetic2DLayerPath2.Count()];



                    auv[0].auvDepth = 700;


                    foreach (PointF p in genetic2DLayerPath2)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }

                    counter = 0;

                }


                if (i == 2) // third chromosome
                {
                     xPoints = new float[genetic2DLayerPath3.Count()];

                     yPoints = new float[genetic2DLayerPath3.Count()];



                    auv[0].auvDepth = 700;


                    foreach (PointF p in genetic2DLayerPath3)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }


                    counter = 0;

                }

                if (i == 3) // fourth chromosome
                {
                     xPoints = new float[genetic2DLayerPath4.Count()];

                     yPoints = new float[genetic2DLayerPath4.Count()];



                    auv[0].auvDepth = 700;


                    foreach (PointF p in genetic2DLayerPath4)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }

                    counter = 0;

                }

                if (i == 4) // fifth chromosome
                {
                     xPoints = new float[genetic2DLayerPath5.Count()];

                     yPoints = new float[genetic2DLayerPath5.Count()];



                    auv[0].auvDepth = 700;


                    foreach (PointF p in genetic2DLayerPath5)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }

                    counter = 0;

                }


                if (i == 5) // sixth chromosome
                {
                    xPoints = new float[genetic2DLayerPath6.Count()];

                    yPoints = new float[genetic2DLayerPath6.Count()];



                    auv[0].auvDepth = 700;


                    foreach (PointF p in genetic2DLayerPath6)
                    {

                        xPoints[counter] = p.X;

                        yPoints[counter] = p.Y;

                        counter++;

                    }

                    counter = 0;

                }

                counter = 0;

                while (countNumOfPaths != 2)
                {


                    Thread.Sleep(900); // assume that it represents the speed of the AUV which is in our case = 3 m/s as each meter equal to 300 seconds in thread.sleep()  

                    if (auv0Genetic.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }


                    if (forward)
                    {


                        if (counter == xPoints.Length - 1)
                        {

                            backward = true;

                            forward = false;

                            countNumOfPaths++;


                        }


                        else
                        {

                            auv[0].auvX = xPoints[counter];

                            auv[0].auvY = yPoints[counter];

                            counter++;

                        }


                    }

                    if (backward)
                    {

                        if (counter == 0)
                        {

                            backward = false;

                            forward = true;

                            countNumOfPaths++;

                        }

                        else
                        {

                            auv[0].auvX = xPoints[counter];


                            auv[0].auvY = yPoints[counter];


                            counter--;
                        }

                    }



                    //////////////////////// Draw ///////////////////////////

                    iSetupDisplay = 0;

                    if (iSetupDisplay != -1)
                    {
                        iSetupDisplay += 10;
                        if (iSetupDisplay >= topology.Width)
                            iSetupDisplay = -1;
                        topology.Refresh();
                    }


                    /////////////////////////////////////////////////////////

                }



            }

        }

問題は、ワーカーが 1 回しか実行されず、次のエラーで実行が停止したことです。

この BackgroundWorker は現在ビジーで、複数のタスクを同時に実行できません。

ここに画像の説明を入力

注: 毎回新しいバックグラウンド ワーカーを作成しようとしましたが、正しく機能せず、50x4 バックグラウンド ワーカーを宣言したため、実行が応答しなくなりました!!.

バックグラウンド ワーカーは次のように登録されます。

private System.ComponentModel.BackgroundWorker auv0Genetic;
private System.ComponentModel.BackgroundWorker auv1Genetic;
private System.ComponentModel.BackgroundWorker auv2Genetic;
private System.ComponentModel.BackgroundWorker auv3Genetic;



this.auv0Genetic = new System.ComponentModel.BackgroundWorker();
this.auv1Genetic = new System.ComponentModel.BackgroundWorker();
this.auv2Genetic = new System.ComponentModel.BackgroundWorker();
this.auv3Genetic = new System.ComponentModel.BackgroundWorker();
4

2 に答える 2