Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace random.py with np_random #74

Merged
merged 10 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gym_miniworld/envs/hallway.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _gen_world(self):

# Place the agent a random distance away from the goal
self.place_agent(
dir=self.rand.float(-math.pi / 4, math.pi / 4), max_x=room.max_x - 2
dir=self.np_random.uniform(-math.pi / 4, math.pi / 4), max_x=room.max_x - 2
)

def step(self, action):
Expand Down
9 changes: 8 additions & 1 deletion gym_miniworld/envs/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ def visit(i, j):
visited.add(room)

# Reorder the neighbors to visit in a random order
neighbors = self.rand.subset([(0, 1), (0, -1), (-1, 0), (1, 0)], 4)
orders = [(0, 1), (0, -1), (-1, 0), (1, 0)]
assert 4 <= len(orders)
neighbors = []

while len(neighbors) < 4:
elem = orders[self.np_random.choice(len(orders))]
orders.remove(elem)
neighbors.append(elem)

# For each possible neighbor
for dj, di in neighbors:
Expand Down
7 changes: 4 additions & 3 deletions gym_miniworld/envs/pickupobjs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from gymnasium import spaces

from gym_miniworld.entity import Ball, Box, Key
from gym_miniworld.entity import COLOR_NAMES, Ball, Box, Key
from gym_miniworld.miniworld import MiniWorldEnv


Expand Down Expand Up @@ -64,10 +64,11 @@ def _gen_world(self):
)

obj_types = [Ball, Box, Key]
colorlist = list(COLOR_NAMES)

for obj in range(self.num_objs):
obj_type = self.rand.choice(obj_types)
color = self.rand.color()
obj_type = obj_types[self.np_random.choice(len(obj_types))]
color = colorlist[self.np_random.choice(len(colorlist))]

if obj_type == Box:
self.place_entity(Box(color=color, size=0.9))
Expand Down
2 changes: 1 addition & 1 deletion gym_miniworld/envs/putnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _gen_world(self):
self.add_rect_room(min_x=0, max_x=self.size, min_z=0, max_z=self.size)

for color in COLOR_NAMES:
box = Box(color=color, size=self.rand.float(0.6, 0.85))
box = Box(color=color, size=self.np_random.uniform(0.6, 0.85))
self.place_entity(box)

if box.color == "red":
Expand Down
13 changes: 9 additions & 4 deletions gym_miniworld/envs/roomobjs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from gym_miniworld.entity import Ball, Box, Key
from gym_miniworld.entity import COLOR_NAMES, Ball, Box, Key
from gym_miniworld.miniworld import MiniWorldEnv


Expand Down Expand Up @@ -64,10 +64,15 @@ def _gen_world(self):

# Reduce chances that objects are too close to see
self.agent.radius = 1.5
colorlist = list(COLOR_NAMES)

self.place_entity(Box(color=self.rand.color(), size=0.9))
self.place_entity(Ball(color=self.rand.color(), size=0.9))
self.place_entity(Key(color=self.rand.color()))
self.place_entity(
Box(color=colorlist[self.np_random.choice(len(colorlist))], size=0.9)
)
self.place_entity(
Ball(color=colorlist[self.np_random.choice(len(colorlist))], size=0.9)
)
self.place_entity(Key(color=colorlist[self.np_random.choice(len(colorlist))]))

self.place_agent()

Expand Down
43 changes: 22 additions & 21 deletions gym_miniworld/envs/simtorealgoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,33 @@ def __init__(self, **kwargs):

def _gen_world(self):
# 1-2 meter wide rink
size = self.rand.float(1, 2)
size = self.np_random.uniform(1, 2)

wall_height = self.rand.float(0.20, 0.50)
wall_height = self.np_random.uniform(0.20, 0.50)

box_size = self.rand.float(0.07, 0.12)
box_size = self.np_random.uniform(0.07, 0.12)

self.agent.radius = 0.11

floor_tex = self.rand.choice(
[
"cardboard",
"wood",
"wood_planks",
]
)

