GetFitnessScores() { Dictionary <int, float> scores = new Dictionary <int, float> (); /* * brains.Sort(delegate (CarBrain a, CarBrain b) { * return b.Fitness().CompareTo(a.Fitness()); * });*/ for (int i = 0; i < brains.Count; i++) { CarBrain brain = brains[i]; scores.Add(brain.genomeID, brain.Fitness()); } return(scores); }
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; } }