|
| 1 | +import re |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import argparse |
| 4 | +import time |
| 5 | + |
| 6 | +def parse_simulation_output(file_path): |
| 7 | + cow_counts = [] |
| 8 | + tiger_counts = [] |
| 9 | + tree_counts = [] |
| 10 | + grass_counts = [] |
| 11 | + |
| 12 | + with open(file_path, 'r') as file: |
| 13 | + for line in file: |
| 14 | + cow_match = re.search(r'Cow:\s*(\d+)', line) |
| 15 | + if cow_match: |
| 16 | + cow_counts.append(int(cow_match.group(1))) |
| 17 | + |
| 18 | + tiger_match = re.search(r'Tiger:\s*(\d+)', line) |
| 19 | + if tiger_match: |
| 20 | + tiger_counts.append(int(tiger_match.group(1))) |
| 21 | + |
| 22 | + tree_match = re.search(r'Tree:\s*(\d+)', line) |
| 23 | + if tree_match: |
| 24 | + tree_counts.append(int(tree_match.group(1))) |
| 25 | + |
| 26 | + grass_match = re.search(r'Grass:\s*(\d+)', line) |
| 27 | + if grass_match: |
| 28 | + grass_counts.append(int(grass_match.group(1))) |
| 29 | + |
| 30 | + return cow_counts, tiger_counts, tree_counts, grass_counts |
| 31 | + |
| 32 | +def plot_predator_prey(cows, tigers, grass): |
| 33 | + plt.plot(range(len(cows)), cows, label='Cows (Prey)', color='blue') |
| 34 | + plt.plot(range(len(tigers)), tigers, label='Tigers (Predator)', color='red') |
| 35 | + plt.plot(range(len(grass)), grass, label='Grass (Food Source)', color='green') |
| 36 | + plt.xlabel('Time Steps') |
| 37 | + plt.ylabel('Population') |
| 38 | + plt.title('Predator-Prey-Resource Population Dynamics') |
| 39 | + plt.legend() |
| 40 | + plt.grid() |
| 41 | + plt.show() |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + parser = argparse.ArgumentParser(description="Parse simulation output and plot predator-prey-resource dynamics.") |
| 45 | + parser.add_argument("filepath", type=str, help="Path to the simulation output file") |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + cow_population, tiger_population, _, grass_population = parse_simulation_output(args.filepath) |
| 49 | + plot_predator_prey(cow_population, tiger_population, grass_population) |
0 commit comments