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

[gym_jiminy/common] Speed-up env pipeline. #634

Merged
merged 1 commit into from
Aug 18, 2023
Merged
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
18 changes: 14 additions & 4 deletions python/gym_jiminy/common/gym_jiminy/common/envs/env_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
from collections.abc import Mapping
from itertools import chain
from typing import (
Dict, Any, List, cast, Optional, Tuple, Callable, Iterable, Union,
SupportsFloat, Iterator, Generic, Sequence, Mapping as MappingT,
Dict, Any, List, cast, no_type_check, Optional, Tuple, Callable, Iterable,
Union, SupportsFloat, Iterator, Generic, Sequence, Mapping as MappingT,
MutableMapping as MutableMappingT)

import tree
import numpy as np
from numpy.core.umath import ( # type: ignore[attr-defined]
copyto as _array_copyto)
from gymnasium import spaces
from gymnasium.core import RenderFrame

Expand Down Expand Up @@ -231,7 +233,6 @@ def __init__(self,
"of custom action spaces.")

# Define specialized operators for efficiency
self._copyto_observation = build_copyto(self.observation)
self._copyto_action = build_copyto(self.action)
self._contains_observation = build_contains(
self.observation, self.observation_space)
Expand Down Expand Up @@ -1376,6 +1377,7 @@ def _refresh_buffers(self) -> None:
`_setup` method.
"""

@no_type_check
def refresh_observation(self, measurement: EngineObsType) -> None:
"""Compute the observation based on the current state of the robot.

Expand All @@ -1396,7 +1398,15 @@ def refresh_observation(self, measurement: EngineObsType) -> None:
checking whether the simulation already started. It is not exactly
the same but it does the job regarding preserving efficiency.
"""
self._copyto_observation(measurement)
observation = self.observation
observation["t"][()] = measurement["t"]
_array_copyto(observation['states']['agent']['q'],
measurement['states']['agent']['q'])
_array_copyto(observation['states']['agent']['v'],
measurement['states']['agent']['v'])
sensors_data = observation['measurements']
for key, value in dict.items(measurement['measurements']):
_array_copyto(sensors_data[key], value)

def compute_command(self, action: ActT) -> np.ndarray:
"""Compute the motors efforts to apply on the robot.
Expand Down