From f1d616576c7ef87e80221556b6939ff7cd8d684a Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 17 May 2020 11:56:06 +0200 Subject: [PATCH] Added optional file log out --- src/AI/resultsExplorer/resultsExplorer.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/AI/resultsExplorer/resultsExplorer.py b/src/AI/resultsExplorer/resultsExplorer.py index 83c1ec5..5827c52 100644 --- a/src/AI/resultsExplorer/resultsExplorer.py +++ b/src/AI/resultsExplorer/resultsExplorer.py @@ -2,6 +2,7 @@ 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. +Optional: Speficy output file. """ import sys @@ -27,26 +28,27 @@ def fitnessPlot(pop): print("Figure saved to file.") -def printBestWorstGenes(pop): +def printBestWorstGenes(pop, logOut=sys.stdout): """ Print info about best and worst specimen. :param pop: Population array with fitness + :param logOut: If specified, print messages to a file. """ fitnesses = [i[4] for i in pop] bestIdxs = (numpy.where(fitnesses == numpy.max(fitnesses)))[0] - print("Best Fitness:", max(fitnesses)) + print("Best Fitness:", max(fitnesses), file=logOut) for idx in bestIdxs: print("Affinities: food={}, water={}, rest={}, walking={}".format(pop[idx][0], pop[idx][1], - pop[idx][2], pop[idx][3])) + pop[idx][2], pop[idx][3]), file=logOut) worstIdxs = (numpy.where(fitnesses == numpy.min(fitnesses)))[0] - print("Worst Fitness:", min(fitnesses)) + print("Worst Fitness:", min(fitnesses), file=logOut) for idx in worstIdxs: print("Affinities: food={}, water={}, rest={}, walking={}".format(pop[idx][0], pop[idx][1], - pop[idx][2], pop[idx][3])) + pop[idx][2], pop[idx][3]), file=logOut) if len(sys.argv) < 2: @@ -62,4 +64,9 @@ else: pop.append(literal_eval(line)) fitnessPlot(pop) - printBestWorstGenes(pop) + if len(sys.argv) >= 3: + with open(sys.argv[2], "w+") as f: + printBestWorstGenes(pop, f) + print("Best/worst species saved to file.") + else: + printBestWorstGenes(pop)