-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: add robosuite support for sac #20
Open
OrangeX4
wants to merge
5
commits into
LAMDA-RL:master
Choose a base branch
from
OrangeX4:sac
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
""" | ||
This file implements a wrapper for facilitating compatibility with OpenAI gym. | ||
This is useful when using these environments with code that assumes a gym-like | ||
interface. | ||
""" | ||
|
||
import numpy as np | ||
import gym | ||
from gym import spaces, Env | ||
|
||
from robosuite.wrappers import Wrapper | ||
|
||
|
||
class GymWrapper(Wrapper, gym.Env): | ||
metadata = None | ||
render_mode = None | ||
""" | ||
Initializes the Gym wrapper. Mimics many of the required functionalities of the Wrapper class | ||
found in the gym.core module | ||
|
||
Args: | ||
env (MujocoEnv): The environment to wrap. | ||
keys (None or list of str): If provided, each observation will | ||
consist of concatenated keys from the wrapped environment's | ||
observation dictionary. Defaults to proprio-state and object-state. | ||
|
||
Raises: | ||
AssertionError: [Object observations must be enabled if no keys] | ||
""" | ||
|
||
def __init__(self, env, keys=None): | ||
# Run super method | ||
super().__init__(env=env) | ||
# Create name for gym | ||
robots = "".join( | ||
[type(robot.robot_model).__name__ for robot in self.env.robots] | ||
) | ||
self.name = robots + "_" + type(self.env).__name__ | ||
|
||
# Get reward range | ||
self.reward_range = (0, self.env.reward_scale) | ||
|
||
if keys is None: | ||
keys = [] | ||
# Add object obs if requested | ||
if self.env.use_object_obs: | ||
keys += ["object-state"] | ||
# Add image obs if requested | ||
if self.env.use_camera_obs: | ||
keys += [f"{cam_name}_image" for cam_name in self.env.camera_names] | ||
# Iterate over all robots to add to state | ||
for idx in range(len(self.env.robots)): | ||
keys += ["robot{}_proprio-state".format(idx)] | ||
self.keys = keys | ||
|
||
# Gym specific attributes | ||
self.env.spec = None | ||
|
||
# set up observation and action spaces | ||
obs = self.env.reset() | ||
self.modality_dims = {key: obs[key].shape for key in self.keys} | ||
flat_ob = self._flatten_obs(obs) | ||
self.obs_dim = flat_ob.size | ||
high = np.inf * np.ones(self.obs_dim) | ||
low = -high | ||
self.observation_space = spaces.Box(low, high) | ||
low, high = self.env.action_spec | ||
self.action_space = spaces.Box(low, high) | ||
|
||
def _flatten_obs(self, obs_dict, verbose=False): | ||
""" | ||
Filters keys of interest out and concatenate the information. | ||
|
||
Args: | ||
obs_dict (OrderedDict): ordered dictionary of observations | ||
verbose (bool): Whether to print out to console as observation keys are processed | ||
|
||
Returns: | ||
np.array: observations flattened into a 1d array | ||
""" | ||
ob_lst = [] | ||
for key in self.keys: | ||
if key in obs_dict: | ||
if verbose: | ||
print("adding key: {}".format(key)) | ||
ob_lst.append(np.array(obs_dict[key]).flatten()) | ||
return np.concatenate(ob_lst) | ||
|
||
def reset(self, seed=None, options=None): | ||
""" | ||
Extends env reset method to return flattened observation instead of normal OrderedDict and optionally resets seed | ||
|
||
Returns: | ||
np.array: Flattened environment observation space after reset occurs | ||
""" | ||
if seed is not None: | ||
if isinstance(seed, int): | ||
np.random.seed(seed) | ||
else: | ||
raise TypeError("Seed must be an integer type!") | ||
ob_dict = self.env.reset() | ||
return self._flatten_obs(ob_dict) | ||
|
||
def step(self, action): | ||
""" | ||
Extends vanilla step() function call to return flattened observation instead of normal OrderedDict. | ||
|
||
Args: | ||
action (np.array): Action to take in environment | ||
|
||
Returns: | ||
4-tuple: | ||
|
||
- (np.array) flattened observations from the environment | ||
- (float) reward from the environment | ||
- (bool) episode ending after reaching an env terminal state | ||
- (dict) misc information | ||
""" | ||
ob_dict, reward, terminated, info = self.env.step(action) | ||
return self._flatten_obs(ob_dict), reward, terminated, info | ||
|
||
def compute_reward(self, achieved_goal, desired_goal, info): | ||
""" | ||
Dummy function to be compatible with gym interface that simply returns environment reward | ||
|
||
Args: | ||
achieved_goal: [NOT USED] | ||
desired_goal: [NOT USED] | ||
info: [NOT USED] | ||
|
||
Returns: | ||
float: environment reward | ||
""" | ||
# Dummy args used to mimic Wrapper interface | ||
return self.env.reward() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from reproduce.sac.config.robosuite.base import * | ||
|
||
task = "Door" | ||
robots = "Panda" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from reproduce.sac.config.robosuite.base import * | ||
|
||
task = "Lift" | ||
robots = "Panda" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from UtilsRL.misc import NameSpace | ||
|
||
seed = 0 | ||
task = None | ||
max_buffer_size = 1000000 | ||
discount = 0.99 | ||
tau = 0.005 | ||
alpha = 0.2 | ||
auto_alpha = True | ||
reward_scale = 1.0 | ||
|
||
critic_hidden_dims = [256, 256] | ||
critic_lr = 0.0003 | ||
actor_hidden_dims = [256, 256] | ||
actor_lr = 0.0003 | ||
|
||
alpha_lr = 0.0003 | ||
|
||
num_epoch = 2000 | ||
episode_per_epoch = 10 | ||
step_per_epoch = 1000 | ||
batch_size = 256 | ||
|
||
eval_interval = 10 | ||
eval_episode = 10 | ||
save_interval = 50 | ||
log_interval = 10 | ||
warmup_epoch = 2 | ||
random_policy_epoch = 5 | ||
max_trajectory_length = 500 | ||
|
||
policy_logstd_min = -20 | ||
policy_logstd_max = 2 | ||
target_update_freq = 1 | ||
|
||
env_type = "robosuite" | ||
name = "robosuite" | ||
|
||
|
||
class wandb(NameSpace): | ||
entity = None | ||
project = None | ||
|
||
|
||
debug = False | ||
|
||
critic_q_num = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的train loop的逻辑要变更,改成:训练N1个epoch,其中每个epoch会先收集N2个episode,然后训练N3个gradient step,其中N1=2000, N2=10, N3=1000