From 0225eda1eb134f81ac9ab33e6d17aab64f98c8af Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 17 May 2020 00:38:36 +0200 Subject: [PATCH] Added file logging --- src/AI/GA.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/AI/GA.py b/src/AI/GA.py index 3a8c66c..ce1c6e2 100644 --- a/src/AI/GA.py +++ b/src/AI/GA.py @@ -1,4 +1,6 @@ import random +from datetime import datetime + import numpy import copy @@ -32,6 +34,11 @@ 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") + for i in range(iter): print("\nRunning {} generation...".format(i + 1)) fitness = [] @@ -47,12 +54,21 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05, multithread=Fals for t in threads: t.join() fitness.append(t.getResult()) + parents = selectMatingPool(population, fitness, int(solutions / 2)) + 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(fitness[0])) + f.write("Best offspring: " + str(offspring[0])) + f.write("\n\n") + def selectMatingPool(population, fitness, count): """