-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeenv2.py
171 lines (136 loc) · 6.13 KB
/
snakeenv2.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
import gym
from gym import spaces
import numpy as np
import cv2
import random
import time
from collections import deque
#This environment simulates the classic Snake game, where a snake tries to eat apples to grow longer while avoiding collisions with boundaries and itself.
#we have 4 different action(representing the direction the snake should move), and an observation space consisting of various parameters related to the state of the game
SNAKE_LEN_GOAL = 30
tableSizeObs = 500
tableSize = 400 ##!!
halfTable = int(tableSize/2)
def collision_with_apple(apple_position, score):
apple_position = [random.randrange(1,tableSize/10)*10,random.randrange(1,tableSize/10)*10]
score += 1
return apple_position, score
def collision_with_boundaries(snake_head):
if snake_head[0]>=tableSize or snake_head[0]<0 or snake_head[1]>=tableSize or snake_head[1]<0 :
return 1
else:
return 0
def collision_with_self(snake_position):
snake_head = snake_position[0]
if snake_head in snake_position[1:]:
return 1
else:
return 0
class SnakeEnv(gym.Env):
width = 400
height = 400
curriculum = 0
def __init__(self,width,height):
super(SnakeEnv, self).__init__()
# Define action and observation space
# They must be gym.spaces objects
# Example when using discrete actions:
self.action_space = spaces.Discrete(4)
# Example for using image as input (channel-first; channel-last also works):
self.observation_space = spaces.Box(low=-tableSizeObs, high=tableSizeObs,
shape=(5+SNAKE_LEN_GOAL,), dtype=np.float64)
def step(self, action):
self.prev_actions.append(action)
cv2.imshow('a',self.img)
cv2.waitKey(1)
self.img = np.zeros((tableSize,tableSize,3),dtype='uint8')
# Display Apple
cv2.rectangle(self.img,(self.apple_position[0],self.apple_position[1]),(self.apple_position[0]+10,self.apple_position[1]+10),(0,0,255),3)
# Display Snake
for position in self.snake_position:
cv2.rectangle(self.img,(position[0],position[1]),(position[0]+10,position[1]+10),(0,255,0),3)
# Takes step after fixed time
t_end = time.time() + 0.05
k = -1
while time.time() < t_end:
if k == -1:
k = cv2.waitKey(1)
else:
continue
button_direction = action
# Change the head position based on the button direction
if button_direction == 1:
self.snake_head[0] += 10
elif button_direction == 0:
self.snake_head[0] -= 10
elif button_direction == 2:
self.snake_head[1] += 10
elif button_direction == 3:
self.snake_head[1] -= 10
apple_reward = 0
# Increase Snake length on eating apple
if self.snake_head == self.apple_position:
self.apple_position, self.score = collision_with_apple(self.apple_position, self.score)
self.snake_position.insert(0,list(self.snake_head))
apple_reward = 10000
else:
self.snake_position.insert(0,list(self.snake_head))
self.snake_position.pop()
# On collision kill the snake and print the score
if collision_with_boundaries(self.snake_head) == 1 or collision_with_self(self.snake_position) == 1:
font = cv2.FONT_HERSHEY_SIMPLEX
self.img = np.zeros((tableSize,tableSize,3),dtype='uint8')
cv2.putText(self.img,'Your Score is {}'.format(self.score),(140,250), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('a',self.img)
self.done = True
euclidean_dist_to_apple = np.linalg.norm(np.array(self.snake_head) - np.array(self.apple_position))
self.total_reward = ((halfTable - euclidean_dist_to_apple) + apple_reward)/100
#print(self.total_reward)
self.reward = self.total_reward - self.prev_reward
self.prev_reward = self.reward
if self.done:
self.reward = -300
info = {}
head_x = self.snake_head[0]
head_y = self.snake_head[1]
snake_length = len(self.snake_position)
apple_delta_x = self.apple_position[0] - head_x
apple_delta_y = self.apple_position[1] - head_y
# create observation:
observation = [head_x, head_y, apple_delta_x, apple_delta_y, snake_length] + list(self.prev_actions)
observation = np.array(observation)
return observation, self.total_reward, self.done, info
def reset(self):
self.img = np.zeros((tableSize,tableSize,3),dtype='uint8')
# Initial Snake and Apple position
self.snake_position = [[halfTable,halfTable],[halfTable - 10,halfTable],[halfTable - 20,halfTable]]
self.apple_position = [random.randrange(1,tableSize/10) * 10,random.randrange(1,tableSize/10) * 10]
self.score = 0
self.prev_button_direction = 1
self.button_direction = 1
self.snake_head = [halfTable,halfTable]
self.prev_reward = 0
self.total_reward = 0
self.done = False
head_x = self.snake_head[0]
head_y = self.snake_head[1]
snake_length = len(self.snake_position)
apple_delta_x = self.apple_position[0] - head_x
apple_delta_y = self.apple_position[1] - head_y
self.prev_actions = deque(maxlen = SNAKE_LEN_GOAL) # however long we aspire the snake to be
for i in range(SNAKE_LEN_GOAL):
self.prev_actions.append(-1) # to create history
# create observation:
observation = [head_x, head_y, apple_delta_x, apple_delta_y, snake_length] + list(self.prev_actions)
observation = np.array(observation)
return observation
def render(self):
# Create a black image.
img = np.zeros((self.height, self.width, 3), dtype=np.uint8)
# Draw the snake.
for position in self.snake_position:
img[position[0], position[1], :] = (0, 255, 0)
# Draw the apple.
img[self.apple_position[0], self.apple_position[1], :] = (0, 0, 255)
# Return the image.
return img