Import() { Debug.Log("importing phenotypes..."); if (File.Exists("phenotypes.neat")) { string json = File.ReadAllText("phenotypes.neat"); SerializablePhenotypes serializablePhenotypes = JsonUtility.FromJson <SerializablePhenotypes> (json); if (serializablePhenotypes.phenotypes.Count != brains.Count) { Debug.LogError("phenotypes and genomes mismatch error. (" + serializablePhenotypes.phenotypes.Count + " / " + brains.Count + ")"); return; } this.phenotypes = serializablePhenotypes.phenotypes; for (int i = 0; i < brains.Count; i++) { CarBrain brain = brains[i]; brain.AssignPhenotype(phenotypes[i]); brain.runOnlyMode = true; brain.Reset(); } runOnlyMode = true; } }
ResetRunBest() { foreach (CarBrain brain in brains) { if (brain != best) { brain.gameObject.SetActive(false); } } best.Reset(); }
Update() { if (!initalized) { return; } if (runningSimulation || runOnlyMode) { bool keepRunning = false; for (int i = 0; i < brains.Count; i++) { CarBrain brain = brains[i]; if (brain.isAlive) { keepRunning = true; if (brain.Fitness() > best.Fitness() || !best.isAlive) { SetBestCar(brain); } } } if (!keepRunning) { if (runOnlyMode) { for (int i = 0; i < brains.Count; i++) { CarBrain brain = brains[i]; brain.Reset(); } } else { // If all genomes are dead we stop the simulation. runningSimulation = false; } } } if (!runningSimulation && !runOnlyMode) { generationCount++; // Keep track of the all time best phenotype if (best.Fitness() > population.BestFitness()) { bestPhenotype = best.Phenotype(); } /* * Using the genetic algorithm try to improve the brains for each * genome in the hope that it will perform better. */ phenotypes = population.Epoch(GetFitnessScores()); // TODO: clean up the mess below Dictionary <int, CGenome> genomeIDs = new Dictionary <int, CGenome>(); foreach (CGenome genome in population.Genomes()) { genomeIDs.Add(genome.ID(), genome); } List <CarBrain> brainsToBeAssigned = new List <CarBrain> (brains); for (int i = brainsToBeAssigned.Count - 1; i >= 0; i--) { int oldGenomeID = brainsToBeAssigned[i].genomeID; if (genomeIDs.ContainsKey(oldGenomeID) && genomeIDs[oldGenomeID].Phenotype() != null) { brainsToBeAssigned[i].AssignPhenotype(genomeIDs[oldGenomeID].Phenotype()); genomeIDs.Remove(oldGenomeID); brainsToBeAssigned[i].GetComponent <Car>().id.text = "" + brains[i].genomeID; brainsToBeAssigned[i].Reset(); brainsToBeAssigned.RemoveAt(i); } } for (int i = 0; i < brainsToBeAssigned.Count; i++) { brainsToBeAssigned[i].genomeID = genomeIDs.ElementAt(0).Key; brainsToBeAssigned[i].AssignPhenotype(genomeIDs.ElementAt(0).Value.Phenotype()); genomeIDs.Remove(genomeIDs.ElementAt(0).Key); brainsToBeAssigned[i].GetComponent <Car>().id.text = "" + brains[i].genomeID; brainsToBeAssigned[i].Reset(); } generation.text = "Generation:\t" + generationCount; bestFitness.text = string.Format("Best fitness:\t{0:#.0000}", population.BestFitness()); Dictionary <int, int> allSpecies = new Dictionary <int, int> (); CSpecies theBestSpecies = null; float bestSpeciesFitness = -1; for (int i = 0; i < population.Species().Count; i++) { CSpecies species = population.Species()[i]; allSpecies.Add(species.ID(), species.NumMembers()); if (species.BestFitness() > bestSpeciesFitness) { bestSpeciesFitness = species.BestFitness(); theBestSpecies = species; } if (species.Leader() == null) { continue; } } numSpecies.text = "Num species:\t" + allSpecies.Count; bestSpecies.text = "Best species:\t" + theBestSpecies.ID(); numMembers.text = "Num members:\t" + theBestSpecies.NumMembers(); speciesAge.text = "Species age:\t" + theBestSpecies.Age(); gensWithoutImproving.text = "Gens no impr:\t" + theBestSpecies.GensNoImprovement(); bar.UpdateBar(allSpecies); runningSimulation = true; } }