예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            int    numOfRuns  = (int)numericUpDown1.Value;
            string seriesName = "fitness/Generation";

            chart1.Series.Remove(chart1.Series[0]);
            Random rnd = new Random();

            for (int i = 0; i < numOfRuns; i++)
            {
                chart1.Series.Add(seriesName + "_" + i.ToString());
                chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization
                                             .Charting.SeriesChartType.Line;
                chart1.Series[i].Color = Color.FromArgb(
                    rnd.Next(1, 255),
                    rnd.Next(1, 255),
                    rnd.Next(1, 255));
            }

            timer1.Start();
            record = new Record(numOfRuns);
            EnvironmentVar[] enVars = new EnvironmentVar[numOfRuns];
            App frontApp            = new App();

            frontApp.Use <SUTInitialization>(textBox1.Text, (int)numericUpDown2.Value, record);
            frontApp.Use <TaskDistributor>(enVars, numOfRuns, record, Convert.ToInt16(textBox2.Text));
            frontApp.Use <Monitor>(record, enVars);
            frontApp.GetAppFunc().Invoke(null);
        }
        public async Task <int> Invoke(EnvironmentVar envar)
        {
            enVar = envar;

            await Task.Run(() => GA_Start());

            await _next(enVar);

            return(0);
        }
예제 #3
0
        public async Task <int> Invoke(EnvironmentVar enVar)
        {
            Task monitor = Task.Run(() =>
            {
                while (!_enVars.All(x => x.finishIndicate == true))
                {
                    for (int i = 0; i < _enVars.Length; i++)
                    {
                        if (_enVars[i].continueIndicate == false)
                        {
                            /** update fitRecord table **/
                            int gen    = (int)_enVars[i].genFitRecord[0];
                            double fit = (double)_enVars[i].genFitRecord[1];
                            _record.fitRecord.Rows[gen][i + 1] = fit;

                            /** update SolRecord table **/
                            string strSolution = null;
                            Array.ForEach(_enVars[i].population
                                          .OrderByDescending(x => x.Value)
                                          .First().Key
                                          .GetGenotype(), x =>
                            {
                                int[] tmp = null;
                                x.GetGenValue(ref tmp);
                                string tempStr = "(";
                                foreach (var item in tmp)
                                {
                                    tempStr = tempStr + item.ToString() + " ";
                                }
                                tempStr     = tempStr + ")";
                                strSolution = strSolution + tempStr;
                            });
                            _record.solRecord.Rows[gen][i + 1] = strSolution;

                            _record.currentGen[i]       = gen;
                            _record.updateIndicate[i]   = true;
                            _enVars[i].continueIndicate = true;
                        }
                    }
                }
                ExcelOperation.dataTableListToExcel(
                    new List <DataTable> {
                    _record.fitRecord, _record.solRecord
                },
                    false,
                    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"/out.xlsx"
                    );
            });

            await monitor;

            Console.WriteLine("Mointor Closed.");
            return(0);
        }
