Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Dogopher
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
Gabriel Santamaria
Dogopher
Commits
cbab3197
Unverified
Commit
cbab3197
authored
9 months ago
by
Gabriel Santamaria
Browse files
Options
Downloads
Patches
Plain Diff
Testing memory
parent
7ca6679c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
game/rules.py
+6
-6
6 additions, 6 deletions
game/rules.py
game/strategy.py
+24
-12
24 additions, 12 deletions
game/strategy.py
plots/output.gif
+0
-0
0 additions, 0 deletions
plots/output.gif
with
30 additions
and
18 deletions
game/rules.py
+
6
−
6
View file @
cbab3197
...
...
@@ -170,8 +170,8 @@ class Dodo(Rules):
@staticmethod
def
get_legal_moves
(
grid
:
HexGrid
,
ply
:
Player
)
->
list
[
Action
]:
if
(
grid
,
ply
)
in
Dodo
.
__legals
:
return
Dodo
.
__legals
[(
grid
,
ply
)]
#
if (grid, ply) in Dodo.__legals:
#
return Dodo.__legals[(grid, ply)]
direction
=
get_direction
(
ply
)
...
...
@@ -184,7 +184,7 @@ class Dodo(Rules):
# We now need to cache the results of the legals
# of this player for this grid.
Dodo
.
__legals
[(
grid
,
ply
)]
=
actions
#
Dodo.__legals[(grid, ply)] = actions
return
actions
...
...
@@ -303,11 +303,11 @@ class Dodo(Rules):
for
move
in
moves
:
pmobility
,
emobility
=
Dodo
.
__mobility
(
grid
,
ply
,
move
)
blocking
=
Dodo
.
__blocking
(
grid
,
ply
,
other
(
ply
),
move
)
#
blocking = Dodo.__blocking(grid, ply, other(ply), move)
probabilities
.
append
(
1
+
50000
*
((
emobility
+
1
)
/
((
pmobility
+
1
)))
-
5000
*
(
1
/
(
1
+
blocking
))
+
50000
*
((
50
*
emobility
+
1
)
/
((
pmobility
+
1
)))
#
- 5000 * (1 / (1 + blocking))
)
probabilities
=
np
.
array
(
probabilities
)
...
...
This diff is collapsed.
Click to expand it.
game/strategy.py
+
24
−
12
View file @
cbab3197
...
...
@@ -2,10 +2,12 @@
Strategy representation and implementation (minimax, MCTS, etc...)
"""
import
gc
from
typing
import
Optional
from
copy
import
deepcopy
from
math
import
inf
from
multiprocessing
import
Pool
,
cpu_count
from
multiprocessing
import
cpu_count
from
mpire
import
WorkerPool
as
Pool
from
numpy.random
import
randint
,
choice
from
api
import
Player
,
Action
...
...
@@ -159,8 +161,8 @@ class MCTSStrategy(Strategy):
References:
- https://www.lri.fr/~sebag/Slides/InvitedTutorial_CP12.pdf
- [1] Chaslot, Guillaume & Winands, Mark & Herik, H. & Uiterwijk, Jos & Bouzy, Bruno.
(2008).
Progressive Strategies for Monte-Carlo Tree Search.
- [1] Chaslot, Guillaume & Winands, Mark & Herik, H. & Uiterwijk, Jos & Bouzy, Bruno.
(2008).
Progressive Strategies for Monte-Carlo Tree Search.
New Mathematics and Natural Computation. 04. 343-357. 10.1142/S1793005708001094.
- [2] H. Baier and M. H. M. Winands,
"
Monte-Carlo Tree Search and minimax hybrids,
"
2013 IEEE Conference on Computational Inteligence in Games (CIG),
...
...
@@ -404,7 +406,7 @@ class MCTSStrategy(Strategy):
node
.
value
+=
result
node
=
node
.
parent
def
train
(
self
)
->
MCTSNode
:
def
__
train
(
self
)
->
MCTSNode
:
"""
Performs a training of the MCTS tree.
"""
...
...
@@ -416,6 +418,17 @@ class MCTSStrategy(Strategy):
return
root
def
train
(
self
,
simulations
):
"""
Train a given number of simulations
Args:
simulations: The number of simulations to run
"""
for
_
in
range
(
simulations
):
self
.
__train
()
return
self
.
root
def
get_action
(
self
,
grid
:
HexGrid
,
ply
:
Player
=
None
)
->
Action
:
"""
Get the action to play for a player using MCTS.
...
...
@@ -432,14 +445,12 @@ class MCTSStrategy(Strategy):
self
.
current_state
=
deepcopy
(
grid
)
self
.
legals
=
self
.
rules
.
get_legal_moves
(
grid
,
self
.
player
)
nodes
=
[]
with
Pool
(
cpu_count
()
//
4
)
as
pool
:
nodes
=
pool
.
map
(
MCTSStrategy
.
train
,
[
self
]
*
self
.
simulations
)
nworkers
=
cpu_count
()
//
2
# Number of workers we're going to use
sims_per_worker
=
self
.
simulations
//
nworkers
# We're just filtering out the list of nodes to remove
# the duplicates potentially added by the multiprocessing
seen
=
set
()
nodes
=
[
x
for
x
in
nodes
if
not
(
x
in
seen
or
seen
.
add
(
x
))]
nodes
=
[]
with
Pool
(
nworkers
)
as
pool
:
nodes
=
pool
.
map
(
self
.
train
,
[
sims_per_worker
]
*
nworkers
)
# We then need to merge every results inside our main
# process memory (we're not using a shared memory system),
...
...
@@ -464,9 +475,10 @@ class MCTSStrategy(Strategy):
self
.
root
.
childs_prior
[
index
]
=
0
self
.
root
.
childs_value
[
index
]
=
0
self
.
root
.
childs_visits
[
index
]
=
0
# Python please free up this fck node
del
self
.
root
.
children
[
action
]
del
self
.
root
gc
.
collect
()
# Python please free up these fck nodes
self
.
root
=
best_child
.
parent
if
not
best_child
.
action
in
self
.
legals
:
...
...
This diff is collapsed.
Click to expand it.
plots/output.gif
0 → 100644
+
0
−
0
View file @
cbab3197
157 KiB
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