예제 #1
0
        private void Run_Click(object sender, EventArgs e)
        {
            double ObjectiveValue;

            double[] index;
            double[] Avg;
            double[] PSOparas = new double[9];
            fx          = Convert.ToInt32(this.fxindex.Value); //choose objective function to be minimized
            PSOparas[0] = Convert.ToDouble(this.PSOiter.Value);
            PSOparas[1] = Convert.ToDouble(this.PSOnumParticles.Value);
            PSOparas[2] = Convert.ToDouble(this.PSOwmin.Text);
            PSOparas[3] = Convert.ToDouble(this.PSOwmax.Text);
            PSOparas[4] = Convert.ToDouble(this.PSOnb.Value);
            PSOparas[5] = Convert.ToDouble(this.PSOcp.Text);
            PSOparas[6] = Convert.ToDouble(this.PSOcg.Text);
            PSOparas[7] = Convert.ToDouble(this.PSOcl.Text);
            PSOparas[8] = Convert.ToDouble(this.PSOcn.Text);
            DateTime start = DateTime.Now;

            AniS = new ArrayList();
            //call PSO algorithm to minimize f(x)
            MainClass.PSO(fx, PSOparas, out ObjectiveValue, out Avg, out index, out Ani, out AniS, out GBest);

            TimeSpan finish = DateTime.Now - start;

            PSOGraph.GraphPane.CurveList.Clear();
            DrawGraph.CreateXY(PSOGraph, index, Avg);
            int density = 2000; //number of points used to construct the outline of a function within the pre-defined range

            #region drawGraph_function_outline
            double[] xx;
            double[] yy;
            PSO_Function.GraphPane.CurveList.Clear();
            xx    = new double[1];
            yy    = new double[1];
            xx[0] = GBest.BestP[0];
            yy[0] = GBest.ObjectiveP;
            DrawGraph.CreateXYScatter(PSO_Function, xx, yy, "F(x)", "Best Solution", Color.Red, 10f, ZedGraph.SymbolType.Triangle);
            xx = new double[density];
            yy = new double[density];
            for (int i = 0; i < density; i++)
            {
                xx[i] = ((Particle)((ArrayList)AniS[0])[0]).PosMin[0] + i * (((Particle)((ArrayList)AniS[0])[0]).PosMax[0] - ((Particle)((ArrayList)AniS[0])[0]).PosMin[0]) / density;
                yy[i] = Function.Test_Function(fx, xx[i]);
            }
            DrawGraph.CreateXYCurve(PSO_Function, xx, yy, "F(x)", "Function", Color.Blue);
            #endregion
            ObjVal.Text = finish.ToString();
            // Enable animation
            this.AniStep.Enabled     = true;
            this.AniStepRun.Enabled  = true;
            this.PSO_dynamic.Enabled = true;
            this.AniSpeed.Enabled    = true;
        }
예제 #2
0
        private void btnRunPSO_Click(object sender, EventArgs e)
        {
            double ObjectiveValue;

            double[] index;
            double[] Avg;
            double[] PSOparas = new double[9];
            fx = Convert.ToInt32(this.fxindex.Value); //determine the function to be minimized
            //set PSO parameters
            PSOparas[0] = Convert.ToDouble(this.PSOiter.Value);
            PSOparas[1] = Convert.ToDouble(this.PSOnumParticles.Value);
            PSOparas[2] = Convert.ToDouble(this.PSOwmin.Text);
            PSOparas[3] = Convert.ToDouble(this.PSOwmax.Text);
            PSOparas[4] = Convert.ToDouble(this.PSOnb.Value);
            PSOparas[5] = Convert.ToDouble(this.PSOcp.Text);
            PSOparas[6] = Convert.ToDouble(this.PSOcg.Text);
            PSOparas[7] = Convert.ToDouble(this.PSOcl.Text);
            PSOparas[8] = Convert.ToDouble(this.PSOcn.Text);

            DateTime start = DateTime.Now;

            //call PSO algorithm to minimize function fx
            MainClass.PSO(fx, PSOparas, out ObjectiveValue, out Avg, out index, out Ani, out AniS, out GBest);
            TimeSpan finish = DateTime.Now - start;

            //Clear the current graph
            PSOGraph.GraphPane.CurveList.Clear();
            //Draw average performance via the search
            DrawGraph.CreateXY(PSOGraph, index, Avg);

            #region drawGraph_function_outline
            //global best position
            pts       = new Point3[1, 1];
            pts[0, 0] = new Point3((float)GBest.BestP[0], (float)GBest.BestP[1], (float)GBest.ObjectiveP, 1);
            //draw graph of function fx with global best position
            Draw3DFunction(this.chart3D1, 0, fx, density, GBest.PosMin[0], GBest.PosMax[0], GBest.PosMin[1], GBest.PosMax[1], pts, pt1);
            #endregion

            ComTime.Text = finish.ToString();

            // Enable animation function
            this.AniStep.Enabled     = true;
            this.AniStepRun.Enabled  = true;
            this.PSO_dynamic.Enabled = true;
            this.AniSpeed.Enabled    = true;
            this.btnEDown.Enabled    = true;
            this.btnEUp.Enabled      = true;
            this.btnRleft.Enabled    = true;
            this.btnRright.Enabled   = true;
        }