Exemplo n.º 1
0
        public async void Reproduction(int[] token)
        {
            int maxNumOfTasks = 20;
            int k             = 0;

            List <Task> lTasks = new List <Task>();

            while (tempPool.Count < popSize)
            {
                lTasks.Add(Task.Run(() =>
                {
                    GlobalVar.mutex_K.WaitOne();
                    int index = k;
                    k         = k + 1;
                    GlobalVar.mutex_K.ReleaseMutex();
                    GAEncoding child   = Copy.DeepCopy(cePool.ElementAt(index).Key);
                    int[] selectedList = DifferentialSelection(index);
                    child = child.DifferentialCrossover(cePool, selectedList);

                    child.FitnessCal(aMatrix);
                    if (child.entropy > cePool.ElementAt(index).Key.entropy
                        /*&& child.diversity > cePool.ElementAt(index).Key.diversity*/)
                    {
                        GlobalVar.mutex_K.WaitOne();
                        tempPool.Add(child, new double[] { child.entropy, -1 });
                        GlobalVar.mutex_K.ReleaseMutex();
                    }
                    else
                    {
                        GlobalVar.mutex_K.WaitOne();
                        tempPool.Add(cePool.ElementAt(index).Key,
                                     new double[] { cePool.ElementAt(index).Key.entropy, -1 });
                        GlobalVar.mutex_K.ReleaseMutex();
                    }
                }));

                if (lTasks.Count == maxNumOfTasks)
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                    int remainTasks = cePool.Count - k;
                    if (remainTasks <= maxNumOfTasks)
                    {
                        maxNumOfTasks = remainTasks;
                    }
                }
            }

            token[0] = 1;
        }
Exemplo n.º 2
0
        public async void GA_FitnessEvaluation(int[] token)
        {
            List <Task> lTasks = new List <Task>();

            // Test inputs Generation
            // Fitness Evaluation
            for (int k = 0; k < tempPool.Count;)
            {
                // Single Thread
                //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds);

                lTasks.Add(Task.Run(() =>
                {
                    GlobalVar.mutex_K.WaitOne();
                    // Console.WriteLine(k);
                    GAEncoding ga = tempPool.ElementAt(k).Key;
                    int id        = k;
                    k             = k + 1;
                    GlobalVar.mutex_K.ReleaseMutex();
                    //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds);
                    //ga.TrueFitnessCal(numOfLabel, labelMatrix);
                    ga.FitnessCal(aMatrix);
                    //Console.WriteLine("{0} job finish",id);
                }));
                if (lTasks.Count == 20 || (tempPool.Count - k - 1 < 20))  //8,8
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                }
            }
            token[0] = 1;
            //Console.WriteLine("Fitness Evaluation Done");
        }