33import random
44import os
55import csv
6+ from matplotlib import pyplot as plt
67
78class plots_stats :
89 def printWinPercentage ():
@@ -28,9 +29,36 @@ def printWinPercentage():
2829
2930 winPercentage = (wins / totalSims ) * 100
3031 winStr = f"{ winPercentage :.10f} " .rstrip ('0' ).rstrip ('.' )
31- print (f"Win percentage: { winStr } %" )
32+ print (f"\n Win percentage: { winStr } %" )
3233
33- def run ():
34+ def printAvgBoxChecks (cfg ):
35+ prisonersLog = os .path .join (working_dir , 'results.csv' )
36+ simResults = {}
37+ avgChecksPerPrisoner = {}
38+ with open (prisonersLog , mode = 'r' , newline = '' ) as file :
39+ reader = csv .DictReader (file )
40+ for row in reader :
41+ simId = int (row ['Simulation' ])
42+ prisoner = int (row ['PrisonerID' ])
43+ checkedBoxesCount = int (row ['CheckedBoxesCount' ])
44+ if simId not in simResults :
45+ simResults [simId ] = {}
46+ simResults [simId ][prisoner ] = checkedBoxesCount
47+
48+ for prisoner in range (cfg ["num_prisoners" ]):
49+ totalChecks = sum (simResults [simId ].get (prisoner , 0 ) for simId in simResults )
50+ avgChecksPerPrisoner [prisoner ] = totalChecks / cfg ["num_simulations" ]
51+ overall_avg = sum (avgChecksPerPrisoner .values ()) / len (avgChecksPerPrisoner )
52+
53+ plt .bar (avgChecksPerPrisoner .keys (), avgChecksPerPrisoner .values ())
54+ plt .axhline (y = overall_avg , color = 'r' , linestyle = '-' , label = f'Overall Average: { overall_avg :.2f} ' )
55+ plt .xlabel ("Prisoner ID" )
56+ plt .ylabel ("Average Checked Boxes" )
57+ plt .title ("Average Checked Boxes per Prisoner" )
58+ plt .legend ()
59+ plt .show ()
60+
61+ def run (cfg ):
3462 prisonersLog = os .path .join (working_dir , 'results.csv' )
3563 if not os .path .exists (prisonersLog ):
3664 print ("No results file found." )
@@ -40,11 +68,14 @@ def run():
4068 print (f"\n Working directory: { working_dir } " )
4169 print ("\n Choose an option:" )
4270 print ("1. Show win percentage" )
43- print ("2. Exit/Return to main menu" )
71+ print ("2. Show average checked boxes per prisoner" )
72+ print ("3. Exit/Return to main menu" )
4473 choice = input ("Enter your choice: " ).strip ()
4574 if choice == '1' :
4675 plots_stats .printWinPercentage ()
4776 elif choice == '2' :
77+ plots_stats .printAvgBoxChecks (cfg )
78+ elif choice == '3' :
4879 print ("Exiting." )
4980 return
5081 else :
@@ -100,9 +131,7 @@ def getWorkingDir():
100131 else :
101132 print (f"Directory { working_dir } does not exist. Please try again." )
102133
103- def simulatePrisoners ():
104- config = importConfigModule ()
105- cfg = config .getConfig ()
134+ def simulatePrisoners (cfg ):
106135 lastSim = logging .loadLogs ()
107136 startSim = lastSim + 1
108137 if startSim >= cfg ["num_simulations" ]:
@@ -130,6 +159,8 @@ def simulatePrisoners():
130159if __name__ == "__main__" :
131160 base_dir = os .path .dirname (os .path .abspath (__file__ ))
132161 working_dir = getWorkingDir ()
162+ config = importConfigModule ()
163+ cfg = config .getConfig ()
133164 while True :
134165 print (f"\n Working directory: { working_dir } " )
135166 print (f"\n Choose an option:" )
@@ -141,9 +172,9 @@ def simulatePrisoners():
141172 if choice == '1' :
142173 working_dir = getWorkingDir ()
143174 elif choice == '2' :
144- simulatePrisoners ()
175+ simulatePrisoners (cfg )
145176 elif choice == '3' :
146- plots_stats .run ()
177+ plots_stats .run (cfg )
147178 elif choice == '4' :
148179 print ("Exiting." )
149180 break
0 commit comments