forked from ofrendo/qwixx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqwixx_agent_mcts.py
More file actions
72 lines (49 loc) · 2.33 KB
/
Copy pathqwixx_agent_mcts.py
File metadata and controls
72 lines (49 loc) · 2.33 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from gym_qwixx.envs.qwixx_env import QwixxEnv
from qwixx_agent_random import QwixxAgentRandom
from qwixx_game_performer import QwixxGamePerformer
import random
# Monte carlo tree search
class QwixxAgentMCTS():
def __init__(self, playerIndex):
#print("Init Qwixx agent random, playerIndex=" + str(playerIndex))
self.playerIndex = playerIndex
def get_next_action(self, env, diceThrow, availableActionsParams):
# For each action:
# perform that action, then perform the rest of the game randomly
actionValues = [-100] * (len(availableActionsParams)) # -100 because must be below -20
for actionIndex in range(len(availableActionsParams)):
# Clone env
envClone = env.clone()
availableActionsPerPlayer = []
chosenActionPerPlayer = []
# Assume other players collect actions this round randomly
for playerIndex in range(envClone.numberPlayers):
availableActions = envClone.get_available_actions(diceThrow, playerIndex)
availableActionsPerPlayer.append(availableActions)
if playerIndex != self.playerIndex:
agent = QwixxAgentRandom(playerIndex)
chosenAction = agent.get_next_action(envClone, diceThrow, availableActions)
chosenActionPerPlayer.append(chosenAction)
else:
chosenActionPerPlayer.append(availableActionsParams[actionIndex])
# Apply actions
gamePerformer = QwixxGamePerformer(envClone.numberPlayers, envClone, [QwixxAgentRandom(playerIndex), QwixxAgentRandom(playerIndex)])
gamePerformer.apply_action_tuple_per_player(diceThrow, chosenActionPerPlayer, availableActionsPerPlayer)
if envClone.is_game_over() == False:
envClone.next_round()
# From then on perform rest of the game randomly
result = gamePerformer.perform_complete_game()
actionValues[actionIndex] = result[self.playerIndex]
chosenActionIndex = self.argmax(actionValues)
#print("actionValues:")
#display(actionValues)
#print("chosenActionIndex: " + str(chosenActionIndex))
return availableActionsParams[chosenActionIndex]
def argmax(self, actionValues):
currentMax = -100
currentIndex = -1
for actionIndex in range(len(actionValues)):
if actionValues[actionIndex] > currentMax:
currentMax = actionValues[actionIndex]
currentIndex = actionIndex
return currentIndex