private void ThreadWorker(int i, int offspringIndex, int matingPopulationSize) { IGenome mother = Population.Genomes[i]; var fatherInt = (int)(ThreadSafeRandom.NextDouble() * matingPopulationSize); IGenome father = Population.Genomes[fatherInt]; IGenome child1 = Population.Genomes[offspringIndex + i * 2]; IGenome child2 = Population.Genomes[offspringIndex + i * 2 + 1]; var worker = new MateWorker(mother, father, child1, child2); worker.Run(); }
/// <summary> /// Modify the weight matrix and bias values based on the last call to /// calcError. /// </summary> public override sealed void Iteration() { if (_first) { Population.Claim(this); _first = false; } var countToMate = (int) (Population.PopulationSize*PercentToMate); int offspringCount = countToMate*2; int offspringIndex = Population.PopulationSize - offspringCount; var matingPopulationSize = (int) (Population.PopulationSize*MatingPopulation); TaskGroup group = EngineConcurrency.Instance .CreateTaskGroup(); // mate and form the next generation for (int i = 0; i < countToMate; i++) { IGenome mother = Population.Genomes[i]; var fatherInt = (int)(ThreadSafeRandom.NextDouble()*matingPopulationSize); IGenome father = Population.Genomes[fatherInt]; IGenome child1 = Population.Genomes[offspringIndex]; IGenome child2 = Population.Genomes[offspringIndex + 1]; var worker = new MateWorker(mother, father, child1, child2); if (MultiThreaded) { EngineConcurrency.Instance.ProcessTask(worker, group); } else { worker.Run(); } offspringIndex += 2; } if (MultiThreaded) { group.WaitForComplete(); } // sort the next generation Population.Sort(); }
/// <summary> /// Modify the weight matrix and bias values based on the last call to /// calcError. /// </summary> public override sealed void Iteration() { if (_first) { Population.Claim(this); _first = false; } var countToMate = (int) (Population.PopulationSize*PercentToMate); int offspringCount = countToMate*2; int offspringIndex = Population.PopulationSize - offspringCount; var matingPopulationSize = (int) (Population.PopulationSize*MatingPopulation); // mate and form the next generation Parallel.For(0, countToMate, i => { IGenome mother = Population.Genomes[i]; var fatherInt = (int)(ThreadSafeRandom.NextDouble() * matingPopulationSize); IGenome father = Population.Genomes[fatherInt]; IGenome child1 = Population.Genomes[offspringIndex]; IGenome child2 = Population.Genomes[offspringIndex + 1]; var worker = new MateWorker(mother, father, child1, child2); worker.Run(); offspringIndex += 2; }); // sort the next generation Population.Sort(); }