-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnvironment.py
160 lines (134 loc) · 5.22 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
import numpy as np
import time
import sys
from random import randint
if sys.version_info.major == 2:
import Tkinter as tk
else:
import tkinter as tk
UNIT = 40 # pixels
ENV_H = 5 # grid height
ENV_W = 5 # grid width
class Environment(tk.Tk, object):
def __init__(self):
super(Environment, self).__init__()
self.action_space = ['u', 'd', 'l', 'r','s']
self.n_actions = len(self.action_space)
self.title('Environment')
self.geometry('{0}x{1}'.format(ENV_H * UNIT, ENV_H * UNIT))
self._build_maze()
self.step_counter = 0
def _build_maze(self):
self.canvas = tk.Canvas(self, bg='blue',
height=ENV_H * UNIT,
width=ENV_W * UNIT)
# create grids
for c in range(0, ENV_W * UNIT, UNIT):
x0, y0, x1, y1 = c, 0, c, ENV_H * UNIT
self.canvas.create_line(x0, y0, x1, y1)
for r in range(0, ENV_H * UNIT, UNIT):
x0, y0, x1, y1 = 0, r, ENV_H * UNIT, r
self.canvas.create_line(x0, y0, x1, y1)
# create origin
origin = np.array([UNIT/2, UNIT/2])
# hell
hell1_center = origin + np.array([UNIT , UNIT])
self.hell1 = self.canvas.create_rectangle(
hell1_center[0] - (UNIT * 3 / 8), hell1_center[1] - (UNIT * 3 / 8),
hell1_center[0] + (UNIT * 3 / 8), hell1_center[1] + (UNIT * 3 / 8),
fill='red')
# create food oval
oval_center = origin + UNIT * 5
self.oval = self.canvas.create_oval(
oval_center[0] - (UNIT * 3 / 8), oval_center[1] - (UNIT * 3 / 8),
oval_center[0] + (UNIT * 3 / 8), oval_center[1] + (UNIT * 3 / 8),
fill='yellow')
# create agent rect
self.rect = self.canvas.create_rectangle(
origin[0] - (UNIT * 3 / 8), origin[1] - (UNIT * 3 / 8),
origin[0] + (UNIT * 3 / 8), origin[1] + (UNIT * 3 / 8),
fill='black')
# pack all
self.canvas.pack()
def reset(self):
self.update()
time.sleep(0.01)
self.canvas.delete(self.rect)
origin = np.array([UNIT/2, UNIT/2])
self.rect = self.canvas.create_rectangle(
origin[0] - (UNIT * 3 / 8), origin[1] - (UNIT * 3 / 8),
origin[0] + (UNIT * 3 / 8), origin[1] + (UNIT * 3 / 8),
fill='black')
self.move_food()
self.move_hell()
while self.canvas.coords(self.oval) == self.canvas.coords(self.hell1):
self.move_food()
self.move_hell()
# return observation
return np.array(self.canvas.coords(self.rect)[0:2] + self.canvas.coords(self.oval)[0:2] + self.canvas.coords(self.hell1)[0:2])
def step(self, action):
s = self.canvas.coords(self.rect)
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] < (ENV_H - 1) * UNIT:
base_action[1] += UNIT
elif action == 2: # right
if s[0] < (ENV_W - 1) * UNIT:
base_action[0] += UNIT
elif action == 3: # left
if s[0] > UNIT:
base_action[0] -= UNIT
self.canvas.move(self.rect, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect) # next state
state = self.canvas.coords(self.rect)[0:2] + self.canvas.coords(self.oval)[0:2] + self.canvas.coords(self.hell1)[0:2]
# reward function
if s_ == self.canvas.coords(self.oval):
reward = 100
done = False
elif s_ == self.canvas.coords(self.hell1):
reward = -20
done = False
else:
reward = -1
done = False
if self.step_counter == 25:
done = True
self.step_counter = 0
self.step_counter += 1
return np.array(state), reward, done
def render(self):
time.sleep(0.01)
self.update()
def move_food(self):
self.canvas.delete(self.oval)
origin = np.array([UNIT/2, UNIT/2])
oval_center = origin + np.array([UNIT * randint(1, ENV_W - 2), UNIT * randint(1, ENV_H - 2)])
self.oval = self.canvas.create_oval(
oval_center[0] - (UNIT * 3 / 8), oval_center[1] - (UNIT * 3 / 8),
oval_center[0] + (UNIT * 3 / 8), oval_center[1] + (UNIT * 3 / 8),
fill='yellow')
def move_hell(self):
self.canvas.delete(self.hell1)
origin = np.array([UNIT/2, UNIT/2])
hell1_center = origin + np.array([UNIT * randint(1, ENV_W - 2), UNIT * randint(1, ENV_H - 2)])
self.hell1 = self.canvas.create_rectangle(
hell1_center[0] - (UNIT * 3 / 8), hell1_center[1] - (UNIT * 3 / 8),
hell1_center[0] + (UNIT * 3 / 8), hell1_center[1] + (UNIT * 3 / 8),
fill='red')
def update():
for t in range(10):
s = env.reset()
while True:
env.render()
a = 1
s, r, done = env.step(a)
#print(s,"\n",r,"\n",done,"\n")
if done:
break
if __name__ == '__main__':
env = Environment()
env.after(100, update)
env.mainloop()