A Fish `Task` for swimming with smooth reward.
| 129 | |
| 130 | |
| 131 | class Swim(base.Task): |
| 132 | """A Fish `Task` for swimming with smooth reward.""" |
| 133 | |
| 134 | def __init__(self, random=None): |
| 135 | """Initializes an instance of `Swim`. |
| 136 | |
| 137 | Args: |
| 138 | random: Optional, either a `numpy.random.RandomState` instance, an |
| 139 | integer seed for creating a new `RandomState`, or None to select a seed |
| 140 | automatically (default). |
| 141 | """ |
| 142 | super().__init__(random=random) |
| 143 | |
| 144 | def initialize_episode(self, physics): |
| 145 | """Sets the state of the environment at the start of each episode.""" |
| 146 | |
| 147 | quat = self.random.randn(4) |
| 148 | physics.named.data.qpos['root'][3:7] = quat / np.linalg.norm(quat) |
| 149 | for joint in _JOINTS: |
| 150 | physics.named.data.qpos[joint] = self.random.uniform(-.2, .2) |
| 151 | # Randomize target position. |
| 152 | physics.named.model.geom_pos['target', 'x'] = self.random.uniform(-.4, .4) |
| 153 | physics.named.model.geom_pos['target', 'y'] = self.random.uniform(-.4, .4) |
| 154 | physics.named.model.geom_pos['target', 'z'] = self.random.uniform(.1, .3) |
| 155 | super().initialize_episode(physics) |
| 156 | |
| 157 | def get_observation(self, physics): |
| 158 | """Returns an observation of joints, target direction and velocities.""" |
| 159 | obs = collections.OrderedDict() |
| 160 | obs['joint_angles'] = physics.joint_angles() |
| 161 | obs['upright'] = physics.upright() |
| 162 | obs['target'] = physics.mouth_to_target() |
| 163 | obs['velocity'] = physics.velocity() |
| 164 | return obs |
| 165 | |
| 166 | def get_reward(self, physics): |
| 167 | """Returns a smooth reward.""" |
| 168 | radii = physics.named.model.geom_size[['mouth', 'target'], 0].sum() |
| 169 | in_target = rewards.tolerance(np.linalg.norm(physics.mouth_to_target()), |
| 170 | bounds=(0, radii), margin=2*radii) |
| 171 | is_upright = 0.5 * (physics.upright() + 1) |
| 172 | return (7*in_target + is_upright) / 8 |
no outgoing calls
no test coverage detected
searching dependent graphs…