-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIvsAI_RL.py
191 lines (163 loc) · 7.8 KB
/
AIvsAI_RL.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
from math import inf as infinity
import itertools
import random
import time
game_state = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
players = ['X','O']
def play_move(state, player, block_num):
if state[int((block_num-1)/3)][(block_num-1)%3] is ' ':
state[int((block_num-1)/3)][(block_num-1)%3] = player
else:
block_num = int(input("Block is not empty, ya blockhead! Choose again: "))
play_move(state, player, block_num)
def copy_game_state(state):
new_state = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
for i in range(3):
for j in range(3):
new_state[i][j] = state[i][j]
return new_state
def check_current_state(game_state):
# Check horizontals
if (game_state[0][0] == game_state[0][1] and game_state[0][1] == game_state[0][2] and game_state[0][0] is not ' '):
return game_state[0][0], "Done"
if (game_state[1][0] == game_state[1][1] and game_state[1][1] == game_state[1][2] and game_state[1][0] is not ' '):
return game_state[1][0], "Done"
if (game_state[2][0] == game_state[2][1] and game_state[2][1] == game_state[2][2] and game_state[2][0] is not ' '):
return game_state[2][0], "Done"
# Check verticals
if (game_state[0][0] == game_state[1][0] and game_state[1][0] == game_state[2][0] and game_state[0][0] is not ' '):
return game_state[0][0], "Done"
if (game_state[0][1] == game_state[1][1] and game_state[1][1] == game_state[2][1] and game_state[0][1] is not ' '):
return game_state[0][1], "Done"
if (game_state[0][2] == game_state[1][2] and game_state[1][2] == game_state[2][2] and game_state[0][2] is not ' '):
return game_state[0][2], "Done"
# Check diagonals
if (game_state[0][0] == game_state[1][1] and game_state[1][1] == game_state[2][2] and game_state[0][0] is not ' '):
return game_state[1][1], "Done"
if (game_state[2][0] == game_state[1][1] and game_state[1][1] == game_state[0][2] and game_state[2][0] is not ' '):
return game_state[1][1], "Done"
# Check if draw
draw_flag = 0
for i in range(3):
for j in range(3):
if game_state[i][j] is ' ':
draw_flag = 1
if draw_flag is 0:
return None, "Draw"
return None, "Not Done"
def print_board(game_state):
print('----------------')
print('| ' + str(game_state[0][0]) + ' || ' + str(game_state[0][1]) + ' || ' + str(game_state[0][2]) + ' |')
print('----------------')
print('| ' + str(game_state[1][0]) + ' || ' + str(game_state[1][1]) + ' || ' + str(game_state[1][2]) + ' |')
print('----------------')
print('| ' + str(game_state[2][0]) + ' || ' + str(game_state[2][1]) + ' || ' + str(game_state[2][2]) + ' |')
print('----------------')
# Initialize state values
player = ['X','O',' ']
states_dict = {}
all_possible_states = [[list(i[0:3]),list(i[3:6]),list(i[6:10])] for i in itertools.product(player, repeat = 9)]
n_states = len(all_possible_states) # 2 players, 9 spaces
n_actions = 9 # 9 spaces
state_values_for_AI_O = np.loadtxt('trained_state_values_X.txt', dtype=np.float64)
state_values_for_AI_X = np.loadtxt('trained_state_values_O.txt', dtype=np.float64)
print("n_states = %i \nn_actions = %i"%(n_states, n_actions))
# State values for AI 'O'
for i in range(n_states):
states_dict[i] = all_possible_states[i]
winner, _ = check_current_state(states_dict[i])
if winner == 'O': # AI won
state_values_for_AI_O[i] = 1
elif winner == 'X': # AI lost
state_values_for_AI_O[i] = -1
# State values for AI 'X'
for i in range(n_states):
winner, _ = check_current_state(states_dict[i])
if winner == 'O': # AI lost
state_values_for_AI_X[i] = -1
elif winner == 'X': # AI won
state_values_for_AI_X[i] = 1
def update_state_value_O(curr_state_idx, next_state_idx, learning_rate):
new_value = state_values_for_AI_O[curr_state_idx] + learning_rate*(state_values_for_AI_O[next_state_idx] - state_values_for_AI_O[curr_state_idx])
state_values_for_AI_O[curr_state_idx] = new_value
def update_state_value_X(curr_state_idx, next_state_idx, learning_rate):
new_value = state_values_for_AI_X[curr_state_idx] + learning_rate*(state_values_for_AI_X[next_state_idx] - state_values_for_AI_X[curr_state_idx])
state_values_for_AI_X[curr_state_idx] = new_value
def getBestMove(state, player, epsilon):
'''
Reinforcement Learning Algorithm
'''
moves = []
curr_state_values = []
empty_cells = []
for i in range(3):
for j in range(3):
if state[i][j] is ' ':
empty_cells.append(i*3 + (j+1))
for empty_cell in empty_cells:
moves.append(empty_cell)
new_state = copy_game_state(state)
play_move(new_state, player, empty_cell)
next_state_idx = list(states_dict.keys())[list(states_dict.values()).index(new_state)]
if player == 'X':
curr_state_values.append(state_values_for_AI_X[next_state_idx])
else:
curr_state_values.append(state_values_for_AI_O[next_state_idx])
print('Possible moves = ' + str(moves))
print('Move values = ' + str(curr_state_values))
best_move_idx = np.argmax(curr_state_values)
if np.random.uniform(0,1) <= epsilon: # Exploration
best_move = random.choice(empty_cells)
print('Agent decides to explore! Takes action = ' + str(best_move))
epsilon *= 0.99
else: #Exploitation
best_move = moves[best_move_idx]
print('Agent decides to exploit! Takes action = ' + str(best_move))
return best_move
# PLaying
#LOAD TRAINED STATE VALUES
state_values_for_AI_X = np.loadtxt('trained_state_values_X.txt', dtype=np.float64)
state_values_for_AI_O = np.loadtxt('trained_state_values_O.txt', dtype=np.float64)
learning_rate = 0.1
epsilon = 0.05
num_iterations = 50000
for iteration in range(num_iterations):
game_state = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
current_state = "Not Done"
print("\nIteration " + str(iteration) + "!")
print_board(game_state)
winner = None
current_player_idx = random.choice([0,1])
while current_state == "Not Done":
curr_state_idx = list(states_dict.keys())[list(states_dict.values()).index(game_state)]
if current_player_idx == 0: # AI_X's turn
print("\nAI X's turn!")
block_choice = getBestMove(game_state, players[current_player_idx], epsilon)
play_move(game_state ,players[current_player_idx], block_choice)
new_state_idx = list(states_dict.keys())[list(states_dict.values()).index(game_state)]
else: # AI_O's turn
print("\nAI O's turn!")
block_choice = getBestMove(game_state, players[current_player_idx], epsilon)
play_move(game_state ,players[current_player_idx], block_choice)
new_state_idx = list(states_dict.keys())[list(states_dict.values()).index(game_state)]
print_board(game_state)
#print('State value = ' + str(state_values_for_AI[new_state_idx]))
update_state_value_O(curr_state_idx, new_state_idx, learning_rate)
update_state_value_X(curr_state_idx, new_state_idx, learning_rate)
winner, current_state = check_current_state(game_state)
if winner is not None:
print(str(winner) + " won!")
else:
current_player_idx = (current_player_idx + 1)%2
if current_state is "Draw":
print("Draw!")
#time.sleep(1)
print('Training Complete!')
# Save state values for future use
np.savetxt('trained_state_values_X.txt', state_values_for_AI_X, fmt = '%.6f')
np.savetxt('trained_state_values_O.txt', state_values_for_AI_O, fmt = '%.6f')