-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadm_env.py
160 lines (127 loc) · 4.51 KB
/
padm_env.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
# Imports:
# --------
import gymnasium as gym
import numpy as np
import pygame
import sys
# Class 1: Define a custom environment
# --------
class PadmEnv(gym.Env):
def __init__(self, grid_size=5, goal_coordinates=(4, 4)) -> None:
super(PadmEnv, self).__init__()
self.grid_size = grid_size
self.cell_size = 100
self.state = None
self.reward = 0
self.info = {}
self.goal = np.array(goal_coordinates)
self.done = False
self.hell_states = []
# Action-space:
self.action_space = gym.spaces.Discrete(4)
# Observation space:
self.observation_space = gym.spaces.Box(
low=0, high=grid_size-1, shape=(2,), dtype=np.int32)
# Initialize the window:
pygame.init()
self.screen = pygame.display.set_mode(
(self.cell_size*self.grid_size, self.cell_size*self.grid_size))
# Method 1: .reset()
# ---------
def reset(self):
"""
Everything must be reset
"""
self.state = np.array([0, 0])
self.done = False
self.reward = 0
self.info["Distance to goal"] = np.sqrt(
(self.state[0]-self.goal[0])**2 +
(self.state[1]-self.goal[1])**2
)
return self.state, self.info
# Method 2: Add hell states
# ---------
def add_hell_states(self, hell_state_coordinates):
self.hell_states.append(np.array(hell_state_coordinates))
# Method 3: .step()
# ---------
def step(self, action):
# Actions:
# --------
# Up:
if action == 0 and self.state[0] > 0:
self.state[0] -= 1
# Down:
if action == 1 and self.state[0] < self.grid_size-1:
self.state[0] += 1
# Right:
if action == 2 and self.state[1] < self.grid_size-1:
self.state[1] += 1
# Left:
if action == 3 and self.state[1] > 0:
self.state[1] -= 1
# Reward:
# -------
if np.array_equal(self.state, self.goal): # Check goal condition
self.reward += 10
self.done = True
# Check hell-states
elif True in [np.array_equal(self.state, each_hell) for each_hell in self.hell_states]:
self.reward += -1
self.done = True
else: # Every other state
self.reward += 0
self.done = False
# Info:
# -----
self.info["Distance to goal"] = np.sqrt(
(self.state[0]-self.goal[0])**2 +
(self.state[1]-self.goal[1])**2
)
return self.state, self.reward, self.done, self.info
# Method 3: .render()
# ---------
def render(self):
# Code for closing the window:
for event in pygame.event.get():
if event == pygame.QUIT:
pygame.quit()
sys.exit()
# We make the background White
self.screen.fill((255, 255, 255))
# Draw Grid lines:
for y in range(self.grid_size):
for x in range(self.grid_size):
grid = pygame.Rect(
y*self.cell_size, x*self.cell_size, self.cell_size, self.cell_size)
pygame.draw.rect(self.screen, (200, 200, 200), grid, 1)
# Draw the Goal-state:
goal = pygame.Rect(self.goal[1]*self.cell_size, self.goal[0]
* self.cell_size, self.cell_size, self.cell_size)
pygame.draw.rect(self.screen, (0, 255, 0), goal)
# Draw the hell-states:
for each_hell in self.hell_states:
hell = pygame.Rect(
each_hell[1]*self.cell_size, each_hell[0]*self.cell_size, self.cell_size, self.cell_size)
pygame.draw.rect(self.screen, (255, 0, 0), hell)
# Draw the agent:
agent = pygame.Rect(self.state[1]*self.cell_size, self.state[0]
* self.cell_size, self.cell_size, self.cell_size)
pygame.draw.rect(self.screen, (0, 0, 0), agent)
# Update contents on the window:
pygame.display.flip()
# Method 4: .close()
# ---------
def close(self):
pygame.quit()
# Function 1: Create an instance of the environment
# -----------
def create_env(goal_coordinates,
hell_state_coordinates):
# Create the environment:
# -----------------------
env = PadmEnv(goal_coordinates=goal_coordinates)
for i in range(len(hell_state_coordinates)):
env.add_hell_states(hell_state_coordinates=hell_state_coordinates[i])
return env