예제 #4
0
        public async Task Invoke(EnvironmentVar enVar)
        {
            Task t = Task.Run(() =>
            {
                for (int i = 0; i >= 0; i--)
                {
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine("Starts in: {0}", i);
                }
            });
            await t;

            await _next(enVar);

            Console.WriteLine("All tasks are done");
        }
        public async Task <int> Invoke(EnvironmentVar enVar)
        {
            int k = 0;

            for (int i = 0; i < _enVars.Length; i++)
            {
                _enVars[i] = new EnvironmentVar();
                _enVars[i].initializeEnvironmentVar(i);
                _enVars[i].pmProblem = _record.sutInfo;
                apps[i] = new App();
            }
            Task <int>[] taskList = new Task <int> [_numOfThread];
            _next(null);   // Start Monitor
            _record.warmUpWatch.Stop();
            _record.gaWatch.Start();
            while (k < _numOfTasks)
            {
                if (_numOfTasks - k < _numOfThread)
                {
                    _numOfThread = _numOfTasks - k;
                }
                for (int i = 0; i < _numOfThread; i++)
                {
                    apps[k + i].Use <InitializationMidWare>();
                    apps[k + i].Use <StdGA>();
                    taskList[i] = apps[k + i].GetAppFunc().Invoke(_enVars[k + i]);
                    //taskList[i] = Task.Run(()=>{
                    //    Console.WriteLine("in tasks {0}",Task.CurrentId);
                    //    System.Threading.Thread.Sleep(5000);
                    //    Console.WriteLine("finish task {0}", Task.CurrentId);

                    //});
                }
                //while (!taskList.All(x => x.Result == 1)) ;
                for (int i = 0; i < _numOfThread; i++)
                {
                    await taskList[i];
                }
                k = k + _numOfThread;
            }
            _record.gaWatch.Stop();
            Console.WriteLine("All tasks are done");
            return(0);
        }
        public async Task <int> Invoke(EnvironmentVar enVar)
        {
            Task taskInitialPop = new Task(() =>
            {
                foreach (var genotype in enVar.tempPopulation)
                {
                    for (int i = 0; i < enVar.pmGenotypeLen; i++)
                    {
                        int count = (int)enVar.pmProblem["Dimension"];
                        if (count != 2)
                        {
                            Console.WriteLine("Dimension is not 2, Program should stop!");
                            break;
                        }
                        int[] newGene  = new int[3];
                        int decodeLow  = 0;
                        int decodeHigh = 0;

                        decodeLow = (int)Math.Pow((((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[0].Item1), 2)
                                    + (((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[0].Item1);
                        decodeHigh = (int)Math.Pow((((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[1].Item2), 2)
                                     + (((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[1].Item2);
                        int lowBound  = enVar.rnd.Next(decodeLow, decodeHigh);
                        int highBound = enVar.rnd.Next(lowBound, decodeHigh);

                        newGene[0] = lowBound;
                        newGene[1] = highBound;
                        newGene[2] = enVar.rnd.Next(1, (int)enVar.pmProblem["Weight"]);
                        genotype.Key.ModifyGenValue(i, newGene);
                    }
                }
            });

            taskInitialPop.Start();
            await taskInitialPop;

            Console.WriteLine("Next GA");
            await _next(enVar);

            Console.WriteLine("Task finish {0}", Task.CurrentId);
            return(1);
        }
예제 #7
0
        public Task <int> InvokeNext(EnvironmentVar env)
        {
            try
            {
                if (_queue.Count == 0)
                {
                    env.emptyQueue = true;

                    return(Task.Run <int>(() =>
                    {
                        return 0;
                    }));
                }
                var        Obj          = _queue.Dequeue();
                MethodInfo InvokeMethod = Obj.GetType().GetMethod("Invoke");
                return((Task <int>)InvokeMethod.Invoke(Obj, new object[] { env }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
        public async Task <int> Invoke(EnvironmentVar enVar)
        {
            Dictionary <string, object> sutParam = SUT.SelectSUT(_selectSUT);

            sutParam["SUTPath"] = _sutPath;
            _record.sutInfo     = sutParam;
            Task taskInitialSUT = Task.Run(() => {
                LocalFileAccess lfa = new LocalFileAccess();
                List <string> list  = new List <string>();
                lfa.StoreLinesToList(_sutPath.ToString(), list);
                DataTable dt = new DataTable();
                dt.Columns.Add("Input", Type.GetType("System.String"));
                for (int i = 0; i < (int)sutParam["NumOfCE"]; i++)
                {
                    dt.Columns.Add((i + 1).ToString(), Type.GetType("System.String"));
                }

                foreach (string s in list)
                {
                    int length = dt.Rows.Count;

                    if (length == 0)
                    {
                        int c             = Convert.ToInt32(s.Substring(s.IndexOf(' ', s.IndexOf(' ') + 1))) + 1;
                        object[] coverSeq = new object[(int)sutParam["NumOfCE"] + 1];
                        for (int i = 0; i < coverSeq.Length; i++)
                        {
                            coverSeq[i] = 0.0;
                        }
                        coverSeq[c] = 1.0;
                        coverSeq[0] = s.Substring(0, s.IndexOf(' ', s.IndexOf(' ') + 1));
                        dt.Rows.Add(
                            coverSeq
                            );
                    }
                    else if (s.Substring(0, s.IndexOf(' ', s.IndexOf(' ') + 1))
                             == dt.Rows[length - 1].ItemArray[0].ToString())
                    {
                        int c = Convert.ToInt32(s.Substring(s.IndexOf(' ', s.IndexOf(' ') + 1) + 1)) + 1;
                        dt.Rows[length - 1][c] = 1.0;
                    }
                    else
                    {
                        int c             = Convert.ToInt32(s.Substring(s.IndexOf(' ', s.IndexOf(' ') + 1))) + 1;
                        object[] coverSeq = new object[(int)sutParam["NumOfCE"] + 1];
                        for (int i = 0; i < coverSeq.Length; i++)
                        {
                            coverSeq[i] = 0.0;
                        }
                        coverSeq[c] = 1.0;
                        coverSeq[0] = s.Substring(0, s.IndexOf(' ', s.IndexOf(' ') + 1));
                        dt.Rows.Add(
                            coverSeq
                            );
                    }
                }
                sutParam["Map"] = dt.AsEnumerable()
                                  .ToDictionary <DataRow, string, double[]>(
                    row => row.Field <string>(0),
                    row =>
                {
                    double[] coverSeq = new double[(int)sutParam["NumOfCE"]];
                    for (int i = 0; i < (int)sutParam["NumOfCE"]; i++)
                    {
                        coverSeq[i] = Convert.ToDouble(row.ItemArray[i + 1]);
                    }
                    return(coverSeq);
                });
            });

            await taskInitialSUT;

            await _next(enVar);

            Console.WriteLine("All tasks are done");
            return(0);
        }