-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ_learning.py
164 lines (135 loc) · 5.93 KB
/
Q_learning.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
#! python3
"""
Author: Long Phan, Nhut Le
Module: Q_learning.py
"""
import chess
import numpy as np
import operator
import copy
class Agent:
def __init__(self, Q_Matrix = None, lr = 0.1, gamma = 0.9, gameObject = None):
self.gameObject = gameObject
self.pathCounter = 0
# Initializing Q_Matrix: dict{board: a dictionary of actions}, an action is UCI_move:score
if Q_Matrix is None:
self.Q_Matrix = {}
else:
self.Q_Matrix = Q_Matrix
# learning rate
self.lr = lr
#gamma
self.gamma = gamma
self.scoreTable = {
'None': 0,
'q' : 9,
'r' : 5,
'n' : 3,
'b' : 3,
'p' : 1,
'k' : 1000000
}
# # this function for testing,
def play(self):
action = self.actionWithMaxQ_ValInState(self.gameObject.fen(),isMutated=False)
self.gameObject.push_uci(action)
def getActions(self, state): #return actions in the state
# if the state doesnt exist, create it.
if not state in self.Q_Matrix:
self.Q_Matrix[state] = {}
return self.Q_Matrix[state]
def swapSide(self, state):
boardStateArray1 = state.split(' ')
boardStateArray1[0] = boardStateArray1[0].swapcase()
boardStateArray2 = boardStateArray1[0].split('/')
boardStateArray2.reverse()
boardStateArray2 = [x[::-1] for x in boardStateArray2]
boardStateArray1[0] = '/'.join(boardStateArray2) # Piece placement (from White's perspective)
boardStateArray1[1] = 'w' # Active color. "w" means White moves next, "b" means Black moves next. Always set to "w"
boardStateArray1[2] = boardStateArray1[2].swapcase() # Castling availability
uppercases = ''
lowercases = ''
for c in boardStateArray1[2]:
if c.isupper():
uppercases += c
else:
lowercases += c
boardStateArray1[2] = uppercases + lowercases
if boardStateArray1[3] != '-': # En passant target square in algebraic notation
boardStateArray1[3] = self.swapLocation(boardStateArray1[3])
boardStateArray1 = ' '.join(boardStateArray1)
return boardStateArray1
def swapAction(self, uci_str):
start = uci_str[:2] # get the first 2 characters
end = uci_str[2:4] # get the 3rd and 4th characters
promotion = ''
if len(uci_str) == 5:
promotion = uci_str[4] # get the 5th character if it is there
return self.swapLocation(start) + self.swapLocation(end) + promotion
def swapLocation(self, algebraic_location_str):
boardSquares = copy.deepcopy(chess.SQUARE_NAMES)
boardSquares.reverse()
algebraic_location_str = boardSquares[chess.SQUARE_NAMES.index(algebraic_location_str)]
return algebraic_location_str
def getNextStateFrom(self, state, action):
currentBoard = chess.Board(state)
currentBoard.push_uci(action)
nextState = currentBoard.fen()
return nextState
def actionWithMaxQ_ValInState(self, state, isMutated): #return action
newState = copy.deepcopy(state)
if not self.gameObject.turn:
newState = self.swapSide(state)
virtualBoard = chess.Board(newState)
actions = self.getActions(newState)
if (not bool(actions)) or isMutated:
possibleActions = np.array([a for a in virtualBoard.legal_moves], dtype=str)
randomChoice = np.random.randint(len(possibleActions))
chosenAction = possibleActions[randomChoice]
# "f2f4" -> "f4"
piece = virtualBoard.piece_at(chess.SQUARE_NAMES.index(chosenAction[2:4]))
scoreForAction = self.scoreTable[str(piece)]
if chosenAction not in actions:
#initializing
actions[chosenAction] = scoreForAction
## mutating
else:
nextState = self.getNextStateFrom(newState, chosenAction)
self.getActions(nextState)
nextStateActions = self.Q_Matrix[nextState]
if bool(nextStateActions):
actions[chosenAction] = actions[chosenAction] + self.lr*(scoreForAction + self.gamma*max(nextStateActions.items(), key=operator.itemgetter(1))[1]-actions[chosenAction])
else:
actions[chosenAction] = actions[chosenAction] + self.lr*(scoreForAction-actions[chosenAction])
else:
chosenAction = max(actions.items(), key=operator.itemgetter(1))[0]
piece = virtualBoard.piece_at(chess.SQUARE_NAMES.index(chosenAction[2:4]))
scoreForAction = self.scoreTable[str(piece)]
nextState = self.getNextStateFrom(newState, chosenAction)
self.getActions(nextState)
nextStateActions = self.Q_Matrix[nextState]
if bool(nextStateActions):
actions[chosenAction] = actions[chosenAction] + self.lr*(scoreForAction + self.gamma*max(nextStateActions.items(), key=operator.itemgetter(1))[1]-actions[chosenAction])
else:
actions[chosenAction] = actions[chosenAction] + self.lr*(scoreForAction-actions[chosenAction])
if not self.gameObject.turn:
chosenAction = self.swapAction(chosenAction)
return chosenAction
def train(self):
# epsilon
# self.epsilon = epsilon
currentState = self.gameObject.fen()
#mutation or not
isMutated = np.random.randint(0,9) < 3
action = self.actionWithMaxQ_ValInState(currentState, isMutated=isMutated)
# if self.gameObject.turn:
# print("w")
# else:
# print("b")
# print(action)
self.pathCounter +=1
self.gameObject.push_uci(action)
if self.gameObject.is_game_over():
self.gameObject = chess.Board()
return 1, self.pathCounter
return 0, self.pathCounter