Skip to content

Commit

Permalink
add parameter to run the algorithm for a specified amount of time (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrscyv committed May 24, 2021
1 parent dd56e94 commit 58771d1
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions mctspy/tree/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
class MonteCarloTreeSearch(object):

def __init__(self, node):
Expand All @@ -9,22 +10,34 @@ def __init__(self, node):
"""
self.root = node

def best_action(self, simulations_number):
def best_action(self, simulations_number=None, total_simulation_seconds=None):
"""
Parameters
----------
simulations_number : int
number of simulations performed to get the best action
total_simulation_seconds : float
Amount of time the algorithm has to run. Specified in seconds
Returns
-------
"""
for _ in range(0, simulations_number):
v = self._tree_policy()
reward = v.rollout()
v.backpropagate(reward)

if simulations_number is None :
assert(total_simulation_seconds is not None)
end_time = time.time() + total_simulation_seconds
while time.time() < end_time:
v = self._tree_policy()
reward = v.rollout()
v.backpropagate(reward)
else :
for _ in range(0, simulations_number):
v = self._tree_policy()
reward = v.rollout()
v.backpropagate(reward)
# to select best child go for exploitation only
return self.root.best_child(c_param=0.)

Expand Down

0 comments on commit 58771d1

Please sign in to comment.