private List <Gene> FindBestGenes(TowerState state, int pathLength, int geneLength, int maximumTurns) { var gg = new GenerationGenerator(generationCount, geneLength); var generation = gg.RandomGeneration(); var simulator = new Simulator(pathLength, maximumTurns, state.Cannons, state.Archers); var bestScores = new List <double>(); while (!EnoughGenerations(bestScores, state)) { Parallel.ForEach(generation, (gene) => { var result = simulator.Simulate(new MyGeneParser(gene)); gene.Score = scoringPolicy.CalculateTotalScore(result); }); bestScores.Add((double)generation.Select(x => x.Score).Max()); generation = gg.Genetic(generation); } return(generation); }
private static Gene findBestGeneForTowerPattern( int[] cannons, int[] archers, int length, int preferredMoneyToSpend, int generationPopulation, int maximumCountOfGenerations, int geneToTroopMean, int lengthOfGene, IScoringPolicy scoringPolicy, bool printEvaluationLog) { Gene bestGene = null; var simulator = new Simulator(length, turns, cannons, archers); var gg = new GenerationGenerator(generationPopulation, lengthOfGene); var generation = gg.RandomGeneration(); var archerString = towerLocationsToString(archers, length, "a"); var cannonString = towerLocationsToString(cannons, length, "c"); double lastBestScore = default; var bestScoreCount = 0; var bestScoreCountLimit = 30; var st = new Stopwatch(); st.Start(); for (var i = 0; i < maximumCountOfGenerations; i++) { foreach (var gene in generation) { var result = simulator.Simulate(new MyGeneParser(gene)); gene.Score = scoringPolicy.CalculateTotalScore(result); } bestGene = Program.bestGene(generation); if (printEvaluationLog) { var sortedGeneration = generation.OrderBy(x => x.Score).ToList(); logGenerationStatistics(length, generation, i, sortedGeneration, bestGene, archerString, cannonString, geneToTroopMean); if (Console.KeyAvailable) { Console.ReadKey(); Console.ReadKey(); logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); Console.ReadKey(); } } if (bestGene.Score != null) { if (i == 0) { lastBestScore = bestGene.Score.Value; bestScoreCount++; } else { if (bestGene.Score.Value.Equals(lastBestScore) && !bestGene.Score.Value.Equals(0)) { bestScoreCount++; } else { lastBestScore = bestGene.Score.Value; bestScoreCount = 1; } } } if (bestScoreCount >= bestScoreCountLimit) { if (printEvaluationLog) { logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); } break; } if (i != maximumCountOfGenerations - 1) { generation = gg.Genetic(generation); } else { if (printEvaluationLog) { logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); } } } st.Stop(); if (printEvaluationLog) { Console.WriteLine(st.Elapsed); } return(bestGene); }
public static async Task RunCompetitions(string mapPath, CompetetionMode competetionMode, CancellationToken cancellationToken = default) { switch (competetionMode) { case CompetetionMode.NoContainer: break; case CompetetionMode.ContainerPerCompetetion: await DockerService.BuildImageAsync(Directory.GetCurrentDirectory(), Program.DockerImageName, cancellationToken); await DockerService.StopAndRemoveAllContainersAsync(cancellationToken); break; case CompetetionMode.ReusableContainer: await DockerService.BuildImageAsync(Directory.GetCurrentDirectory(), Program.DockerImageName, cancellationToken); await DockerService.StopAndRemoveAllContainersAsync(cancellationToken); await ContainerRepository.InitalizeContainers(_geneProcessLimit, Program.DockerImageName, cancellationToken); break; } _arenaStartTime = DateTime.Now; Console.WriteLine("welcome to Colosseum. enjoy the show :)"); List <Gene> lastGeneration = null; var lastGenerationFile = new FileInfo("generationInfo.json"); if (lastGenerationFile.Exists) { Console.WriteLine($"loading last generation from {lastGenerationFile.FullName}"); try { var content = await File.ReadAllTextAsync(lastGenerationFile.FullName, cancellationToken); lastGeneration = JsonConvert.DeserializeObject <List <Gene> >(content); } catch (Exception ex) { Console.WriteLine($"loading last generation failed. error:{Environment.NewLine}{ex}"); } } var generationNumber = 1; var arenaDir = new DirectoryInfo("arena"); arenaDir.Create(); var currentRunDir = arenaDir.CreateSubdirectory(DateTime.Now.ToString("s").Replace(" ", "-").ToValidFileName()); currentRunDir.Create(); var port = _startPort; var cts = new CancellationTokenSource(); cancellationToken.Register(() => cts.Cancel()); while (true) { var generationProcessStartTime = DateTime.Now; var generationDir = currentRunDir.CreateSubdirectory(generationNumber.ToString()); generationDir.Create(); var newGeneration = _generationGenerator.Genetic(lastGeneration); lastGeneration = new List <Gene>(); var competitionTasks = new List <Task>(); switch (competetionMode) { case CompetetionMode.NoContainer: await cleanSystem(cancellationToken); break; case CompetetionMode.ContainerPerCompetetion: await DockerService.StopAndRemoveAllContainersAsync(cancellationToken); break; case CompetetionMode.ReusableContainer: break; } Console.WriteLine($"running generation #{generationNumber}"); Console.WriteLine($"this generation will have {newGeneration.Count} genes and we'll process up to {_geneProcessLimit} genes simultaneously"); Console.WriteLine("-----------"); Console.WriteLine("no.\tsuccess\tscore\t\tid\t\telapsed\t\tsystem elapsed\t\tPPM\t\tcurrent time"); foreach (var gene in newGeneration) { competitionTasks.AddThreadSafe(processGene(gene, mapPath, lastGeneration, port, generationDir, competetionMode, cancellationToken)); port++; } await Task.WhenAll(competitionTasks).CancelOnFaulted(cts); var generationInfoFilePath = Path.Combine(generationDir.FullName, "generationInfo.json"); await File.WriteAllTextAsync(generationInfoFilePath, JsonConvert.SerializeObject(newGeneration, Formatting.Indented), cancellationToken); var generationScoreAverage = newGeneration.Average(x => x.Score); Console.WriteLine($"generation score average: {generationScoreAverage}, generation elapsed time: {DateTime.Now - generationProcessStartTime}"); generationNumber++; } }