Пример #1
0
        private void Step_Animate_Swarm(Color[] pColor, int s)
        {
            double[] xx = new double[((ArrayList)AniS[s]).Count];
            double[] yy = new double[((ArrayList)AniS[s]).Count];
            //plot the current position of particles and their corresponding fitness (objective values)
            for (int i = 0; i < ((ArrayList)AniS[s]).Count; i++)
            {
                xx[i] = ((Particle)((ArrayList)AniS[s])[i]).Position[0];
                yy[i] = ((Particle)((ArrayList)AniS[s])[i]).Objective;
            }
            DrawGraph.CreateXYScatter(Animation, xx, yy, "particle", "Animation_StepS", pColor[3], 8f, ZedGraph.SymbolType.Circle);
            //plot the best found position of particles and their corresponding best objective values
            for (int i = 0; i < ((ArrayList)AniS[s]).Count; i++)
            {
                xx[i] = ((Particle)((ArrayList)AniS[s])[i]).BestP[0];
                yy[i] = ((Particle)((ArrayList)AniS[s])[i]).ObjectiveP;
            }
            DrawGraph.CreateXYScatter(Animation, xx, yy, "particle", "Animation_StepS", Color.Orange, 9f, ZedGraph.SymbolType.Diamond);

            if ((s % 50 == 0) || (s == AniS.Count))
            {
                Animation.GraphPane.XAxis.Scale.MaxAuto = true;
                Animation.GraphPane.XAxis.Scale.MinAuto = true;
                Animation.GraphPane.YAxis.Scale.MaxAuto = true;
                Animation.GraphPane.YAxis.Scale.MinAuto = true;
            }
            else
            {
                Animation.GraphPane.XAxis.Scale.MaxAuto = false;
                Animation.GraphPane.XAxis.Scale.MinAuto = false;
                Animation.GraphPane.YAxis.Scale.MaxAuto = false;
                Animation.GraphPane.YAxis.Scale.MinAuto = false;
            }
        }
Пример #2
0
        private void PSO_dynamic_Click(object sender, EventArgs e)
        {
            int Aspeed = Int16.Parse(this.AniSpeed.Text);

            Istep = Int16.Parse(this.AniStep.Text);
            #region drawGraph of function
            double[] xx;
            double[] yy;
            PSO_Function.GraphPane.CurveList.Clear();
            int density = 2000;
            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]);
            }
            #endregion
            for (int s = Istep; s < AniS.Count; s++)
            {
                Animation.GraphPane.CurveList.Clear();
                DrawGraph.CreateXYCurve(Animation, xx, yy, "F(x)", "Function", Color.Blue);
                Step_Animate(pColor, s);
                Step_Animate_Swarm(pColor, s);
                System.Threading.Thread.Sleep(Aspeed);
            }
        }
Пример #3
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;
        }
Пример #4
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;
        }
Пример #5
0
        private void Back_Click(object sender, EventArgs e)
        {
            Istep--;
            if (Istep < 0)
            {
                MessageBox.Show("This is first step");
                Istep++;
                return;
            }
            Animation.GraphPane.CurveList.Clear();
            #region drawGraph
            double[] xx;
            double[] yy;
            PSO_Function.GraphPane.CurveList.Clear();
            int density = 2000;
            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]);
            }

            #endregion
            DrawGraph.CreateXYCurve(Animation, xx, yy, "particle", "Animation_StepS", Color.Blue);
            #region drawDirection
            xx    = new double[2];
            yy    = new double[2];
            xx[0] = ((Particle)((ArrayList)AniS[Istep])[0]).Position[0];
            yy[0] = ((Particle)((ArrayList)AniS[Istep])[0]).Objective;
            xx[1] = ((Particle)((ArrayList)AniS[Istep])[0]).BestP[0];
            yy[1] = ((Particle)((ArrayList)AniS[Istep])[0]).ObjectiveP;
            DrawGraph.CreateXYCurve(Animation, xx, yy, "direction", "Animation_StepS", Color.Orange);
            xx[1] = ((Particle)Ani[Istep]).BestP[0];
            yy[1] = ((Particle)Ani[Istep]).ObjectiveP;
            DrawGraph.CreateXYCurve(Animation, xx, yy, "direction", "Animation_StepS", Color.Green);
            #endregion
            Step_Animate(pColor, Istep);
            Step_Animate_Swarm(pColor, Istep);
        }
Пример #6
0
 private void Step_Animate(Color[] pColor, int s)
 {
     double[] xx = new double[1];
     double[] yy = new double[1];
     xx[0] = ((Particle)Ani[s]).BestP[0];
     yy[0] = ((Particle)Ani[s]).ObjectiveP;
     DrawGraph.CreateXYScatter(Animation, xx, yy, "Gbest", "Animation_Step" + s.ToString(), Color.Green, 10f, ZedGraph.SymbolType.Triangle);
     //choose whether the graph should be rescale after a number of step
     if ((s % 50 == 0) || (s == AniS.Count))
     {
         Animation.GraphPane.XAxis.Scale.MaxAuto = true;
         Animation.GraphPane.XAxis.Scale.MinAuto = true;
         Animation.GraphPane.YAxis.Scale.MaxAuto = true;
         Animation.GraphPane.YAxis.Scale.MinAuto = true;
     }
     else
     {
         // Animation.GraphPane.XAxis.Scale.MaxAuto = false;
         // Animation.GraphPane.XAxis.Scale.MinAuto = false;
         // Animation.GraphPane.YAxis.Scale.MaxAuto = false;
         // Animation.GraphPane.YAxis.Scale.MinAuto = false;
     }
 }