forked from Farama-Foundation/Minigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty.py
106 lines (80 loc) · 2.86 KB
/
empty.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
import random
import numpy as np
from gym_minigrid.minigrid import *
from gym_minigrid.register import register
class EmptyEnv(MiniGridEnv):
"""
Empty grid environment, no obstacles, sparse reward
"""
def __init__(self, **kwargs):
super().__init__(
# Set this to True for maximum speed
see_through_walls=True,
**kwargs
)
def _gen_grid(self, width, height,total_sum=None,**kwargs):
# Create an empty grid
self.grid = Grid(width, height,total_sum=None,**kwargs)#**kwargs)
# Generate the surrounding walls
self.grid.wall_rect(0, 0, width, height)
# Place the agent in the top-left corner
self.start_pos = (width //2, height//2)
#self.start_pos=(1,1)
self.start_dir = 0
# Place a goal square in the bottom-right corner
#if self.goal is None :
###############The goal definition
#self.pos_goal = (width-2, height-2)
#self.grid.set(self.pos_goal[0], self.pos_goal[1], Goal())
#else :
# index = np.argmax(self.goal,axis=0)
# self.pos_goal = (width-2,1) if index ==0 else (1,height-2)
# self.grid.set(self.pos_goal[0], self.pos_goal[1], Goal())
"""
while True :
self.start_pos = (random.randint(1,width-2),random.randint(1,height-2))
if self.start_pos != self.pos_goal:
break
self.start_dir = random.randint(0,3)
"""
self.mission = "get to the green goal square"
def _reward(self):
"""
Compute the reward to be given upon success
"""
#x,y = self.agent_pos
#goal_x, goal_y = self.pos_goal
#reward = -np.sqrt(math.pow(x-goal_x,2)+math.pow(y-goal_y,2)) / self.width
#return reward
return 1
#return 1 - 0.9 * (self.step_count / self.max_steps)
class EmptyEnv6x6(EmptyEnv):
def __init__(self):
super().__init__(size=6)
class EmptyEnv16x16(EmptyEnv):
def __init__(self):
super().__init__(size=16)
class EmptyWallEnv(EmptyEnv):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def _gen_grid(self, width, height,*args,**kwargs):
super()._gen_grid( width, height,*args,**kwargs)
self.grid.vert_wall(width//2 +2 , 1,(height//2)+6)
class EmptyWallHorzEnv(EmptyEnv):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def _gen_grid(self, width, height,*args,**kwargs):
super()._gen_grid( width, height,*args,**kwargs)
self.grid.horz_wall(width//2-6, (height//2), 5)
register(
id='MiniGrid-Empty-6x6-v0',
entry_point='gym_minigrid.envs:EmptyEnv6x6'
)
register(
id='MiniGrid-Empty-8x8-v0',
entry_point='gym_minigrid.envs:EmptyEnv'
)
register(
id='MiniGrid-Empty-16x16-v0',
entry_point='gym_minigrid.envs:EmptyEnv16x16'
)