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
import random
q_table = {}
states = ["start", "middle", "end"]
actions = ["left", "right"]
for s in states:
for a in actions:
q_table[(s, a)] = 0.0
alpha = 0.1
gamma = 0.9
def update_q(state, action, reward, next_state):
old_q = q_table[(state, action)]
max_next = max(q_table[(next_state, a)] for a in actions)
new_q = old_q + alpha * (reward + gamma * max_next - old_q)
q_table[(state, action)] = new_q
return new_q
new_val = update_q("start", "right", 1, "middle")
print(f"Updated Q(start, right): {new_val:.3f}")