malak123A456/q-learning-gridworld
0
1import numpy as np
2import random
3import matplotlib.pyplot as plt
4
5# Parameters
6alpha = 0.1 # Learning rate
7gamma = 0.9 # Discount factor
8epsilon = 1.0 # Exploration rate
9epsilon_decay = 0.995
10min_epsilon = 0.01
11episodes = 1000
12grid_size = 5
13
14# Initialize Q-table
15q_table = np.zeros((grid_size, grid_size, 4)) # 4 actions: up, down, left, right
16
17# Environment definition
18start_state = (0, 0)
19goal_state = (4, 4)
20obstacles = [(2, 2), (3, 3)]
21
22# Rewards
23def get_reward(state):
24 if state == goal_state:
25 return 100
26 elif state in obstacles:
27 return -10
28 return -1
29
30# Get next state
31def get_next_state(state, action):
32 x, y = state
33 if action == 0 and x > 0: # Up
34 x -= 1
35 elif action == 1 and x < grid_size - 1: # Down
36 x += 1
37 elif action == 2 and y > 0: # Left
38 y -= 1
39 elif action == 3 and y < grid_size - 1: # Right
40 y += 1
41 return (x, y)
42
43# Train Q-learning
44for episode in range(episodes):
45 state = start_state
46 done = False
47 while not done:
48 # Choose action using epsilon-greedy
49 if random.uniform(0, 1) < epsilon:
50 action = random.randint(0, 3) # Explore
51 else:
52 action = np.argmax(q_table[state]) # Exploit
53
54 # Take action
55 next_state = get_next_state(state, action)
56 reward = get_reward(next_state)
57
58 # Update Q-value
59 best_next_action = np.argmax(q_table[next_state])
60 q_table[state][action] += alpha * (
61 reward + gamma * q_table[next_state][best_next_action] - q_table[state][action]
62 )
63
64 state = next_state
65
66 # Check if episode is done
67 if state == goal_state or state in obstacles:
68 done = True
69
70 # Decay epsilon
71 epsilon = max(min_epsilon, epsilon * epsilon_decay)
72
73# Visualize optimal policy
74policy = np.zeros((grid_size, grid_size), dtype=str)
75directions = ['↑', '↓', '←', '→']
76for i in range(grid_size):
77 for j in range(grid_size):
78 policy[i, j] = directions[np.argmax(q_table[(i, j)])]
79policy[goal_state] = 'G'
80for obs in obstacles:
81 policy[obs] = 'X'
82policy[start_state] = 'S'
83
84print("Optimal Policy:")
85print(policy)
86 