//Update the genetic algorithm. public void update(TestAgent agent) { //End of current target test. if (++tick == TICKS_PER_GENOME) { //Move to next target. currTarget = (currTarget+1) % agent.totalTargets(); //Notify agent of end of target test agent.endOfTarget(currTarget); //calculate fitness and add to genomes current total. population[populationIndex].totalFitness += agent.calculateFitness(); //Reset the agent back to starting values. agent.reset(); //Finished testing current genome if(!agent.targetsEnabled || currTarget == 0) { //Notify agent agent.endOfTests(); //Calculate fitness for current genome population[populationIndex].fitness = population[populationIndex].totalFitness/(agent.targetsEnabled?agent.totalTargets():1); //Add fitness to total. totalFitness += population[populationIndex].fitness; Debug.Log("Population["+populationIndex+"] " +population[populationIndex].fitness); //Save the best fitness for the generation. bestFitness = Math.Max(bestFitness, population[populationIndex].fitness); //Move to next genome ++populationIndex; //Replace weights in agent with new genome's weights. agent.replaceBrain(population[populationIndex%populationSize].weights); //End of one generation if(populationIndex == populationSize) { Debug.Log("Generation "+generation +" completed"); createNewPopulation(); populationIndex = 0; totalFitness = 0; bestFitness = 0; ++generation; } } tick = 0; } }