wall_tex = self.rand.choice(
[
"drywall",
"stucco",
"cardboard",
# Chosen because they have visible lines/seams
"concrete_tiles",
"ceiling_tiles",
]
)
# Randomly choosing floor_tex and wall_tex
floor_tex_list = [
"cardboard",
"wood",
"wood_planks",
]

wall_tex_list = [
"drywall",
"stucco",
"cardboard",
# Chosen because they have visible lines/seams
"concrete_tiles",
"ceiling_tiles",
]

floor_tex = self.np_random.choice(floor_tex_list)

wall_tex = self.np_random.choice(wall_tex_list)

# Create a long rectangular room
self.add_rect_room(
Expand Down
45 changes: 23 additions & 22 deletions gym_miniworld/envs/simtorealpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,32 @@ def __init__(self, **kwargs):

def _gen_world(self):
# Size of the rink the robot is placed in
size = self.rand.float(1.6, 1.7)
wall_height = self.rand.float(0.42, 0.50)
size = self.np_random.uniform(1.6, 1.7)
wall_height = self.np_random.uniform(0.42, 0.50)

box1_size = self.rand.float(0.075, 0.090)
box2_size = self.rand.float(0.075, 0.090)
box1_size = self.np_random.uniform(0.075, 0.090)
box2_size = self.np_random.uniform(0.075, 0.090)

self.agent.radius = 0.11

floor_tex = self.rand.choice(
[
"cardboard",
"wood",
"wood_planks",
]
)

wall_tex = self.rand.choice(
[
"drywall",
"stucco",
# Materials chosen because they have visible lines/seams
"concrete_tiles",
"ceiling_tiles",
]
)
# Randomly choosing floor_tex and wall_tex
floor_tex_list = [
"cardboard",
"wood",
"wood_planks",
]

wall_tex_list = [
"drywall",
"stucco",
"cardboard",
# Chosen because they have visible lines/seams
"concrete_tiles",
"ceiling_tiles",
]
floor_tex = self.np_random.choice(floor_tex_list)

wall_tex = self.np_random.choice(wall_tex_list)

# Create a long rectangular room
self.add_rect_room(
Expand Down Expand Up @@ -154,7 +155,7 @@ def step(self, action):
next_box_pos = box.pos + vec
if not self.intersect(box, next_box_pos, box.radius):
box.pos = next_box_pos
box.dir += self.rand.float(-math.pi / 5, math.pi / 5)
box.dir += self.np_random.uniform(-math.pi / 5, math.pi / 5)

obs, reward, termination, truncation, info = super().step(action)

Expand Down
6 changes: 4 additions & 2 deletions gym_miniworld/envs/tmaze.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ def _gen_world(self):
max_z=self.goal_pos[2],
)
else:
if self.rand.bool():
if self.np_random.integers(0, 2) == 0:
self.place_entity(self.box, room=room2, max_z=room2.min_z + 2)
else:
self.place_entity(self.box, room=room2, min_z=room2.max_z - 2)

# Choose a random room and position to spawn at
self.place_agent(dir=self.rand.float(-math.pi / 4, math.pi / 4), room=room1)
self.place_agent(
dir=self.np_random.uniform(-math.pi / 4, math.pi / 4), room=room1
)

def step(self, action):
obs, reward, termination, truncation, info = super().step(action)
Expand Down
6 changes: 4 additions & 2 deletions gym_miniworld/envs/ymaze.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ def _gen_world(self):
max_z=self.goal_pos[2],
)
else:
if self.rand.bool():
if self.np_random.integers(0, 2) == 0:
self.place_entity(self.box, room=left_arm, max_z=left_arm.min_z + 2.5)
else:
self.place_entity(self.box, room=right_arm, min_z=right_arm.max_z - 2.5)

# Choose a random room and position to spawn at
self.place_agent(dir=self.rand.float(-math.pi / 4, math.pi / 4), room=main_arm)
self.place_agent(
dir=self.np_random.uniform(-math.pi / 4, math.pi / 4), room=main_arm
)

