Connect Four using Q Table Reinforcement Learning
up vote
-1
down vote
favorite
I'm trying to make a Q table based reinforcement learning algorithm play Connect Four against a Neural Network Q table. It seems to work, but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time.
I got the connect four code from this github page and modified it into a class with a couple of extra functions such that give a list of valid moves and such.
Here is my connect four class
And here is my Q table code
def trainQ(nRepetitions, learningRate, epsilonDecayFactor):
# Initialize Q
Qred = {}
Qyellow = {}
players = ['R', 'Y']
steps =
epsilon = 1
numTimesRWon = 0
numTimesYWon = 0
for step in range(nRepetitions):
game = Game()
sOld = None
aOld = None
stepCount = 0
epsilon = epsilon * epsilonDecayFactor
while not game.isDone():
player = players[stepCount % 2 == 0]
# Select next action.
if player == 'R':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qred.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qred[game.boardTup(sOld, aOld)] = Qred.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qred.get(game.boardTup(game.board, a), 0) - Qred.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if player == 'Y':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qyellow.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if True:#epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qyellow[game.boardTup(sOld, aOld)] = Qyellow.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qyellow.get(game.boardTup(game.board, a), 0) - Qyellow.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if game.checkForWin():
# Update Qold with TDerror (1-Qold)
if player == 'R':
numTimesRWon += 1
Qred[game.boardTup(sOld, aOld)] = 1 + Qred.get(game.boardTup(sOld, aOld), 0)
if player == 'Y':
numTimesYWon += 1
Qyellow[game.boardTup(sOld, aOld)] = 1 + Qyellow.get(game.boardTup(sOld, aOld), 0)
steps.append(stepCount)
print("R won: ", numTimesRWon)
print("Y won: ", numTimesYWon)
return Qred, Qyellow, steps
python reinventing-the-wheel connect-four
New contributor
add a comment |
up vote
-1
down vote
favorite
I'm trying to make a Q table based reinforcement learning algorithm play Connect Four against a Neural Network Q table. It seems to work, but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time.
I got the connect four code from this github page and modified it into a class with a couple of extra functions such that give a list of valid moves and such.
Here is my connect four class
And here is my Q table code
def trainQ(nRepetitions, learningRate, epsilonDecayFactor):
# Initialize Q
Qred = {}
Qyellow = {}
players = ['R', 'Y']
steps =
epsilon = 1
numTimesRWon = 0
numTimesYWon = 0
for step in range(nRepetitions):
game = Game()
sOld = None
aOld = None
stepCount = 0
epsilon = epsilon * epsilonDecayFactor
while not game.isDone():
player = players[stepCount % 2 == 0]
# Select next action.
if player == 'R':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qred.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qred[game.boardTup(sOld, aOld)] = Qred.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qred.get(game.boardTup(game.board, a), 0) - Qred.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if player == 'Y':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qyellow.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if True:#epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qyellow[game.boardTup(sOld, aOld)] = Qyellow.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qyellow.get(game.boardTup(game.board, a), 0) - Qyellow.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if game.checkForWin():
# Update Qold with TDerror (1-Qold)
if player == 'R':
numTimesRWon += 1
Qred[game.boardTup(sOld, aOld)] = 1 + Qred.get(game.boardTup(sOld, aOld), 0)
if player == 'Y':
numTimesYWon += 1
Qyellow[game.boardTup(sOld, aOld)] = 1 + Qyellow.get(game.boardTup(sOld, aOld), 0)
steps.append(stepCount)
print("R won: ", numTimesRWon)
print("Y won: ", numTimesYWon)
return Qred, Qyellow, steps
python reinventing-the-wheel connect-four
New contributor
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to make a Q table based reinforcement learning algorithm play Connect Four against a Neural Network Q table. It seems to work, but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time.
I got the connect four code from this github page and modified it into a class with a couple of extra functions such that give a list of valid moves and such.
Here is my connect four class
And here is my Q table code
def trainQ(nRepetitions, learningRate, epsilonDecayFactor):
# Initialize Q
Qred = {}
Qyellow = {}
players = ['R', 'Y']
steps =
epsilon = 1
numTimesRWon = 0
numTimesYWon = 0
for step in range(nRepetitions):
game = Game()
sOld = None
aOld = None
stepCount = 0
epsilon = epsilon * epsilonDecayFactor
while not game.isDone():
player = players[stepCount % 2 == 0]
# Select next action.
if player == 'R':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qred.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qred[game.boardTup(sOld, aOld)] = Qred.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qred.get(game.boardTup(game.board, a), 0) - Qred.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if player == 'Y':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qyellow.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if True:#epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qyellow[game.boardTup(sOld, aOld)] = Qyellow.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qyellow.get(game.boardTup(game.board, a), 0) - Qyellow.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if game.checkForWin():
# Update Qold with TDerror (1-Qold)
if player == 'R':
numTimesRWon += 1
Qred[game.boardTup(sOld, aOld)] = 1 + Qred.get(game.boardTup(sOld, aOld), 0)
if player == 'Y':
numTimesYWon += 1
Qyellow[game.boardTup(sOld, aOld)] = 1 + Qyellow.get(game.boardTup(sOld, aOld), 0)
steps.append(stepCount)
print("R won: ", numTimesRWon)
print("Y won: ", numTimesYWon)
return Qred, Qyellow, steps
python reinventing-the-wheel connect-four
New contributor
I'm trying to make a Q table based reinforcement learning algorithm play Connect Four against a Neural Network Q table. It seems to work, but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time.
I got the connect four code from this github page and modified it into a class with a couple of extra functions such that give a list of valid moves and such.
Here is my connect four class
And here is my Q table code
def trainQ(nRepetitions, learningRate, epsilonDecayFactor):
# Initialize Q
Qred = {}
Qyellow = {}
players = ['R', 'Y']
steps =
epsilon = 1
numTimesRWon = 0
numTimesYWon = 0
for step in range(nRepetitions):
game = Game()
sOld = None
aOld = None
stepCount = 0
epsilon = epsilon * epsilonDecayFactor
while not game.isDone():
player = players[stepCount % 2 == 0]
# Select next action.
if player == 'R':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qred.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qred[game.boardTup(sOld, aOld)] = Qred.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qred.get(game.boardTup(game.board, a), 0) - Qred.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if player == 'Y':
moves = game.validMoves()
# List of Q values for valid moves
moveQ = [Qyellow.get(game.boardTup(game.board, move), 0) for move in moves]
ranNum = np.random.random()
if True:#epsilon > ranNum:
a = moves[np.random.choice(len(moveQ))]
else:
a = moves[np.argmax(np.array(moveQ))]
# If not first step, update Qold with TD error (1 + Qnew - Qold)
if sOld != None:
Qyellow[game.boardTup(sOld, aOld)] = Qyellow.get(game.boardTup(sOld, aOld), 0) + learningRate *
(1 + Qyellow.get(game.boardTup(game.board, a), 0) - Qyellow.get(game.boardTup(sOld, aOld), 0))
# Shift current board and action to old ones.
sOld, aOld = game.board, a
# Apply action to get new board.
game.insert(a, player)
stepCount += 1
if game.checkForWin():
# Update Qold with TDerror (1-Qold)
if player == 'R':
numTimesRWon += 1
Qred[game.boardTup(sOld, aOld)] = 1 + Qred.get(game.boardTup(sOld, aOld), 0)
if player == 'Y':
numTimesYWon += 1
Qyellow[game.boardTup(sOld, aOld)] = 1 + Qyellow.get(game.boardTup(sOld, aOld), 0)
steps.append(stepCount)
print("R won: ", numTimesRWon)
print("Y won: ", numTimesYWon)
return Qred, Qyellow, steps
python reinventing-the-wheel connect-four
python reinventing-the-wheel connect-four
New contributor
New contributor
New contributor
asked 1 hour ago
Cepheid
1
1
New contributor
New contributor
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago
add a comment |
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cepheid is a new contributor. Be nice, and check out our Code of Conduct.
Cepheid is a new contributor. Be nice, and check out our Code of Conduct.
Cepheid is a new contributor. Be nice, and check out our Code of Conduct.
Cepheid is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208971%2fconnect-four-using-q-table-reinforcement-learning%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"but when I try to use the Q table against a opponent that randomly picks moves, it loses almost every time" — then it sounds like your code is not working correctly as intended, and thus is not ready for review. See the help center.
– 200_success
58 mins ago
Welcome on Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Calak
38 mins ago