From 04e37b160175309b4b21ca27b593ef4434ae1105 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Mon, 2 Nov 2020 14:16:04 +0100 Subject: [PATCH] continued with qtable --- src/qtable.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/qtable.py b/src/qtable.py index 0ea19ec..fb1f390 100755 --- a/src/qtable.py +++ b/src/qtable.py @@ -8,7 +8,7 @@ from game import Game states_dim = 147456 # 2^12 * 6^2 actions_dim = 637 # 12+1 * (6+1)^2 -num_episodes = 1000 +num_episodes = 10000000 def find_state_qid(shutable, diced): qid = 0 @@ -42,12 +42,27 @@ def select_option(opts, qs): Q = np.zeros([states_dim, actions_dim]) +running_score = [0.0, 0.0] + for i in range(num_episodes): g = Game() + g.dice() + state_qid = find_state_qid(g.get_shutable(), g.get_diced()) while not g.is_over(): - g.dice() - state_qid = find_state_qid(g.get_shutable(), g.get_diced()) opt, opt_qid = select_option( g.get_options(), Q[state_qid, :] ) if opt: + old_score = g.get_score() g.shut(opt) - print( "%d: %d" % (i, g.get_score()) ) + g.dice() + reward = g.get_score() - old_score + new_state_qid = find_state_qid(g.get_shutable(), g.get_diced()) + lr = 0.1 + gamma = 0.99 + Q[state_qid, opt_qid] = Q[state_qid, opt_qid] + \ + lr * (reward + gamma * np.max(Q[new_state_qid, :]) - Q[state_qid, opt_qid]) + state_qid = new_state_qid + running_score[0] *= 0.999 + running_score[0] += g.get_score() + running_score[1] *= 0.999 + running_score[1] += 1.0 + print( "%d: %f" % (i, running_score[0]/running_score[1]) ) -- 2.39.5