diff options
Diffstat (limited to 'src/trainer/logging.c')
-rw-r--r-- | src/trainer/logging.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/trainer/logging.c b/src/trainer/logging.c new file mode 100644 index 0000000..f35ab73 --- /dev/null +++ b/src/trainer/logging.c @@ -0,0 +1,71 @@ +// File by "Yangelis" https://github.com/yangelis + +#include "../game.h" + +FILE *gnuplot_pipe = 0; +#define nOfGnuplotCmds 14 +char *gnuplotCmds[] = { + "reset\n", + "set size 1,1\n", + "set origin 0,0\n", + "set xlabel 'Generations'\n", + "set multiplot layout 2, 2 columnsfirst title 'gp live stats'\n", + "set ylabel 'Avg Lifetime'\n", + "plot 'log.dat' using 1:2 with lines lw 2 lt 10 notitle \n", + "set ylabel 'Min lifetime'\n", + "plot 'log.dat' using 1:3 with lines lw 2 lt 10 notitle\n", + "set ylabel 'Max lifetime'\n", + "plot 'log.dat' using 1:4 with lines lw 2 lt 10 notitle\n", + "set ylabel 'Food Eaten'\n", + "plot 'log.dat' using 1:5 with lines lw 2 lt 10 notitle\n", + "unset multiplot\n" +}; + + +float avg_lifetime(Agent *agents) { + int result = 0.0; + for (size_t i = 0; i < AGENTS_COUNT; ++i) { + result += agents[i].lifetime; + } + return (float)result / AGENTS_COUNT; +} +void log_header(FILE *stream) { + fprintf(stream, "#Loggind generations\n"); + fprintf( + stream, + "#Generation, Avg Lifetime, Min Lifetime, Max Lifetime, Food eaten\n"); + gnuplot_pipe = popen("gnuplot -p", "w"); +} +void log_generation(FILE *stream, int gen, Game *game) { + float avg_lf = avg_lifetime(game->agents); + int max_lifetime = game->agents[0].lifetime; + int min_lifetime = game->agents[0].lifetime; + for (size_t i = 1; i < AGENTS_COUNT; ++i) { + if (game->agents[i].lifetime > max_lifetime) { + max_lifetime = game->agents[i].lifetime; + } + if (game->agents[i].lifetime < min_lifetime) { + min_lifetime = game->agents[i].lifetime; + } + } + + int food_eaten = FOODS_COUNT; + for (size_t y = 0; y < BOARD_HEIGHT; ++y) { + for (size_t x = 0; x < BOARD_WIDTH; ++x) { + if (game->foods_map[y][x]) { + food_eaten--; + } + } + } + + /* Writing to log file*/ + fprintf(stream, "%d, %f, %d, %d, %d\n", gen, avg_lf, min_lifetime, + max_lifetime, food_eaten); +} +void log_live_update(void) { + for (size_t i = 0; i < nOfGnuplotCmds; ++i) { + fprintf(gnuplot_pipe, "%s \n", gnuplotCmds[i]); + } + fflush(gnuplot_pipe); +} +void log_close_pipe(void) { pclose(gnuplot_pipe); } |