-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
64 lines (54 loc) · 1.61 KB
/
utils.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
import numpy as np
###### Constants you should be using in your code ######
DISPLAY_HEIGHT = 10
DISPLAY_WIDTH = 18
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
# Colors for pygame
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (72, 61, 139)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
NUM_FOOD_DIR_X=3
NUM_FOOD_DIR_Y=3
NUM_ADJOINING_WALL_X_STATES=3
NUM_ADJOINING_WALL_Y_STATES=3
NUM_ADJOINING_BODY_TOP_STATES=2
NUM_ADJOINING_BODY_BOTTOM_STATES=2
NUM_ADJOINING_BODY_LEFT_STATES=2
NUM_ADJOINING_BODY_RIGHT_STATES=2
NUM_ACTIONS = 4
CHECKPOINT = 'checkpoint.npy'
def create_q_table():
return np.zeros((NUM_FOOD_DIR_X, NUM_FOOD_DIR_Y, NUM_ADJOINING_WALL_X_STATES, NUM_ADJOINING_WALL_Y_STATES,
NUM_ADJOINING_BODY_TOP_STATES, NUM_ADJOINING_BODY_BOTTOM_STATES, NUM_ADJOINING_BODY_LEFT_STATES,
NUM_ADJOINING_BODY_RIGHT_STATES, NUM_ACTIONS))
def sanity_check(arr):
if (type(arr) is np.ndarray and
arr.shape==(NUM_FOOD_DIR_X, NUM_FOOD_DIR_Y, NUM_ADJOINING_WALL_X_STATES, NUM_ADJOINING_WALL_Y_STATES,
NUM_ADJOINING_BODY_TOP_STATES, NUM_ADJOINING_BODY_BOTTOM_STATES, NUM_ADJOINING_BODY_LEFT_STATES,
NUM_ADJOINING_BODY_RIGHT_STATES,NUM_ACTIONS)):
return True
else:
return False
def save(filename, arr):
if sanity_check(arr):
np.save(filename,arr)
return True
else:
print("Failed to save model")
return False
def load(filename):
try:
arr = np.load(filename)
if sanity_check(arr):
print("Loaded model successfully")
return arr
print("Model loaded is not in the required format")
return None
except:
print("Filename doesnt exist")
return None