public T GetChildren <T>(double mutationRate, int amount) where T : GeneralizedCreature, new() { if (CreatureBrain == null) { return(default(T)); } if (!CreatureBrain.IsMeshed) { return(default(T)); } DNA _DNAChild = DNA.CrossOver(CreatureBrain.DNA, CreatureBrain.DNA); _DNAChild.Mutate(mutationRate, amount); T _NewChild = new T() { CreatureBrain = new Brain( CreatureBrain.Layers[0].NeuroneCount, CreatureBrain.Layers[1].NeuroneCount, CreatureBrain.Layers[2].NeuroneCount, CreatureBrain.Layers[3].NeuroneCount, _DNAChild) }; _NewChild.CreatureBrain.Mesh(); return((T)_NewChild); }
private void CreatenextGenerationCars(int[] indexProbability, List <GameObject> nextGenerationCars) { int totalScore = ActualCars.Sum(x => x.GetComponent <Car>().Score); //PrintScores(); for (int i = 0; i < this.Population; i++) { int firstCarIndex = indexProbability[Random.Range(0, totalScore - 1)]; int secondCarIndex = indexProbability[Random.Range(0, totalScore - 1)]; DNA first = ActualCars[firstCarIndex].GetComponent <Car>().DNA; DNA second = ActualCars[secondCarIndex].GetComponent <Car>().DNA; DNA crossOver = first.CrossOver(first, second); crossOver.Mutate(); float xRandom = Random.Range(InitPosition.x - 1f, InitPosition.x + 1f);; float yRandom = Random.Range(InitPosition.y - 0.5f, InitPosition.y + 0.5f); GameObject newCar = Instantiate(Prefab, new Vector2(xRandom, yRandom), Rotation); Car car = newCar.GetComponent <Car>(); car.Initialize(crossOver); car.CarEvent += ReducePopulation; nextGenerationCars.Add(newCar); } }
public void Should_CrossOver(string str1, string str2, string expected) { DNA parent1 = new DNA(str1.Length); parent1.Word = new StringBuilder(str1); DNA parent2 = new DNA(str2.Length); parent2.Word = new StringBuilder(str2); DNA child = parent1.CrossOver(parent2); Assert.Equal(child.Word.ToString(), expected); }
// Creating a new Generation public void Generate() { for (int i = 0; i < popullations.Length; i++) { DNA randomChild_1 = mattingPool [Random.Range(0, mattingPool.Count)]; DNA randomChild_2 = mattingPool [Random.Range(0, mattingPool.Count)]; DNA newChild = randomChild_1.CrossOver(randomChild_2); newChild.Mutate(mutationRate); this.popullations [i] = newChild; } CalculateFitness(); generation++; }
public void NewGeneration() { if (population.Count <= 0) { return; } CalculateFitness(); List <Challenger> newPopulation = new List <Challenger>(); // for (int i = 0; i < amountOfChallengers; i++) // { // Challenger c = Instantiate(challengerPrefab, challengerPrefab.transform.position, Quaternion.identity); // c.dna = new DNA<Direction>(genesLength, random, GetRandomDirection, FitnessFunction, true); // c.transform.name = "Challenger " + i; // newPopulation.Add(c); // } //crossovers for (int i = 0; i < population.Count; i++) { DNA <Direction> parent1 = ChooseParent(); DNA <Direction> parent2 = ChooseParent(); DNA <Direction> child = null; Challenger c = Instantiate(challengerPrefab, challengerPrefab.transform.position, Quaternion.identity); if (parent1 != null && parent2 != null) { child = parent1.CrossOver(parent2); child.Mutate(MutationRate); c.dna = child; } else { c.dna = new DNA <Direction>(genesLength, random, GetRandomDirection, FitnessFunction, true); } newPopulation.Add(c); } foreach (var item in population) { Destroy(item.gameObject); } population.Clear(); population = newPopulation; Generation++; }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDna = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < finalCount; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDna) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.CrossOver(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
static void Main(string[] args) { var neural1 = new NeuralNetwork(new int[] { 4, 4, 2 }); var neural2 = new NeuralNetwork(new int[] { 4, 4, 2 }); Console.WriteLine("Valeurs d'entrées : 0 1 0 1"); Console.WriteLine("-- --- --- --- --- --- --- --"); /* Test de la propagation des données, on doit obtenir les mêmes résultats si on mets les mêmes entrées! */ Console.WriteLine("Reseau neuronal 1"); for (int i = 0; i < 2; i++) { var results = neural1.Propagate(new float[] { 0, 1, 0, 1 }); Console.Write(string.Format("n{0}\n", i)); foreach (var result in results) { Console.WriteLine(result.ToString()); } Console.Write("\n"); } Console.WriteLine("-- --- --- --- --- --- --- --"); Console.WriteLine("Reseau neuronal 2"); for (int i = 0; i < 2; i++) { var results = neural2.Propagate(new float[] { 0, 1, 0, 1 }); Console.Write(string.Format("n{0} : \n", i)); foreach (var result in results) { Console.WriteLine(result.ToString()); } Console.Write("\n"); } Console.WriteLine("-- --- --- --- --- --- --- --"); Console.WriteLine("CrossOver"); /* Test du cross-over */ DNA parent1 = new DNA(neural1); DNA parent2 = new DNA(neural2); DNA child1 = parent1.CrossOver(parent2); DNA child2 = parent2.CrossOver(parent1); Console.WriteLine("\nSans mutation\n"); for (int i = 0; i < parent1.genes.Length; i++) { Console.Write(string.Format("P1 : {0} | P2 : {1} | C1 : {2}{4} | C2 : {3}{5}\n", Math.Round(parent1.genes[i], 5), Math.Round(parent2.genes[i], 5), Math.Round(child1.genes[i], 5), Math.Round(child2.genes[i], 5), child1.genes[i] == parent1.genes[i] ? " (P1)" : child1.genes[i] == parent2.genes[i] ? " (P2)" : " (MU)", child2.genes[i] == parent1.genes[i] ? " (P1)" : child2.genes[i] == parent2.genes[i] ? " (P2)" : " (MU)" ) ); } int mutationRate = 5; child1.Mutate(mutationRate); child2.Mutate(mutationRate); Console.WriteLine(string.Format("\nAvec un taux de mutation a {0}/100 par gènes, soit une déterioration de l'ADN de {1}% max, ce qui fait que l'ADN contient au min {2}% des gènes des parents.\n", mutationRate, (mutationRate / 100.0) * 100, 100.0 - (mutationRate))); for (int i = 0; i < parent1.genes.Length; i++) { Console.Write(string.Format("P1 : {0} | P2 : {1} | C1 : {2}{4} | C2 : {3}{5}\n", Math.Round(parent1.genes[i], 5), Math.Round(parent2.genes[i], 5), Math.Round(child1.genes[i], 5), Math.Round(child2.genes[i], 5), child1.genes[i] == parent1.genes[i] ? " (P1)" : child1.genes[i] == parent2.genes[i] ? " (P2)" : " (MU)", child2.genes[i] == parent1.genes[i] ? " (P1)" : child2.genes[i] == parent2.genes[i] ? " (P2)" : " (MU)" ) ); } var childNeural1 = new NeuralNetwork(child1); var childNeural2 = new NeuralNetwork(child2); var child1Result = childNeural1.Propagate(new float[] { 0, 1, 0, 1 }); var child2Result = childNeural2.Propagate(new float[] { 0, 1, 0, 1 }); Console.WriteLine("Resultat du child 1 : "); foreach (var result in child1Result) { Console.Write(result.ToString() + "\n"); } Console.WriteLine("\nResultat du child 2 : "); foreach (var result in child2Result) { Console.Write(result.ToString() + "\n"); } }