Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SAE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MIRANDA GONZALES Marcelo
SAE
Commits
4c72f4ed
Commit
4c72f4ed
authored
1 month ago
by
MIRANDA GONZALES Marcelo
Browse files
Options
Downloads
Patches
Plain Diff
Delete compare_all_randoms.ipynb
parent
e84e046e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
games/compare_all_randoms.ipynb
+0
-211
0 additions, 211 deletions
games/compare_all_randoms.ipynb
with
0 additions
and
211 deletions
games/compare_all_randoms.ipynb
deleted
100644 → 0
+
0
−
211
View file @
e84e046e
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"background-color: gray;\n",
" color: black;\n",
" padding: 20px;\n",
" text-align: center;\">INFO</h1>\n",
"\n",
"In this script, we compare players `Random1`, `Random2` and `Random3` in a game where there is only one cheese to catch in a maze without mud. \\\n",
"All programs are evaluated on the same game configurations. \\\n",
"We do not show the game interface here, to make the script faster. \\\n",
"The goal is to compare the performances of the different random players in the same conditions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"background-color: gray;\n",
" color: black;\n",
" padding: 20px;\n",
" text-align: center;\">IMPORTS</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# External imports\n",
"import sys\n",
"import os\n",
"import tqdm.auto as tqdm\n",
"import matplotlib.pyplot as pyplot\n",
"import scipy.stats as scstats\n",
"\n",
"# Add needed directories to the path\n",
"sys.path.append(os.path.join(\"..\", \"players\"))\n",
"\n",
"# PyRat imports\n",
"from pyrat import Game, GameMode\n",
"from Random1 import Random1\n",
"from Random2 import Random2\n",
"from Random3 import Random3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"background-color: gray;\n",
" color: black;\n",
" padding: 20px;\n",
" text-align: center;\">CONSTANTS</h1>\n",
"\n",
"In this script, we are going to make multiple independent games. \\\n",
"The goal is to collect enough statistics to draw conclusions on which algorithm is better than the other. \\\n",
"This constant defines how many games are made."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Determines how many games will be played for each player\n",
"NB_GAMES = 1000"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's configure the game with a dictionary. \\\n",
"Note that we put the game mode as `SIMULATION` to perform all games as fast as possible."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Customize the game elements\n",
"CONFIG = {\"mud_percentage\": 0.0,\n",
" \"nb_cheese\": 1,\n",
" \"game_mode\": GameMode.SIMULATION}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"background-color: gray;\n",
" color: black;\n",
" padding: 20px;\n",
" text-align: center;\">RUN THE GAMES</h1>\n",
"\n",
"Let us now perform all games. \\\n",
"For each game, we remember the number of turns needed to complete it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Players to test (keys are legends to appear in the plot)\n",
"players = {\"Random 1\": {\"class\": Random1, \"args\": {}},\n",
" \"Random 2\": {\"class\": Random2, \"args\": {}},\n",
" \"Random 3\": {\"class\": Random3, \"args\": {}}}\n",
"\n",
"# Run the games for each player\n",
"results = {player: [] for player in players}\n",
"for key in players:\n",
" for seed in tqdm.tqdm(range(NB_GAMES), desc=key):\n",
" \n",
" # Make the game with given seed\n",
" game = Game(random_seed=seed, **CONFIG)\n",
" player = players[key][\"class\"](**players[key][\"args\"])\n",
" game.add_player(player)\n",
" stats = game.start()\n",
" \n",
" # Store the number of turns needed\n",
" results[key].append(stats[\"turns\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"background-color: gray;\n",
" color: black;\n",
" padding: 20px;\n",
" text-align: center;\">ANALYZE THE RESULTS</h1>\n",
" \n",
"Now that all games are performed, we plot the percentage of games completed as a function of the number of turns elapsed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualization of cumulative curves of numbers of turns taken per program\n",
"max_turn = max([max(results[player]) for player in results])\n",
"pyplot.figure(figsize=(10, 5))\n",
"for player in results:\n",
" turns = [0] + sorted(results[player]) + [max_turn]\n",
" games_completed_per_turn = [len([turn for turn in results[player] if turn <= t]) * 100.0 / NB_GAMES for t in turns]\n",
" pyplot.plot(turns, games_completed_per_turn, label=player)\n",
"pyplot.title(\"Comparison of turns needed to complete all %d games\" % (NB_GAMES))\n",
"pyplot.xlabel(\"Turns per game\")\n",
"pyplot.ylabel(\"% of games completed\")\n",
"pyplot.xscale(\"log\")\n",
"pyplot.legend()\n",
"pyplot.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualizing is great, but it may be hard to conclude with just a plot. \\\n",
"Here, we perform a statistical test that will give more insight on whether an algorithm is better than the other."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Formal statistics to check if these curves are statistically significant\n",
"for i, player_1 in enumerate(results):\n",
" for j, player_2 in enumerate(results):\n",
" if j > i:\n",
" test_result = scstats.mannwhitneyu(results[player_1], results[player_2], alternative=\"two-sided\")\n",
" print(\"Mann-Whitney U test between turns of program '%s' and of program '%s':\" % (player_1, player_2), test_result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
<h1 style="background-color: gray;
color: black;
padding: 20px;
text-align: center;">INFO
</h1>
In this script, we compare players
`Random1`
,
`Random2`
and
`Random3`
in a game where there is only one cheese to catch in a maze without mud.
\
All programs are evaluated on the same game configurations.
\
We do not show the game interface here, to make the script faster.
\
The goal is to compare the performances of the different random players in the same conditions.
%% Cell type:markdown id: tags:
<h1 style="background-color: gray;
color: black;
padding: 20px;
text-align: center;">IMPORTS
</h1>
%% Cell type:code id: tags:
```
python
# External imports
import
sys
import
os
import
tqdm.auto
as
tqdm
import
matplotlib.pyplot
as
pyplot
import
scipy.stats
as
scstats
# Add needed directories to the path
sys
.
path
.
append
(
os
.
path
.
join
(
"
..
"
,
"
players
"
))
# PyRat imports
from
pyrat
import
Game
,
GameMode
from
Random1
import
Random1
from
Random2
import
Random2
from
Random3
import
Random3
```
%% Cell type:markdown id: tags:
<h1 style="background-color: gray;
color: black;
padding: 20px;
text-align: center;">CONSTANTS
</h1>
In this script, we are going to make multiple independent games.
\
The goal is to collect enough statistics to draw conclusions on which algorithm is better than the other.
\
This constant defines how many games are made.
%% Cell type:code id: tags:
```
python
# Determines how many games will be played for each player
NB_GAMES
=
1000
```
%% Cell type:markdown id: tags:
Let's configure the game with a dictionary.
\
Note that we put the game mode as
`SIMULATION`
to perform all games as fast as possible.
%% Cell type:code id: tags:
```
python
# Customize the game elements
CONFIG
=
{
"
mud_percentage
"
:
0.0
,
"
nb_cheese
"
:
1
,
"
game_mode
"
:
GameMode
.
SIMULATION
}
```
%% Cell type:markdown id: tags:
<h1 style="background-color: gray;
color: black;
padding: 20px;
text-align: center;">RUN THE GAMES
</h1>
Let us now perform all games.
\
For each game, we remember the number of turns needed to complete it.
%% Cell type:code id: tags:
```
python
# Players to test (keys are legends to appear in the plot)
players
=
{
"
Random 1
"
:
{
"
class
"
:
Random1
,
"
args
"
:
{}},
"
Random 2
"
:
{
"
class
"
:
Random2
,
"
args
"
:
{}},
"
Random 3
"
:
{
"
class
"
:
Random3
,
"
args
"
:
{}}}
# Run the games for each player
results
=
{
player
:
[]
for
player
in
players
}
for
key
in
players
:
for
seed
in
tqdm
.
tqdm
(
range
(
NB_GAMES
),
desc
=
key
):
# Make the game with given seed
game
=
Game
(
random_seed
=
seed
,
**
CONFIG
)
player
=
players
[
key
][
"
class
"
](
**
players
[
key
][
"
args
"
])
game
.
add_player
(
player
)
stats
=
game
.
start
()
# Store the number of turns needed
results
[
key
].
append
(
stats
[
"
turns
"
])
```
%% Cell type:markdown id: tags:
<h1 style="background-color: gray;
color: black;
padding: 20px;
text-align: center;">ANALYZE THE RESULTS
</h1>
Now that all games are performed, we plot the percentage of games completed as a function of the number of turns elapsed.
%% Cell type:code id: tags:
```
python
# Visualization of cumulative curves of numbers of turns taken per program
max_turn
=
max
([
max
(
results
[
player
])
for
player
in
results
])
pyplot
.
figure
(
figsize
=
(
10
,
5
))
for
player
in
results
:
turns
=
[
0
]
+
sorted
(
results
[
player
])
+
[
max_turn
]
games_completed_per_turn
=
[
len
([
turn
for
turn
in
results
[
player
]
if
turn
<=
t
])
*
100.0
/
NB_GAMES
for
t
in
turns
]
pyplot
.
plot
(
turns
,
games_completed_per_turn
,
label
=
player
)
pyplot
.
title
(
"
Comparison of turns needed to complete all %d games
"
%
(
NB_GAMES
))
pyplot
.
xlabel
(
"
Turns per game
"
)
pyplot
.
ylabel
(
"
% of games completed
"
)
pyplot
.
xscale
(
"
log
"
)
pyplot
.
legend
()
pyplot
.
show
()
```
%% Cell type:markdown id: tags:
Visualizing is great, but it may be hard to conclude with just a plot.
\
Here, we perform a statistical test that will give more insight on whether an algorithm is better than the other.
%% Cell type:code id: tags:
```
python
# Formal statistics to check if these curves are statistically significant
for
i
,
player_1
in
enumerate
(
results
):
for
j
,
player_2
in
enumerate
(
results
):
if
j
>
i
:
test_result
=
scstats
.
mannwhitneyu
(
results
[
player_1
],
results
[
player_2
],
alternative
=
"
two-sided
"
)
print
(
"
Mann-Whitney U test between turns of program
'
%s
'
and of program
'
%s
'
:
"
%
(
player_1
,
player_2
),
test_result
)
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment