///<summary> /// Breed the next generation of networks from the current best and overall best. ///</summary> protected virtual void BreedNextGeneration() { int k = 0; int l = 0; for (int i = 0; i < population; i++) { if (i < population * breedingRepartitionCoef) // all time generation { hostPopulation[i].GetComponent <Controller>().NeuralNet = Evolution.CreateMutatedOffspring( alltimeFittestNets[k], l + 1, hiddenLayerNbMutation, hiddenLayerNbMutationRate, hiddenNbMutation, hiddenLayerNbMutationRate, synapsesMutationRate, synapsesMutationRange); // l+1 as mutate coef to make each version more propable to mutate a lot l += 1; if (l > population * breedingRepartitionCoef / breedingSampleNb) { l = 0; k += 1; } } else if (i < population * (1 - freshBloodProportion)) // this generation { hostPopulation[i].GetComponent <Controller>().NeuralNet = Evolution.CreateMutatedOffspring( generationFittestNets[k], l + 1, hiddenLayerNbMutation, hiddenLayerNbMutationRate, hiddenNbMutation, hiddenLayerNbMutationRate, synapsesMutationRate, synapsesMutationRange); l += 1; if (l > population * (1 - freshBloodProportion) / breedingSampleNb) { l = 0; k += 1; } } else // fresh blood (10%) { hostPopulation[i].GetComponent <Controller>().NeuralNet = new Network(baseLayersSizes); } if (k >= breedingSampleNb) { k = 0; } } }
/// <summary> /// Sorts the current generation of networks by fitness. /// </summary> protected virtual void SortCurrentGeneration() { if (generationFittestNets[generationFittestNets.Length - 1] != null) { for (int i = 0; i < generationFittestNets.Length; i++) { generationFittestNets[i].FitnessScore = 0; } } for (int i = 0; i < hostPopulation.Length; i++) { layered.Network fitnessContender = hostPopulation[i].GetComponent <Controller>().NeuralNet; Evolution.RankFitnessContender(alltimeFittestNets, fitnessContender.GetClone()); Evolution.RankFitnessContender(generationFittestNets, fitnessContender.GetClone()); } }