private void StartNewGeneration() { //Turn off Models float[] lastFitness = new float[Population.Length]; string[] lastParents = new string[Population.Length]; float[] lastMutation = new float[Population.Length]; foreach (var individual in Population) { individual.active = false; } //fit parens, mut //Sort fitness. Model[] Sorted = Population.OrderBy(p => p.fitnes).ToArray(); //Remember last 3 best lastbest = Sorted[Sorted.Length - 1].id; for (int i = 0; i < lastThreeBest.Length; i++) { lastThreeBest[i] = newThreeBest[i]; newThreeBest[i] = Sorted[Sorted.Length - 1 - i].fitnes; } string ratting; ratting = "Last Winners\n"; for (int i = 0; i < lastThreeBest.Length; i++) { ratting += (i + 1) + " F:" + newThreeBest[i].ToString("f2") + " LastF:" + lastThreeBest[i].ToString("f2") + " Prog:" + (newThreeBest[i] - lastThreeBest[i]).ToString("f2") + "\n"; } UserInterface.Ratting.text = ratting; //Save Best Half and Create new Half from best, aka Algorithm evolutionStrategy int k = Sorted.Length - 1; for (int i = 0; i < Sorted.Length; i++) { string parents = "NoN"; lastFitness[Sorted[i].id] = Sorted[i].fitnes; Sorted[i].lastMutation = 0; int half = (Sorted.Length / 2); if (i < half) { //create a child int whereCut = UnityEngine.Random.Range(2, SchematSize - 3); //build new Neural network float[] Weights = new float[SchematSize]; Sorted[k].neuralNetwork.WrtieWeights(0, whereCut, ref Weights); Sorted[k - 1].neuralNetwork.WrtieWeights(whereCut, SchematSize - 1, ref Weights); parents = Sorted[k].id + "x" + Sorted[k - 1].id; Sorted[i].ColorMixer(Sorted[k].MySkin.material.color, Sorted[k - 1].MySkin.material.color); k--; //implement new neural network Sorted[i].neuralNetwork.ApplyNewWeights(Weights); //Mutation Sorted[i].Mutate(Mutation); } else { } lastParents[Sorted[i].id] = parents; lastMutation[Sorted[i].id] = Sorted[i].lastMutation; float lotto = UnityEngine.Random.Range(0f, 100f); Sorted[i].ResetPostion(); } Generation++; UserInterface.GenerationInformation.text = Generation + "\t:Generation"; //Turn On Models foreach (var individual in Population) { individual.active = true; } dataRecorder.NexGenRecord(LifeTime, Mutation, lastFitness, lastParents, lastMutation); UserInterface.DataText.text = dataRecorder.ShowData(); }