forked from rlcode/reinforcement-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
237 lines (181 loc) · 6.9 KB
/
environment.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import time
import numpy as np
import tkinter as tk
from PIL import ImageTk, Image
UNIT = 50 # pixels
HEIGHT = 10 # grid height
WIDTH = 10 # grid width
# np.random.seed(1)
class Env(tk.Tk):
def __init__(self):
super(Env, self).__init__()
self.action_space = ['u', 'd', 'l', 'r']
self.action_size = len(self.action_space)
self.title('Policy Gradient')
self.geometry('{0}x{1}'.format(HEIGHT * UNIT, HEIGHT * UNIT))
self.build_graphic()
self.counter = 0
def build_graphic(self):
self.canvas = tk.Canvas(self, bg='white',
height=HEIGHT * UNIT,
width=WIDTH * UNIT)
# create grids
for c in range(0, WIDTH * UNIT, UNIT): # 0~400 by 80
x0, y0, x1, y1 = c, 0, c, HEIGHT * UNIT
self.canvas.create_line(x0, y0, x1, y1)
for r in range(0, HEIGHT * UNIT, UNIT): # 0~400 by 80
x0, y0, x1, y1 = 0, r, HEIGHT * UNIT, r
self.canvas.create_line(x0, y0, x1, y1)
# image_load
self.rectangle_image = ImageTk.PhotoImage(
Image.open("../resources/rectangle.png").resize((30, 30), Image.ANTIALIAS))
self.fire_image = ImageTk.PhotoImage(Image.open("../resources/triangle.png").resize((30, 30)))
self.fish_image = ImageTk.PhotoImage(Image.open("../resources/circle.png").resize((30, 30)))
self.rewards = list()
self.goal = list()
# obstacle
self.set_reward([2, 7], -1)
self.set_reward([3, 2], -1)
self.set_reward([2, 5], -1)
self.set_reward([4, 9], -1)
self.set_reward([5, 7], -1)
self.set_reward([6, 4], -1)
self.set_reward([7, 8], -1)
self.set_reward([8, 3], -1)
self.set_reward([9, 1], -1)
#
#
# #goal
self.set_reward([9, 9], 5)
# add image to canvas
self.rectangle = self.canvas.create_image(UNIT/2, UNIT/2, image=self.rectangle_image)
# pack all`
self.canvas.pack()
def reset_reward(self):
for reward in self.rewards:
self.canvas.delete(reward['figure'])
self.rewards.clear()
self.goal.clear()
# obstacle
self.set_reward([2, 7], -1)
self.set_reward([3, 2], -1)
self.set_reward([2, 5], -1)
self.set_reward([4, 9], -1)
self.set_reward([5, 7], -1)
self.set_reward([6, 4], -1)
self.set_reward([7, 8], -1)
self.set_reward([8, 3], -1)
self.set_reward([9, 1], -1)
#
#
# #goal
self.set_reward([9, 9], 5)
def set_reward(self, state, reward):
state = [int(state[0]), int(state[1])]
temp = {}
if reward > 0:
temp['reward'] = reward
temp['figure'] = self.canvas.create_image((UNIT * state[0]) + UNIT/2, (UNIT * state[1]) + UNIT/2,
image=self.fish_image)
self.goal.append(temp['figure'])
elif reward < 0:
temp['reward'] = reward
temp['figure'] = self.canvas.create_image((UNIT * state[0]) + UNIT/2, (UNIT * state[1]) + UNIT/2,
image=self.fire_image)
temp['coords'] = self.canvas.coords(temp['figure'])
temp['state'] = state
self.rewards.append(temp)
# new methods
def check_if_reward(self, state):
check_list = dict()
check_list['if_goal'] = False
rewards = 0
for reward in self.rewards:
if reward['state'] == state:
rewards += reward['reward']
if reward['reward'] == 5:
check_list['if_goal'] = True
check_list['rewards'] = rewards
return check_list
def coords_to_state(self, coords):
x = int((coords[0] - 50) / 100)
y = int((coords[1] - 50) / 100)
return [x, y]
def reset(self):
self.update()
time.sleep(0.5)
self.canvas.delete(self.rectangle)
self.rectangle = self.canvas.create_image(UNIT/2, UNIT/2, image=self.rectangle_image)
# return observation
self.reset_reward()
return self.get_state()
def step(self, action):
self.counter += 1
self.render()
next_coords = self.move(self.rectangle, action)
if self.counter % 2 == 1:
self.rewards = self.move_rewards()
check = self.check_if_reward(self.coords_to_state(next_coords))
done = check['if_goal']
reward = check['rewards']
s_ = self.get_state()
return s_, reward, done
def get_state(self):
agent_location = self.coords_to_state(self.canvas.coords(self.rectangle))
agent_x = agent_location[0]
agent_y = agent_location[1]
locations = list()
locations.append(agent_x)
locations.append(agent_y)
for reward in self.rewards:
reward_location = reward['state']
locations.append(agent_x - reward_location[0])
locations.append(agent_y - reward_location[1])
return locations
def move_rewards(self):
new_rewards = []
for temp in self.rewards:
if temp['reward'] == 10:
new_rewards.append(temp)
continue
temp['coords'] = self.move_const(temp['figure'])
temp['state'] = self.coords_to_state(temp['coords'])
new_rewards.append(temp)
return new_rewards
def move_const(self, target):
s = self.canvas.coords(target)
base_action = np.array([0, 0])
if s[0] < (WIDTH - 1) * UNIT:
base_action[0] += UNIT
else:
base_action[0] = -(WIDTH - 1) * UNIT
# if action == 4 # move _none
if target is not self.rectangle and s == [(WIDTH - 1) * UNIT, (HEIGHT - 1) * UNIT]:
base_action = np.array([0, 0])
self.canvas.move(target, base_action[0], base_action[1])
s_ = self.canvas.coords(target)
return s_
def move(self, target, action):
s = self.canvas.coords(target)
base_action = np.array([0, 0])
if action == 0: # up
if s[1] > UNIT:
base_action[1] -= UNIT
elif action == 1: # down
if s[1] < (HEIGHT - 1) * UNIT:
base_action[1] += UNIT
elif action == 2: # right
if s[0] < (WIDTH - 1) * UNIT:
base_action[0] += UNIT
elif action == 3: # left
if s[0] > UNIT:
base_action[0] -= UNIT
# if action == 4 # move _none
if target is not self.rectangle and s == [(WIDTH - 1) * UNIT, (HEIGHT - 1) * UNIT]:
base_action = np.array([0, 0])
self.canvas.move(target, base_action[0], base_action[1])
s_ = self.canvas.coords(target)
return s_
def render(self):
time.sleep(0.1)
self.update()