Exemplo n.º 1
0
        private void btnParallelSwarms_Click(object sender, EventArgs e)
        {
            int launchSwams = 10;

            // An Array of List
            Task <SwarmResult>[] arrTask = new Task <SwarmResult> [launchSwams];

            // for loop
            for (int i = 0; i < arrTask.Length; i++)
            {
                arrTask[i] = Task.Factory.StartNew <SwarmResult>((obj) =>
                {
                    SwarmSystem ss = new SwarmSystem((int)obj);
                    ss.Initialize();
                    SwarmResult sr = ss.DoPSO();
                    return(sr);
                }, i);
            }
            List <SwarmResult> resultList = new List <SwarmResult>();

            Task finalTask = Task.Factory.ContinueWhenAll(arrTask, (tks) =>
            {
                Console.WriteLine(tks.Length.ToString() + " tasks");
                for (int i = 0; i < tks.Length; i++)
                {
                    resultList.Add(tks[i].Result);
                }
            });

            finalTask.Wait();
            resultList.Sort();
            dataGridView1.DataSource = resultList;
            dataGridView1.Refresh();
            lblResult.Text = resultList[0].ToString();
        }
Exemplo n.º 2
0
        private void btnSolveBySwarm_Click(object sender, EventArgs e)
        {
            SwarmSystem ss = new SwarmSystem(0);

            ss.Initialize();
            SwarmResult sr = ss.DoPSO();

            //txtGValue.Text = ss.funcValue.ToString();
            lblResult.Text = sr.ToString();
            //double res = ss.FunctionValue(-1.14);
            //MessageBox.Show(res.ToString());
        }