|
| 1 | +# Copyright 2020 The dm_control Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================ |
| 15 | +"""Produces reference environments for rodent tasks.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import functools |
| 22 | + |
| 23 | +from dm_control import composer |
| 24 | +from dm_control.composer.variation import distributions |
| 25 | +from dm_control.locomotion.arenas import bowl |
| 26 | +from dm_control.locomotion.arenas import corridors as corr_arenas |
| 27 | +from dm_control.locomotion.arenas import floors |
| 28 | +from dm_control.locomotion.arenas import labmaze_textures |
| 29 | +from dm_control.locomotion.arenas import mazes |
| 30 | +from dm_control.locomotion.props import target_sphere |
| 31 | +from dm_control.locomotion.tasks import corridors as corr_tasks |
| 32 | +from dm_control.locomotion.tasks import escape |
| 33 | +from dm_control.locomotion.tasks import random_goal_maze |
| 34 | +from dm_control.locomotion.tasks import reach |
| 35 | +from dm_control.locomotion.walkers import rodent |
| 36 | + |
| 37 | +_CONTROL_TIMESTEP = .02 |
| 38 | +_PHYSICS_TIMESTEP = 0.001 |
| 39 | + |
| 40 | + |
| 41 | +def rodent_escape_bowl(random_state=None): |
| 42 | + """Requires a rodent to climb out of a bowl-shaped terrain.""" |
| 43 | + |
| 44 | + # Build a position-controlled rodent walker. |
| 45 | + walker = rodent.Rat( |
| 46 | + observable_options={'egocentric_camera': dict(enabled=True)}) |
| 47 | + |
| 48 | + # Build a corridor-shaped arena that is obstructed by walls. |
| 49 | + arena = bowl.Bowl( |
| 50 | + size=(20., 20.), |
| 51 | + aesthetic='outdoor_natural') |
| 52 | + |
| 53 | + # Build a task that rewards the agent for running down the corridor at a |
| 54 | + # specific velocity. |
| 55 | + task = escape.Escape( |
| 56 | + walker=walker, |
| 57 | + arena=arena, |
| 58 | + physics_timestep=_PHYSICS_TIMESTEP, |
| 59 | + control_timestep=_CONTROL_TIMESTEP) |
| 60 | + |
| 61 | + return composer.Environment(time_limit=20, |
| 62 | + task=task, |
| 63 | + random_state=random_state, |
| 64 | + strip_singleton_obs_buffer_dim=True) |
| 65 | + |
| 66 | + |
| 67 | +def rodent_run_gaps(random_state=None): |
| 68 | + """Requires a rodent to run down a corridor with gaps.""" |
| 69 | + |
| 70 | + # Build a position-controlled rodent walker. |
| 71 | + walker = rodent.Rat( |
| 72 | + observable_options={'egocentric_camera': dict(enabled=True)}) |
| 73 | + |
| 74 | + # Build a corridor-shaped arena with gaps, where the sizes of the gaps and |
| 75 | + # platforms are uniformly randomized. |
| 76 | + arena = corr_arenas.GapsCorridor( |
| 77 | + platform_length=distributions.Uniform(.4, .8), |
| 78 | + gap_length=distributions.Uniform(.05, .2), |
| 79 | + corridor_width=2, |
| 80 | + corridor_length=40, |
| 81 | + aesthetic='outdoor_natural') |
| 82 | + |
| 83 | + # Build a task that rewards the agent for running down the corridor at a |
| 84 | + # specific velocity. |
| 85 | + task = corr_tasks.RunThroughCorridor( |
| 86 | + walker=walker, |
| 87 | + arena=arena, |
| 88 | + walker_spawn_position=(5, 0, 0), |
| 89 | + walker_spawn_rotation=0, |
| 90 | + target_velocity=1.0, |
| 91 | + contact_termination=False, |
| 92 | + terminate_at_height=-0.3, |
| 93 | + physics_timestep=_PHYSICS_TIMESTEP, |
| 94 | + control_timestep=_CONTROL_TIMESTEP) |
| 95 | + |
| 96 | + return composer.Environment(time_limit=30, |
| 97 | + task=task, |
| 98 | + random_state=random_state, |
| 99 | + strip_singleton_obs_buffer_dim=True) |
| 100 | + |
| 101 | + |
| 102 | +def rodent_maze_forage(random_state=None): |
| 103 | + """Requires a rodent to find all items in a maze.""" |
| 104 | + |
| 105 | + # Build a position-controlled rodent walker. |
| 106 | + walker = rodent.Rat( |
| 107 | + observable_options={'egocentric_camera': dict(enabled=True)}) |
| 108 | + |
| 109 | + # Build a maze with rooms and targets. |
| 110 | + wall_textures = labmaze_textures.WallTextures(style='style_01') |
| 111 | + arena = mazes.RandomMazeWithTargets( |
| 112 | + x_cells=11, |
| 113 | + y_cells=11, |
| 114 | + xy_scale=.5, |
| 115 | + z_height=.3, |
| 116 | + max_rooms=4, |
| 117 | + room_min_size=4, |
| 118 | + room_max_size=5, |
| 119 | + spawns_per_room=1, |
| 120 | + targets_per_room=3, |
| 121 | + wall_textures=wall_textures, |
| 122 | + aesthetic='outdoor_natural') |
| 123 | + |
| 124 | + # Build a task that rewards the agent for obtaining targets. |
| 125 | + task = random_goal_maze.ManyGoalsMaze( |
| 126 | + walker=walker, |
| 127 | + maze_arena=arena, |
| 128 | + target_builder=functools.partial( |
| 129 | + target_sphere.TargetSphere, |
| 130 | + radius=0.05, |
| 131 | + height_above_ground=.125, |
| 132 | + rgb1=(0, 0, 0.4), |
| 133 | + rgb2=(0, 0, 0.7)), |
| 134 | + target_reward_scale=50., |
| 135 | + contact_termination=False, |
| 136 | + physics_timestep=_PHYSICS_TIMESTEP, |
| 137 | + control_timestep=_CONTROL_TIMESTEP) |
| 138 | + |
| 139 | + return composer.Environment(time_limit=30, |
| 140 | + task=task, |
| 141 | + random_state=random_state, |
| 142 | + strip_singleton_obs_buffer_dim=True) |
| 143 | + |
| 144 | + |
| 145 | +def rodent_two_touch(random_state=None): |
| 146 | + """Requires a rodent to tap an orb, wait an interval, and tap it again.""" |
| 147 | + |
| 148 | + # Build a position-controlled rodent walker. |
| 149 | + walker = rodent.Rat( |
| 150 | + observable_options={'egocentric_camera': dict(enabled=True)}) |
| 151 | + |
| 152 | + arena = floors.Floor( |
| 153 | + size=(10., 10.), |
| 154 | + aesthetic='outdoor_natural') |
| 155 | + |
| 156 | + task = reach.TwoTouch( |
| 157 | + walker=walker, |
| 158 | + arena=arena, |
| 159 | + target_builders=[ |
| 160 | + functools.partial(target_sphere.TargetSphereTwoTouch, radius=0.025), |
| 161 | + ], |
| 162 | + randomize_spawn_rotation=True, |
| 163 | + target_type_rewards=[25.], |
| 164 | + shuffle_target_builders=False, |
| 165 | + target_area=(1.5, 1.5), |
| 166 | + physics_timestep=_PHYSICS_TIMESTEP, |
| 167 | + control_timestep=_CONTROL_TIMESTEP, |
| 168 | + ) |
| 169 | + |
| 170 | + return composer.Environment(time_limit=30, |
| 171 | + task=task, |
| 172 | + random_state=random_state, |
| 173 | + strip_singleton_obs_buffer_dim=True) |
0 commit comments