From 6017a762a118dc4361c3f868835e21d6bd61e62a Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 16 Mar 2025 14:05:18 +0000 Subject: [PATCH] [CT421]: Add Assignment 2 plots.py --- .../assignments/assignment2/code/plots.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 year4/semester2/CT421/assignments/assignment2/code/plots.py diff --git a/year4/semester2/CT421/assignments/assignment2/code/plots.py b/year4/semester2/CT421/assignments/assignment2/code/plots.py new file mode 100755 index 00000000..f304ec9d --- /dev/null +++ b/year4/semester2/CT421/assignments/assignment2/code/plots.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 + +import pandas as pd +import matplotlib.pyplot as plt +import argparse + +# Set up argument parser +parser = argparse.ArgumentParser(description='Plot fitness and strategy counts from a TSV file.') +parser.add_argument('file_path', type=str, help='Path to the TSV file') +args = parser.parse_args() + +# Read the file +df = pd.read_csv(args.file_path, sep="\t") + +df['Generation'] = pd.to_numeric(df['Generation'], errors='coerce') + +# Plot 1: Best Fitness & Average Fitness over Generations +plt.figure(figsize=(10, 5)) +plt.plot(df['Generation'], df['BestFitness'], label='Best Fitness', marker='o') +plt.plot(df['Generation'], df['AvgFitness'], label='Average Fitness', linestyle='dashed', marker='s') +plt.xlabel('Generation') +plt.ylabel('Fitness') +plt.title('Best & Average Fitness Over Generations') +plt.legend() +plt.grid(True) +plt.show() + +# Plot 2: Strategy Counts over Generations +plt.figure(figsize=(10, 5)) +strategies = ["000", "001", "010", "011", "100", "101", "110", "111"] + +for strategy in strategies: + if strategy in df.columns: + plt.plot(df['Generation'], df[strategy], label=strategy, marker='.') + +plt.xlabel('Generation') +plt.ylabel('Strategy Count') +plt.title('Strategy Counts Over Generations') +plt.legend(title="Strategies") +plt.grid(True) +plt.show()