-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (137 loc) · 5.34 KB
/
main.py
File metadata and controls
151 lines (137 loc) · 5.34 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import numpy as np
import domain_mcst as dm
num_rollouts = 100
game_type = int(input("Tree Games:\n1. Optimal Cat vs Optimal Mouse\n2. Optimal Cat vs Sub-Optimal "
"Mouse\n3. Sub-Optimal Cat vs Optimal Mouse\n4. Sub-Optimal Cat vs Sub-Optimal Mouse\nNN+Tree "
"Games: (Size = 6)\n5. Optimal Cat vs Optimal Mouse\n6. Optimal Cat vs Sub-Optimal Mouse "
"\n7. Optimal Cat vs Interactive Mouse\n8. Interactive Cat vs Optimal Mouse\n\nEnter Choice: "))
if game_type in [5, 6]:
dm.SIZE = 6
else:
SIZE = int(input("Select a grid size: 6, 7, 8, 9, 10\nChoice: "))
dm.SIZE = SIZE
state = dm.Node(dm.make_grid(), dm.CAT)
print("Initial State:")
print(dm.state_string(state.grid))
print("Performing initial rollouts...")
for r in range(num_rollouts * 10):
dm.rollouts_visited = {}
dm.rollout(state)
def cat_vs_mouse(state, gametype):
moves = 0
curr_state = state
dm.explored = {}
number_of_nodes = 0
strategies = {1: (dm.exploit, dm.exploit),
2: (dm.exploit, dm.random_choice),
3: (dm.random_choice, dm.exploit),
4: (dm.random_choice, dm.random_choice),
5: (dm.exploit, dm.exploit),
6: (dm.exploit, dm.random_choice)}
cat_strategy, mouse_strategy = strategies[gametype]
flag = strategies[gametype] != 4
while not dm.is_leaf(curr_state) and moves <= 100 and curr_state.children() != []:
if flag:
dm.rollouts = []
if gametype in [5, 6]:
nn = True
else:
nn = False
for r in range(num_rollouts):
dm.rollouts_visited = {}
dm.rollout(curr_state, nn)
if moves > 10 and gametype == 5:
cat_strategy, mouse_strategy = dm.nn_exploit, dm.nn_exploit
elif moves > 10 and gametype == 6:
cat_strategy = dm.nn_exploit
if curr_state.turn == dm.CAT:
print("Cat's Move")
curr_state = cat_strategy(curr_state)
else:
print("Mouse' Move")
curr_state = mouse_strategy(curr_state)
moves += 1
dm.explored[tuple((curr_state.cat_pos, curr_state.mouse_pos))] = curr_state.score_estimate
print(dm.state_string(curr_state.grid))
number_of_nodes += sum(dm.rollouts)
print("GAME OVER")
print("Moves: ", moves)
sign, dist = 0, 0
if dm.winner(curr_state) == dm.CAT:
print("Cat Wins")
sign = 1
dist = dm.getdist(state.cat_pos, state.mouse_pos)
elif dm.winner(curr_state) == dm.MOUSE:
print("Mouse Wins")
sign = -1
dist = dm.getdist(state.mouse_pos, tuple(np.argwhere(state.grid[dm.HOLE] == 1)[0]))
print("Performance: ", (sign * dist) / moves)
return (sign * dist) / moves, number_of_nodes
def interactive_mouse(state):
moves = 0
curr_state = state
while not dm.is_leaf(curr_state):
for r in range(num_rollouts):
dm.rollouts_visited = {}
dm.rollout(curr_state, nn=True)
if curr_state.turn == dm.CAT:
print("Cat's Move")
curr_state = dm.exploit(curr_state)
else:
print("Mouse' Move")
print("Valid Actions: ", dm.get_actions(curr_state))
action = int(input("Enter index of the action: "))
curr_state = curr_state.children()[action]
moves += 1
dm.explored[tuple((curr_state.cat_pos, curr_state.mouse_pos))] = curr_state.score_estimate
print(dm.state_string(curr_state.grid))
print("GAME OVER")
print("Moves: ", moves)
sign, dist = 0, 0
if dm.winner(curr_state) == dm.CAT:
print("Cat Wins")
sign = 1
dist = dm.getdist(state.cat_pos, state.mouse_pos)
elif dm.winner(curr_state) == dm.MOUSE:
print("Mouse Wins")
sign = -1
dist = dm.getdist(state.mouse_pos, tuple(np.argwhere(state.grid[dm.HOLE] == 1)[0]))
print("Performance: ", (sign * dist) / moves)
def interactive_cat(state):
moves = 0
curr_state = state
while not dm.is_leaf(curr_state):
for r in range(num_rollouts):
dm.rollouts_visited = {}
dm.rollout(curr_state, nn=True)
if curr_state.turn == dm.CAT:
print("Cat's Move")
print("Valid Actions: ", dm.get_actions(curr_state))
action = int(input("Enter index of the action: "))
curr_state = curr_state.children()[action]
else:
print("Mouse' Move")
curr_state = dm.exploit(curr_state)
moves += 1
dm.explored[tuple((curr_state.cat_pos, curr_state.mouse_pos))] = curr_state.score_estimate
print(dm.state_string(curr_state.grid))
print("GAME OVER")
print("Moves: ", moves)
sign, dist = 0, 0
if dm.winner(curr_state) == dm.CAT:
print("Cat Wins")
sign = 1
dist = dm.getdist(state.cat_pos, state.mouse_pos)
elif dm.winner(curr_state) == dm.MOUSE:
print("Mouse Wins")
sign = -1
dist = dm.getdist(state.mouse_pos, tuple(np.argwhere(state.grid[dm.HOLE] == 1)[0]))
print("Performance: ", (sign * dist) / moves)
if game_type in [1, 2, 3, 4, 5, 6]:
cat_vs_mouse(state, game_type)
elif game_type == 7:
interactive_mouse(state)
elif game_type == 8:
interactive_cat(state)
else:
print("Invalid Input")