Skip to content

Commit 238e5c4

Browse files
authored
Improved play_games_and_return_stats , closing #128 (#131)
Co-authored-by: Michael Cochez <[email protected]>
1 parent 643fa4e commit 238e5c4

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

executables/cli.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@ def main() -> None:
2222
"""Various Schnapsen Game Examples"""
2323

2424

25-
def play_games_and_return_stats(engine: GamePlayEngine, bot1: Bot, bot2: Bot, number_of_games: int) -> int:
25+
def play_games_and_return_stats(engine: GamePlayEngine, bot1: Bot, bot2: Bot, pairs_of_games: int) -> int:
2626
"""
27-
Play number_of_games games between bot1 and bot2, using the SchnapsenGamePlayEngine, and return how often bot1 won.
28-
Prints progress.
27+
Play 2 * pairs_of_games games between bot1 and bot2, using the SchnapsenGamePlayEngine, and return how often bot1 won.
28+
Prints progress. Each pair of games is the same original dealing of cards, but the roles of the bots are swapped.
2929
"""
3030
bot1_wins: int = 0
3131
lead, follower = bot1, bot2
32-
for i in range(1, number_of_games + 1):
33-
if i % 2 == 0:
34-
# swap bots so both start the same number of times
35-
lead, follower = follower, lead
36-
winner, _, _ = engine.play_game(lead, follower, random.Random(i))
37-
if winner == bot1:
38-
bot1_wins += 1
39-
if i % 500 == 0:
40-
print(f"Progress: {i}/{number_of_games}")
32+
for game_pair in range(pairs_of_games):
33+
for lead, follower in [(bot1, bot2), (bot2, bot1)]:
34+
winner, _, _ = engine.play_game(lead, follower, random.Random(game_pair))
35+
if winner == bot1:
36+
bot1_wins += 1
37+
if game_pair > 0 and (game_pair + 1) % 500 == 0:
38+
print(f"Progress: {game_pair + 1}/{pairs_of_games} game pairs played")
4139
return bot1_wins
4240

4341

@@ -180,9 +178,10 @@ def try_bot_game() -> None:
180178
bot1: Bot = MLPlayingBot(model_location=model_location)
181179
bot2: Bot = RandBot(random.Random(464566))
182180
number_of_games: int = 10000
181+
pairs_of_games = number_of_games // 2
183182

184183
# play games with altering leader position on first rounds
185-
ml_bot_wins_against_random = play_games_and_return_stats(engine=engine, bot1=bot1, bot2=bot2, number_of_games=number_of_games)
184+
ml_bot_wins_against_random = play_games_and_return_stats(engine=engine, bot1=bot1, bot2=bot2, pairs_of_games=pairs_of_games)
186185
print(f"The ML bot with name {model_name}, won {ml_bot_wins_against_random} times out of {number_of_games} games played.")
187186

188187

src/schnapsen/bots/ml_bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def train_ML_model(replay_memory_location: Optional[pathlib.Path],
208208
# Train a simpler Linear Logistic Regression model
209209
# learn more about the model or how to use better use it by checking out its documentation
210210
# https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression
211-
print("Training a Simple (Linear Logistic Regression model)")
211+
print("Training a Simple (Linear Logistic Regression) model")
212212

213213
# Usually there is no reason to change the hyperparameters of such a simple model but fill free to experiment:
214214
learner = LogisticRegression(max_iter=1000)

0 commit comments

Comments
 (0)