Esempio n. 1
0
        private void btn_himmelblau_parallel_Click(object sender, EventArgs e)
        {
            int numSwarmsToLaunch = 10;
            Task<SwarmResult>[] taskArr = new Task<SwarmResult>[numSwarmsToLaunch];
            for (int i = 0; i < taskArr.Length; i++)
            {
                taskArr[i] = Task.Factory.StartNew<SwarmResult>(
                    (obj) =>
                    {
                        SwarmSystem ss = new SwarmSystem((int)obj);
                        ss.Intialize();
                        SwarmResult sr = ss.doPSO("Himmelblau");
                        return sr;
                    }, i);
            }

            List<SwarmResult> rList = new List<SwarmResult>();
            Task tskFinal = Task.Factory.ContinueWhenAll(taskArr,
                (tsks) =>
                {
                    Console.Write(tsks.Length.ToString() + " tasks");
                    for (int i = 0; i < tsks.Length; i++)
                        rList.Add(tsks[i].Result);
                }
                );
            tskFinal.Wait();
            rList.Sort();
            dataGridView1.DataSource = rList;
            dataGridView1.Refresh();
            label1.Text = rList[0].ToString();
        }
Esempio n. 2
0
 private void btn_seq_swarm_Click(object sender, EventArgs e)
 {
     SwarmSystem ss = new SwarmSystem(0);
     ss.Intialize();
     SwarmResult sr = ss.doPSO("Rosenbrock");
     label1.Text = sr.ToString();
 }
Esempio n. 3
0
        public SwarmResult MDoPS0( )
        {
            // Particle movement to achieve for particle swarm optimization
            int         kiI;
            SwarmResult koSR;

            this.VdGx = this.VoPList[0].VdXx;
            this.VdGy = this.VoPList[0].VdXy;

            for (kiI = 0; kiI < 1000; kiI++)
            {
                this.VdPx = this.VoPList[0].VdXx;
                this.VdPy = this.VoPList[0].VdXy;

                foreach (Particle koPt in this.VoPList)
                {
                    if (Math.Abs(this.MRosenbrock(koPt.VdXx, koPt.VdXy)) <
                        Math.Abs(this.MRosenbrock(this.VdPx, this.VdPy)))
                    {
                        this.VdPx = koPt.VdXx;
                        this.VdPy = koPt.VdXy;
                    }
                }
                if (Math.Abs(this.MRosenbrock(this.VdPx, this.VdPy)) <
                    Math.Abs(this.MRosenbrock(this.VdGx, this.VdGy)))
                {
                    this.VdGx = this.VdPx;
                    this.VdGy = this.VdPy;
                }
                foreach (Particle koPt in this.VoPList)
                {
                    koPt.MUpdateVelocity(this.VdPx, this.VdPy, this.VdGx, this.VdGy);
                    koPt.MUpdatePosition( );
                }
            }

            koSR = new SwarmResult
            {
                ViID    = this.viNum,
                VdX     = this.VdGx,
                VdY     = this.VdGy,
                VdValue = this.MRosenbrock(this.VdGx, this.VdGy)
            };

            return(koSR);
        }
Esempio n. 4
0
        public SwarmResult MDoHimmelblau( )
        {
            int         kiI;
            SwarmResult koSR;

            this.VdGx = this.VoPList[0].VdXx;
            this.VdGy = this.VoPList[0].VdXy;

            for (kiI = 0; kiI < 2000; kiI++)
            {
                this.VdPx = this.VoPList[0].VdXx;
                this.VdPy = this.VoPList[0].VdXy;

                foreach (Particle koPt in this.VoPList)
                {
                    if (Math.Abs(this.MHimmelblau(koPt.VdXx, koPt.VdXy)) <
                        Math.Abs(this.MHimmelblau(this.VdPx, this.VdPy)))
                    {
                        this.VdPx = koPt.VdXx;
                        this.VdPy = koPt.VdXy;
                    }
                }
                if (Math.Abs(this.MHimmelblau(this.VdPx, this.VdPy)) <
                    Math.Abs(this.MHimmelblau(this.VdGx, this.VdGy)))
                {
                    this.VdGx = this.VdPx;
                    this.VdGy = this.VdPy;
                }
                foreach (Particle koPt in this.VoPList)
                {
                    koPt.MUpdateVelocity(this.VdPx, this.VdPy, this.VdGx, this.VdGy);
                    koPt.MUpdatePosition( );
                }
            }

            koSR = new SwarmResult
            {
                ViID    = this.viNum,
                VdX     = this.VdGx,
                VdY     = this.VdGy,
                VdValue = this.MHimmelblau(this.VdGx, this.VdGy)
            };

            return(koSR);
        }
        public SwarmResult doPSO(string type)  //Particle movement to achieve Particle Swarm Optimisation
        {
            Gx = pList[0].Xx;
            Gy = pList[0].Xy;
            for (int i = 0; i < 1000; i++)
            {
                //find best position in swarm
                Px = pList[0].Xx;
                Py = pList[0].Xy;
                foreach (Particle par in pList)
                {
                    if (Math.Abs(functionToSolve(par.Xx, par.Xy, type)) < Math.Abs(functionToSolve(Px, Py, type)))
                    {
                        Px = par.Xx;
                        Py = par.Xy;
                    }
                }

                if (Math.Abs(functionToSolve(Px, Py, type)) < Math.Abs(functionToSolve(Gx, Gy, type)))
                {
                    Gx = Px;
                    Gy = Py;
                }

                foreach (Particle par in pList)
                {
                    par.updateVelocity(Px, Py, Gx, Gy);
                    par.updatePosition();
                }
            }

            SwarmResult sres = new SwarmResult
            {
                SwarmId       = swarmNum,
                X             = Gx,
                Y             = Gy,
                FunctionValue = functionToSolve(Gx, Gy, type)
            };

            return(sres);
        }