-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprototype.py
142 lines (115 loc) · 4.25 KB
/
prototype.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import numpy as np
import json
# If no arguments were passed via -i input, default to 5
grid_size = int(sys.argv[1]) if len(sys.argv) > 1 else 5
from enum import Enum
class Action(Enum):
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
class GridWorld:
def __init__(self, size=5):
self.size = size
self.reward = np.zeros((size, size))
# Set goal to bottom-right corner of the grid
self.goal = (size - 1, size - 1)
self.state = None
self.reset()
def get_reward(self, state):
if state == self.goal:
return 1
return 0
def reset(self):
self.state = (0, 0)
return self.state
def step(self, action):
row, col = self.state
if action == Action.RIGHT.value:
col = min(col + 1, self.size - 1)
elif action == Action.LEFT.value:
col = max(col - 1, 0)
elif action == Action.DOWN.value:
row = min(row + 1, self.size - 1)
elif action == Action.UP.value:
row = max(row - 1, 0)
self.state = (row, col)
reward_value = self.get_reward(self.state)
done = self.state == self.goal
return self.state, reward_value, done, {}
def render(self):
grid = [['-' for _ in range(self.size)] for _ in range(self.size)]
grid[self.state[0]][self.state[1]] = 'x'
grid[self.goal[0]][self.goal[1]] = 'G'
return json.dumps(grid)
class QTable:
def __init__(self, row, col, num_actions):
self.values = np.zeros((row, col, num_actions))
def get_best_action(self, row, col):
return np.argmax(self.values[row][col])
def get_value(self, row, col, action):
return self.values[row][col][action]
class SarsaAgent:
def __init__(self, num_actions, alpha=0.1, gamma=0.99, epsilon=0.1):
self.num_actions = num_actions
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
self.last_state = None
self.last_action = None
def set_q_table(self, q_table):
self.q_table = q_table
def choose_action(self, state):
if np.random.uniform(0, 1) < self.epsilon:
return np.random.randint(self.num_actions)
else:
row, col = state
return self.q_table.get_best_action(row, col)
def learn(self, state, action, reward, next_state, next_action):
next_row, next_col = next_state
if self.last_state is not None:
current_q = self.q_table.get_value(next_row, next_col, next_action)
last_q = self.q_table.values[self.last_state][self.last_action]
update = self.alpha * (reward + self.gamma * current_q - last_q)
self.q_table.values[self.last_state][self.last_action] += update
self.last_state = next_state
self.last_action = next_action
def reset(self):
self.last_state = None
self.last_action = None
def train_agent(env, agent, num_episodes=100, render=False):
for episode in range(1, num_episodes + 1):
state = env.reset()
action = agent.choose_action(state)
agent.reset()
done = False
while not done:
if render:
print(env.render()) # Print JSON output for debugging or interfacing
next_state, reward, done, _ = env.step(action)
next_action = agent.choose_action(next_state)
agent.learn(state, action, reward, next_state, next_action)
state = next_state
action = next_action
if render:
print(env.render()) # Final render in JSON
# Define environment and agent
env = GridWorld(grid_size)
grid_output = env.render() # Get initial state as JSON string
ns = grid_size * grid_size
na = len(list(Action))
# Create a dictionary to hold all the required information
output_data = {
'grid': json.loads(grid_output), # Convert grid JSON string back to Python list
'ns': ns,
'na': na
}
# Convert the entire output to a JSON formatted string
final_output = json.dumps(output_data, indent=4) # `indent=4` for pretty printing
# Print the final JSON output
print(final_output)
q = QTable(grid_size, grid_size, na)
agent = SarsaAgent(na)
agent.set_q_table(q)
train_agent(env, agent, 1)