Added optional file log out

This commit is contained in:
Marcin Kostrzewski 2020-05-17 11:56:06 +02:00
parent 89eb557a2d
commit f1d616576c

View File

@ -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)