Skip to content
Snippets Groups Projects
Unverified Commit cbab3197 authored by Gabriel Santamaria's avatar Gabriel Santamaria
Browse files

Testing memory

parent 7ca6679c
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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:
......
plots/output.gif

157 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment