void Evolve(ref List<double> fitnesses, bool useElites) { //here we're going to cross the children and return //spin wheel to get who survives try { GA_Threads.Clear(); List<List<int>> cutPop = new List<List<int>>(Population.Count / GAThreadcount); List<double> cutFitnesses = new List<double>(fitnesses.Count / GAThreadcount); int step = Population.Count / GAThreadcount; for (int i = 0; i < GAThreadcount; i++) { GADoneEvents[i] = new ManualResetEvent(false); cutPop = new List<List<int>>(Population.GetRange(i * step, step)); cutFitnesses = new List<double>(fitnesses.GetRange(i * step, step)); GAThread gathr = new GAThread(GADoneEvents[i], cutPop, cutFitnesses, cross, mutate, spin, Eval); GA_Threads.Add(gathr); ThreadPool.QueueUserWorkItem(gathr.ThreadPoolCallback, i); } // Wait for all threads in pool to finish. foreach (var e in GADoneEvents) e.WaitOne(); Population.Clear(); GA_Threads.ForEach((GAThread ga) => { Population.AddRange(ga.Result); }); } catch (Exception e) { string message = e.Message; m_stop = true; } //List<int> chosen = new List<int>(); //List<List<int>> children = new List<List<int>>(); //List<List<int>> elites = new List<List<int>>(); //List<int> child1, child2; //child1 = new List<int>(); //child2 = new List<int>(); //int i; //for (i = 0; i < Population.Count; i++) // chosen.Add(Spin(ref fitnesses)); //for (i = 1; i < Population.Count; i += 2) //{ // Cross(chosen[i - 1], chosen[i], ref child1, ref child2); // children.Add(new List<int>(child1)); // children.Add(new List<int>(child2)); //} //Population.Clear(); //children.ForEach((List<int> chromos) => // { // //Mutate(ref chromos); // Population.Add(new List<int>(chromos)); // }); //for (i = 0; i < Population.Count; i++) // children.Add(new List<int>(Population[i])); //copy(&children[i].front(), &m_pop[i].front()); //if (useElites) //{ // elites = GetElite(m_pop, fitnesses); // for (i = 0; i < elites.size(); i++) // { // if (IsBinaryGA()) // mutate(&elites[i].front()); // else // swap(&elites[i].front()); // copy(&elites[i].front(), &m_pop[i].front()); // } //} }
void Evolve(ref List<double> fitnesses, bool useElites) { //here we're going to cross the children and return //spin wheel to get who survives //try //{ GA_Threads.Clear(); List<List<int>> cutPop = new List<List<int>>(Population.Count / GAThreadcount); List<double> cutFitnesses = new List<double>(fitnesses.Count / GAThreadcount); int step = Population.Count / GAThreadcount; for (int i = 0; i < GAThreadcount; i++) { GADoneEvents[i] = new ManualResetEvent(false); cutPop = new List<List<int>>(Population.GetRange(i * step, step)); cutFitnesses = new List<double>(fitnesses.GetRange(i * step, step)); GAThread gathr = new GAThread(GADoneEvents[i], cutPop, cutFitnesses, cross, mutate, spin, Eval); GA_Threads.Add(gathr); ThreadPool.QueueUserWorkItem(gathr.ThreadPoolCallback, i); } // Wait for all threads in pool to finish. foreach (var e in GADoneEvents) e.WaitOne(); Population.Clear(); GA_Threads.ForEach((GAThread ga) => { Population.AddRange(ga.Result); }); //} //catch (Exception e) //{ // string message = e.Message; // m_stop = true; //} }