def step(self, action):
obs, reward, termination, truncation, info = super().step(action)
Expand Down
30 changes: 20 additions & 10 deletions gym_miniworld/miniworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
from gym_miniworld.math import Y_VEC, intersect_circle_segs
from gym_miniworld.opengl import FrameBuffer, Texture, drawBox
from gym_miniworld.params import DEFAULT_PARAMS
from gym_miniworld.random import RandGen

# Default wall height for room
DEFAULT_WALL_HEIGHT = 2.74
Expand Down Expand Up @@ -550,7 +549,6 @@ def reset(
This also randomizes many environment parameters (domain randomization)
"""
super().reset(seed=seed)
self.rand = RandGen(seed)

# Step count since episode start
self.step_count = 0
Expand All @@ -572,7 +570,7 @@ def reset(
self._gen_world()

# Check if domain randomization is enabled or not
rand = self.rand if self.domain_rand else None
rand = self.np_random if self.domain_rand else None

# Randomize elements of the world (domain randomization)
self.params.sample_many(
Expand Down Expand Up @@ -676,7 +674,7 @@ def step(self, action):

self.step_count += 1

rand = self.rand if self.domain_rand else None
rand = self.np_random if self.domain_rand else None
fwd_step = self.params.sample(rand, "forward_step")
fwd_drift = self.params.sample(rand, "forward_drift")
turn_step = self.params.sample(rand, "turn_step")
Expand Down Expand Up @@ -863,22 +861,30 @@ def place_entity(

# If an exact position if specified
if pos is not None:
ent.dir = dir if dir is not None else self.rand.float(-math.pi, math.pi)
ent.dir = (
dir if dir is not None else self.np_random.uniform(-math.pi, math.pi)
)
ent.pos = pos
self.entities.append(ent)
return ent

# Keep retrying until we find a suitable position
while True:
# Pick a room, sample rooms proportionally to floor surface area
r = room if room else self.rand.choice(self.rooms, probs=self.room_probs)
r = (
room
if room
else list(self.rooms)[
self.np_random.choice(len(list(self.rooms)), p=self.room_probs)
]
)

# Choose a random point within the square bounding box of the room
lx = r.min_x if min_x is None else min_x
hx = r.max_x if max_x is None else max_x
lz = r.min_z if min_z is None else min_z
hz = r.max_z if max_z is None else max_z
pos = self.rand.float(
pos = self.np_random.uniform(
low=[lx - ent.radius, 0, lz - ent.radius],
high=[hx + ent.radius, 0, hz + ent.radius],
)
Expand All @@ -892,7 +898,7 @@ def place_entity(
continue

# Pick a direction
d = dir if dir is not None else self.rand.float(-math.pi, math.pi)
d = dir if dir is not None else self.np_random.uniform(-math.pi, math.pi)

ent.pos = pos
ent.dir = d
Expand Down Expand Up @@ -965,7 +971,9 @@ def _load_tex(self, tex_name):
Load a texture, with or without domain randomization
"""

rand = self.rand if self.params.sample(self.rand, "tex_rand") else None
rand = (
self.np_random if self.params.sample(self.np_random, "tex_rand") else None
)
return Texture.get(tex_name, rand)

def _gen_static_data(self):
Expand All @@ -975,7 +983,9 @@ def _gen_static_data(self):

# Generate the static data for each room
for room in self.rooms:
room._gen_static_data(self.params, self.rand if self.domain_rand else None)
room._gen_static_data(
self.params, self.np_random if self.domain_rand else None
)

# Concatenate the wall segments
self.wall_segs = np.concatenate([r.wall_segs for r in self.rooms])
Expand Down
2 changes: 1 addition & 1 deletion gym_miniworld/opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def get(self, tex_name, rng=None):

# If domain-randomization is to be used
if rng:
path_idx = rng.int(0, len(paths))
path_idx = rng.integers(0, len(paths))
path = paths[path_idx]
else:
path = paths[0]
Expand Down
4 changes: 2 additions & 2 deletions gym_miniworld/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def sample(self, rng, name):
return p.default

if p.type == "float":
return rng.float(p.min, p.max)
return rng.uniform(p.min, p.max)
elif p.type == "int":
return rng.int(p.min, p.max + 1)
return rng.integers(p.min, p.max + 1)

assert False

Expand Down
Loading