-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogram.py
More file actions
158 lines (125 loc) · 3.9 KB
/
Copy pathHistogram.py
File metadata and controls
158 lines (125 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import annotations
import random
from typing import List
import matplotlib.pyplot as plt
# Import from your main Blackjack simulation
from main import (
Rules,
Policy,
RandomPolicy,
ThresholdPolicy,
BasicStrategyPolicy,
simulate,
)
# ----------------------------
# Plotting helpers
# ----------------------------
def plot_outcome_rates(
policies: List[Policy],
win_rates: List[float],
loss_rates: List[float],
push_rates: List[float],
filename: str
) -> None:
"""
Create a grouped bar chart showing win/loss/push rates per strategy.
Args:
policies: List of strategies (used for x-axis labels).
win_rates: Win rates per strategy (0..1).
loss_rates: Loss rates per strategy (0..1).
push_rates: Push rates per strategy (0..1).
filename: Output image filename (e.g., 'diagram_outcome_rates.png').
"""
x = list(range(len(policies)))
width = 0.25
plt.figure()
plt.bar([i - width for i in x], win_rates, width=width, label="Win rate")
plt.bar([i for i in x], loss_rates, width=width, label="Loss rate")
plt.bar([i + width for i in x], push_rates, width=width, label="Push rate")
labels = [getattr(p, "name", p.__class__.__name__) for p in policies]
plt.xticks(x, labels, rotation=25, ha="right")
plt.ylim(0, 1)
plt.xlabel("Strategy")
plt.ylabel("Rate")
plt.title("Blackjack Strategy Comparison: Win / Loss / Push Rates")
plt.legend()
plt.tight_layout()
plt.savefig(filename, dpi=200)
plt.close()
def plot_average_profit(
policies: List[Policy],
avg_profits: List[float],
filename: str
) -> None:
"""
Create a bar chart showing average profit per game for each strategy.
Args:
policies: List of strategies (used for x-axis labels).
avg_profits: Average profit per game per strategy.
filename: Output image filename (e.g., 'diagram_average_profit.png').
"""
plt.figure()
labels = [getattr(p, "name", p.__class__.__name__) for p in policies]
plt.bar(labels, avg_profits)
plt.xticks(rotation=25, ha="right")
plt.xlabel("Strategy")
plt.ylabel("Average Profit per Game")
plt.title("Blackjack Strategy Comparison: Average Profit")
plt.tight_layout()
plt.savefig(filename, dpi=200)
plt.close()
# ----------------------------
# Main
# ----------------------------
def main() -> None:
"""
Run the Monte Carlo simulation for multiple strategies and export diagrams.
Outputs:
- diagram_outcome_rates.png
- diagram_average_profit.png
"""
n_games = 50_000
rules = Rules(
dealer_hits_soft_17=False,
blackjack_payout=1.5,
bet=1.0
)
policies: List[Policy] = [
RandomPolicy(rng=random.Random(1)),
ThresholdPolicy(threshold=17),
ThresholdPolicy(threshold=16),
BasicStrategyPolicy(),
]
win_rates: List[float] = []
loss_rates: List[float] = []
push_rates: List[float] = []
avg_profits: List[float] = []
for i, policy in enumerate(policies):
# Different seeds make runs reproducible but avoid identical randomness per strategy.
summary = simulate(
policy,
n_games=n_games,
seed=100 + i,
rules=rules
)
win_rates.append(summary.win_rate)
loss_rates.append(summary.loss_rate)
push_rates.append(summary.push_rate)
avg_profits.append(summary.profit_avg)
plot_outcome_rates(
policies,
win_rates,
loss_rates,
push_rates,
filename="diagram_outcome_rates.png"
)
plot_average_profit(
policies,
avg_profits,
filename="diagram_average_profit.png"
)
print("✔ Diagrams successfully created:")
print(" - diagram_outcome_rates.png")
print(" - diagram_average_profit.png")
if __name__ == "__main__":
main()