Skip to content

Commit 9de1c79

Browse files
committed
Refactor plotting functions to remove cfg parameter and dynamically determine config parameters
1 parent 47d549f commit 9de1c79

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

main.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,28 @@ def printWinPercentage():
3131
winStr = f"{winPercentage:.10f}".rstrip('0').rstrip('.')
3232
print(f"\nWin percentage: {winStr}%")
3333

34-
def printAvgBoxChecks(cfg):
34+
def printAvgBoxChecks():
3535
prisonersLog = os.path.join(working_dir, 'results.csv')
36+
num_prisoners = -1
37+
num_simulations = -1
3638
simResults = {}
3739
avgChecksPerPrisoner = {}
40+
3841
with open(prisonersLog, mode='r', newline='') as file:
3942
reader = csv.DictReader(file)
4043
for row in reader:
4144
simId = int(row['Simulation'])
4245
prisoner = int(row['PrisonerID'])
4346
checkedBoxesCount = int(row['CheckedBoxesCount'])
47+
num_prisoners = max(num_prisoners, prisoner + 1)
48+
num_simulations = max(num_simulations, simId + 1)
4449
if simId not in simResults:
4550
simResults[simId] = {}
4651
simResults[simId][prisoner] = checkedBoxesCount
4752

48-
for prisoner in range(cfg["num_prisoners"]):
53+
for prisoner in range(num_prisoners):
4954
totalChecks = sum(simResults[simId].get(prisoner, 0) for simId in simResults)
50-
avgChecksPerPrisoner[prisoner] = totalChecks / cfg["num_simulations"]
55+
avgChecksPerPrisoner[prisoner] = totalChecks / num_simulations
5156
overall_avg = sum(avgChecksPerPrisoner.values()) / len(avgChecksPerPrisoner)
5257

5358
plt.bar(avgChecksPerPrisoner.keys(), avgChecksPerPrisoner.values())
@@ -58,25 +63,32 @@ def printAvgBoxChecks(cfg):
5863
plt.legend()
5964
plt.show()
6065

61-
def printPctFinds(cfg):
66+
def printPctFinds():
6267
prisonersLog = os.path.join(working_dir, 'results.csv')
63-
findCounts = {i: 0 for i in range(cfg["num_prisoners"])}
68+
num_prisoners = -1
69+
num_simulations = -1
70+
findCounts = {}
6471
with open(prisonersLog, mode='r', newline='') as file:
6572
reader = csv.DictReader(file)
6673
for row in reader:
6774
prisoner = int(row['PrisonerID'])
75+
simId = int(row['Simulation'])
6876
found = row['FoundBox'] == 'True'
77+
num_prisoners = max(num_prisoners, prisoner + 1)
78+
num_simulations = max(num_simulations, simId + 1)
79+
if prisoner not in findCounts:
80+
findCounts[prisoner] = 0
6981
if found:
7082
findCounts[prisoner] += 1
7183

72-
pctFinds = {prisoner: (count / cfg["num_simulations"]) * 100 for prisoner, count in findCounts.items()}
84+
pctFinds = {prisoner: (count / num_simulations) * 100 for prisoner, count in findCounts.items()}
7385
plt.bar(pctFinds.keys(), pctFinds.values())
7486
plt.xlabel("Prisoner ID")
7587
plt.ylabel("Percentage of Finds (%)")
7688
plt.title("Percentage of Finds per Prisoner")
7789
plt.show()
7890

79-
def run(cfg):
91+
def run():
8092
prisonersLog = os.path.join(working_dir, 'results.csv')
8193
if not os.path.exists(prisonersLog):
8294
print("No results file found.")
@@ -93,9 +105,9 @@ def run(cfg):
93105
if choice == '1':
94106
plots_stats.printWinPercentage()
95107
elif choice == '2':
96-
plots_stats.printAvgBoxChecks(cfg)
108+
plots_stats.printAvgBoxChecks()
97109
elif choice == '3':
98-
plots_stats.printPctFinds(cfg)
110+
plots_stats.printPctFinds()
99111
elif choice == '4':
100112
print("Exiting.")
101113
return
@@ -195,7 +207,7 @@ def simulatePrisoners(cfg):
195207
elif choice == '2':
196208
simulatePrisoners(cfg)
197209
elif choice == '3':
198-
plots_stats.run(cfg)
210+
plots_stats.run()
199211
elif choice == '4':
200212
print("Exiting.")
201213
break

0 commit comments

Comments
 (0)