From 31083435139db9df7f6c8ab1c6fdba0a7b79e1ee Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 17 May 2020 10:25:10 +0200 Subject: [PATCH] Logging changes --- src/AI/GA.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/AI/GA.py b/src/AI/GA.py index e3808ba..5b0cc47 100644 --- a/src/AI/GA.py +++ b/src/AI/GA.py @@ -34,10 +34,6 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05, multithread=Fals for i in range(solutions): maps.append(Map(map.filename, map.screen)) - # Initialize log file - with open("results.txt", "w+") as f: - f.write("GA Results from " + str(datetime.now())) - f.write("\n") # Set the RNG seed for this GA # 5 is good for weak start @@ -66,14 +62,10 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05, multithread=Fals print("Best fitness: {}".format(max(fitness))) offspring = mating(parents, solutions, mutationAmount) print("Best offspring: ", offspring[0]) - population = offspring - # Add info to logfile - with open("results.txt", "a") as f: - f.write("Population: {}\n".format(i)) - f.write("Best fitness: {}\n".format(max(fitness))) - f.write("Best offspring: " + str(offspring[0])) - f.write("\n\n") + writeResults(i, max(fitness), offspring[0]) + + population = offspring def selectMatingPool(population, fitness, count): @@ -218,3 +210,21 @@ def pickEntity(player, map): # choice = random.choices(finalEntities, finalWeights)[0] return choice + + +def writeResults(iter, bestFit, bestMember): + if iter == 0: + # Initialize human-readable log file + with open("results.txt", "w+") as f: + f.write("GA Results from " + str(datetime.now())) + f.write("\n") + + with open("results.txt", "a") as f: + f.write("Population: {}\n".format(iter)) + f.write("Best fitness: {}\n".format(bestFit)) + f.write("Best offspring: " + str(bestMember)) + f.write("\n\n") + + # Write raw arrays + with open("resultsRaw.txt", "w+") as f: + f.write(str(bestMember + [bestFit]))