Exemple #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            double[]    lb            = new double[] { -100, -100 };
            double[]    ub            = new double[] { 100, 100 };
            PSOSettings optimSettings = new PSOSettings();

            PSO    myPSO = new PSO(optimSettings, 2, G, lb, ub);
            double best  = myPSO.Optimize();

            Bitmap bmp = new Bitmap(750, 750);



            for (int numIters = 0; numIters < 1000; numIters++)
            {
                for (int i = 0; i < 750; i++)
                {
                    for (int j = 0; j < 750; j++)
                    {
                        bmp.SetPixel(i, j, Color.White);
                    }
                }


                for (int j = 0; j < 20; j++)
                {
                    int x = ScaleDim(myPSO.particleHistory[numIters][j][0], lb[0], ub[0], 750);
                    int y = ScaleDim(myPSO.particleHistory[numIters][j][1], lb[1], ub[1], 750);

                    if (x > 2 && x < 748 && y > 2 && y < 748)
                    {
                        for (int q = x - 2; q < x + 2; q++)
                        {
                            for (int r = y - 2; r < y + 2; r++)
                            {
                                bmp.SetPixel(q, r, Color.Red);
                            }
                        }
                    }
                    else
                    {
                        bmp.SetPixel(x, y, Color.Red);
                    }
                }

                //bmp.Save("C:\\Users\\fista\\Documents\\pso\\" + numIters.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

                pictureBox1.Image = bmp;
                pictureBox1.Invalidate();
                pictureBox1.Refresh();
                textBox1.Text = numIters.ToString();
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            OptimTests myGoal = new StyblinskiTang(2);// (4, 100);

            double[] lb = new double[] { -100, -100, -100, -100, -100 };
            double[] ub = new double[] { 100, 100, 100, 100, 100 };

            PSOSettings optimSettings = new PSOSettings();
            PSO         myPSO         = new PSO(optimSettings, myGoal.numDims, myGoal.goalFunc, myGoal.lbounds, myGoal.ubounds);
            double      best          = myPSO.Optimize();

            Console.WriteLine("Best: " + best);
            Console.ReadLine();

            for (int i = 0; i < myPSO.gBestHistory.Count; i++)
            {
                Console.WriteLine(i + " " + myPSO.gBestHistory[i]);
            }
            Console.ReadLine();
        }