-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsarsa.py
More file actions
81 lines (57 loc) · 1.92 KB
/
Copy pathsarsa.py
File metadata and controls
81 lines (57 loc) · 1.92 KB
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
import game
import numpy as np
import random
from collections import Counter
import pandas as pd
import matplotlib.pyplot as plt
import time
import math
N0 = 100
ITERATIONS = 100000
hit = True
stick = False
actions = [hit, stick]
lmd = 0.8
Q_matrix = np.zeros((10, 21, 2))
N_matrix = np.zeros((10, 21, 2))
def Q(state, action):
return Q_matrix[state.dealer-1][state.player-1][int(action)]
def N(state, action):
return N_matrix[state.dealer-1][state.player-1][int(action)]
def allQ(state):
return Q_matrix[state.dealer-1][state.player-1]
def allN(state):
return N_matrix[state.dealer-1][state.player-1]
def allE(state):
return E_matrix[state.dealer-1][state.player-1]
def V(q):
return np.max(q, axis=2)
def epsilon_greedy(q, n):
epsilon = N0 / (N0 + sum(n))
if np.random.random() < epsilon:
return random.choice(actions)
else:
return bool(np.argmax(q))
if __name__ == "__main__":
for k in range(1, ITERATIONS):
terminal = False
E_matrix = np.zeros_like(Q_matrix)
state = game.initialise_state()
action = epsilon_greedy(allQ(state), allN(state))
while not terminal:
next_state, reward = game.step(state, action)
terminal = state.terminal
if not terminal:
next_action = epsilon_greedy(allQ(state), allN(state))
delta = reward + Q(next_state, next_action) - Q(state, action)
else:
delta = reward - Q(state, action)
allE(state)[int(action)] += 1
allN(state)[int(action)] += 1
alpha = 1/N(state,action)
Q_matrix += alpha * delta * E_matrix
E_matrix *= lmd
if not terminal:
state = next_state
action = next_action
game.visualise(V(Q_matrix))