From aa3eea8b39da6bd9a223047464de9959a7715ae8 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 17 May 2020 11:15:54 +0200 Subject: [PATCH] Added resultsExplorer.py --- src/AI/resultsExplorer/resultsExplorer.py | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/AI/resultsExplorer/resultsExplorer.py diff --git a/src/AI/resultsExplorer/resultsExplorer.py b/src/AI/resultsExplorer/resultsExplorer.py new file mode 100644 index 0000000..5fe5c94 --- /dev/null +++ b/src/AI/resultsExplorer/resultsExplorer.py @@ -0,0 +1,54 @@ +""" +A small script to help you analyze the output logs from GA algorithm. +It shows you best and worst population with all values. +Specify the input file as an exec parameter. +""" +import sys +from ast import literal_eval +import matplotlib.pyplot as plt +import numpy + + +def fitnessPlot(pop): + fig, ax = plt.subplots() + ax.plot([i[4] for i in pop]) + ax.set_title("Best fitness by generation") + ax.set_ylabel("Fitness") + ax.set_xlabel("Generation") + fig.legend() + fig.show() + fig.savefig("fitnessPlot") + print("Figure saved to file.") + + +def printBestWorstGenes(pop): + fitnesses = [i[4] for i in pop] + bestIdxs = (numpy.where(fitnesses == numpy.max(fitnesses)))[0] + + print("Best Fitness:", max(fitnesses)) + for idx in bestIdxs: + print("Affinities: food={}, water={}, rest={}, walking={}".format(pop[idx][0], pop[idx][1], + pop[idx][2], pop[idx][3])) + + worstIdxs = (numpy.where(fitnesses == numpy.min(fitnesses)))[0] + + print("Worst Fitness:", min(fitnesses)) + for idx in worstIdxs: + print("Affinities: food={}, water={}, rest={}, walking={}".format(pop[idx][0], pop[idx][1], + pop[idx][2], pop[idx][3])) + + +if len(sys.argv) < 2: + print("No file specified.") +else: + with open(sys.argv[1], "r") as file: + if file.readline() != "=HEADER=GA=\n": + print("Invalid header. Incorrect file or missing header?") + else: + # Load data into memory + pop = [] + for line in file: + pop.append(literal_eval(line)) + + fitnessPlot(pop) + printBestWorstGenes(pop)