-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAI.py
332 lines (267 loc) · 9.13 KB
/
AI.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import random
import os.path
import pathlib
class AI:
def __init__(self):
"""
This initialises a new ai with a:
1) game_boards list to store
all the games played so far.
2) the ai's internal memory that
stores all boards its seen
in a dictionary.
:return:
"""
self.self_save = True
self.is_learning = True
self.game_boards_current = []
self.game_boards_memory = dict()
self.file_name = "memory.txt"
self.current_state = None
self.randomness = 1
self.certainty = 1000
self.RAND = 0
self.CERT = 1
self.rand_choice_lst = ([self.RAND]*self.randomness) + \
([self.CERT]*self.certainty)
# check if the file exists
if os.path.isfile(self.file_name):
self.load()
def move(self, board):
"""
Takes in a board and returns what index of
board to play the next move.
board: a list of 9 spaces that signals a game board.
:param board: list[]
:return: int
>>> ai = AI()
>>> board = [0, 0, 0, 0, 1, 0, 0, 0, 0]
>>> index = ai.move(board)
>>> print(index)
0
>>> len(ai.game_boards_current)
1
>>> board_better = [0, 2, 0, 0, 1, 0, 0, 0, 0]
>>> board_better_key = str(board_better)
>>> ai.game_boards_memory[board_better_key] = 5
>>> index = ai.move(board)
>>> print(index)
1
"""
# generate all possible next moves
next_moves_list = generate_next(board)
# figure out the best move
# get a random index
index = random.randint(0, len(next_moves_list)-1)
# best_move is a list that looks like:
# [index_to_play_to_get to that move, the_actual_board_of_next_move]
best_move = next_moves_list[index]
# decide if or not to choose randomly
pick = random.choice(self.rand_choice_lst)
# AI chose without random
if pick == self.CERT:
print("chose certainly")
#####
for next_move in next_moves_list:
# if the next move is better, it replaces the best move
if self.get_board_value(best_move[1]) < self.get_board_value(next_move[1]):
best_move = next_move
####
else:
print("chose randomly")
# add the next move that we are going to make to a list of all boards seen so far
# the game_boards current.
self.game_boards_current.append(best_move[1])
# update the current board state with the value of
# new move chosen
self.backtrack(self.get_board_value(best_move[1]))
self.current_state = best_move[1]
# debug
print(show_board(best_move[1]))
print("val:{}\n".format(self.get_board_value(best_move[1])))
# return the index of where to play the next move
return best_move[0]
def backtrack(self,future_move_value):
if self.is_learning:
if self.current_state != None:
# the below line of code is key
# update the current board's state with the value
# of the future move.
# What you might want to think about...
# the value of a board is kept in dictionary self.game_boards_memory[]
# the string of the board is used as a key to index that dictionary
# "future_move_value" is a numeric value of the future move
# "current_state" is an array that represent the boards current state.
#####
self.game_boards_memory[str(self.current_state)] += 0.99*(future_move_value-self.get_board_value(self.current_state))
#####
# optimizies training time
if self.self_save:
self.save()
def get_board_value(self, board):
"""
returns the value of that board in the computers memory.
board: a list of 9 spaces that signals a game board.
:param board: list[]
:return: int
>>> ai = AI()
>>> board = [0,0,0]
>>> board_key = str(board)
>>> ai.get_board_value(board_key)
0
>>> ai.game_boards_memory[board_key] = 5
>>> ai.get_board_value(board_key)
5
"""
# use string representation as the key of the dictionary
board_key = str(board)
if board_key in self.game_boards_memory:
value = self.game_boards_memory.get(board_key)
else:
value = 0.5
self.game_boards_memory[board_key] = value
return value
def has_lost(self):
self.backtrack(0)
self.current_state = None
def has_drawn(self):
self.backtrack(0.5)
self.current_state = None
def has_won(self):
self.backtrack(1)
self.current_state = None
def stop_learning(self):
self.is_learning = False
print("Stopped learning")
def start_learning(self):
self.is_learning = True
print("Started learning")
def stop_self_saving(self):
self.self_save = False
print("Stopped self saving")
def start_self_saving(self):
self.self_save = True
print("Started self saving")
def save(self):
"""
Save the contents of the database into a file.
:param file_name: str
:return: number of entries saved
"""
file_name = self.file_name
entries = 0
target_file = open(file_name, 'w')
for key in self.game_boards_memory:
target_file.write(str(key) + ":" + str(self.game_boards_memory[key]) + "\n")
entries += 1
target_file.close()
def load(self):
"""
Load the contents of a file into its database (dict)
:param file_name: str
:return: number of entries loaded
"""
file_name = self.file_name
entries = 0
target_file = open(file_name, 'r')
for line in target_file:
temp_list = line.strip("\n").split(":")
if len(temp_list) == 2:
self.game_boards_memory[temp_list[0]] = float(temp_list[1])
entries += 1
return entries
def get_memory(self):
"""
Return a string representation of the ai's memory
:return:
"""
s = ""
for board_key in self.game_boards_memory:
s += (str(board_key) + "\n" + str(self.game_boards_memory[board_key]) + "\n")
return s
def generate_next(board):
"""
Takes a board and returns a list of
all possible moves that can be made by the ai on that board.
It also includes the move needed to get to that board.
The AI, see's its own moves on the board as 2's , other people's
moves on the board as 1's and no moves on the board as 0's.
board: a list of 9 spaces that signals a game board.
:param board: list[]
:return: list[list[int(index of position to play), board]]
# test out code
>>> board = [0, 0, 0, 0, 1, 0, 0, 0, 0]
>>> next_moves_list = generate_next(board)
>>> for board in next_moves_list: print(str(board[0]) + "\\n" + show_board(board[1]))
0
200
010
000
<BLANKLINE>
1
020
010
000
<BLANKLINE>
2
002
010
000
<BLANKLINE>
3
000
210
000
<BLANKLINE>
5
000
012
000
<BLANKLINE>
6
000
010
200
<BLANKLINE>
7
000
010
020
<BLANKLINE>
8
000
010
002
<BLANKLINE>
"""
#######
# store next moves here
next_moves_list = []
# loop through all possible moves
for i in range(len(board)):
if board[i] == 0:
# generate new board and add it to next_moves_list.
new_board = board[:]
new_board[i] = 2 # the ai's symbol is represented by a 2
next_moves_list.append([i, new_board])
return next_moves_list
########
def show_board(board):
"""
Takes a board and prints out it's status.
:param board: a list of length 9 that signifies a game board
:return: null
# test out the code.
>>> board = [0, 0, 0, 0, 1, 0, 0, 0, 0]
>>> show_board(board)
000
010
000
"""
s = ""
# loop through the board
for i in range(9):
s += str(board[i])
if (((i+1) % 3) == 0) and (i != 0):
s += "\n"
return s