-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcs_agent.py
29 lines (25 loc) · 977 Bytes
/
mcs_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from random import shuffle
from agent_interface import AgentInterface
from envs.oware import Oware
from random_agent import RandomAgent
from game import Game
class MCSAgent(AgentInterface):
"""
Monte Carlo Search: Evaluate each action by taking it, followed by
random plays. Action with most wins is chosen.
"""
def __init__(self):
self.__simulator = Game(RandomAgent(), RandomAgent())
def info(self):
return {"agent name": "MCS"}
def decide(self, state: Oware, actions):
shuffle(actions)
win_counter = [0] * len(actions)
counter = 0
while True:
counter += 1
for i, action in enumerate(actions):
state2 = state.successor(action)
result = self.__simulator.play(output=False, starting_state=state2)
win_counter[i] += 1 if result == [state.current_player()] else 0
yield actions[win_counter.index(max(win_counter))]