/// <summary> /// Evolve a population /// </summary> /// <param name="pop">Population to evolve</param> /// <param name="generationCount"></param> /// <returns>Evolved population</returns> internal static Population EvolvePopulation(Population pop, int generationCount) { var newPopulation = new Population(pop.Size(), false, generationCount); var fc = new FileCreator(generationCount, pop); // Keep our best individual if (Elitism) { newPopulation.SaveIndividual(0, pop.GetFittest()); } // Crossover population const int elitismOffset = Elitism ? 1 : 0; // Loop over the population size and create new individuals with crossover for (var i = elitismOffset; i < pop.Size(); i++) { var individual1 = TournamentSelection(pop); var individual2 = TournamentSelection(pop); var newIndividual = Crossover(individual1, individual2, generationCount, i); newPopulation.SaveIndividual(i, newIndividual); } // Mutate population for (var i = elitismOffset; i < newPopulation.Size(); i++) { Mutate(newPopulation.GetIndividual(i)); } return(newPopulation); }
internal void CreateRobotFiles(string filePath, string robotId, DnaToCode dnaTranslator) { //Create Robot_gX_iY.cs FileCreator.CreateFile( filePath, $"{robotId}{FileCreator.CodeFileExtension}", GetFileText(robotId, filePath, dnaTranslator), // GetBotZero(robotId), true ); }
} = new Random(); // for random colors internal void CreateStateFiles(string filePath, string robotId, DnaToCode dnaTranslator) { for (var i = 0; i <= 1; i++) { FileCreator.CreateFile( filePath, $"{robotId}_State{i}{FileCreator.CodeFileExtension}", GetFileText(i, robotId, dnaTranslator), true ); } }
internal void CreateBattleFiles(string filePath, string nameSpace, string robotName) { foreach (var enemyRobot in EnemyRobots) { FileCreator.CreateFile( filePath, $"{robotName}_vs_{enemyRobot}.battle", GetFileText($"{nameSpace}.{robotName}", enemyRobot), true ); } }
internal static void CreateFile(int generation, Population population) { var contents = ""; for (var i = 0; i < population.Size(); i++) { contents += population.GetIndividual(i) + Environment.NewLine; } FileCreator.CreateFile(Path.Combine(FileCreator.RootFolderName, FileCreator.PopulationsFolderName), GetFileName(generation), contents, true); }