-
Notifications
You must be signed in to change notification settings - Fork 126
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
How to introduce our environment? #285
Comments
|
Have you imported your custom environment in from omnisafe.envs.custom_env import CustomEnv to resolve that issue. |
I tried writing code in a similar way to your screenshots, but unfortunately, I was unable to reproduce the same error. I will share my steps and source code below, and I hope it can be of help to you:
from __future__ import annotations
from typing import Any, ClassVar
import torch
from omnisafe.envs.core import CMDP, env_register
from omnisafe.typing import DEVICE_CPU
@env_register
class CustomEnv(CMDP):
_support_envs: ClassVar[list[str]] = [
'Custom0-v0',
'Custom1-v0',
'Custom2-v0',
]
def __init__(
self,
env_id: str,
num_envs: int = 1,
device: torch.device = DEVICE_CPU,
**kwargs: Any,
) -> None:
super().__init__(env_id)
self._num_envs = num_envs
self._device = torch.device(device)
def reset(
self,
seed: int | None = None,
options: dict[str, Any] | None = None,
) -> tuple[torch.Tensor, dict[str, Any]]:
pass
def step(self):
pass
def set_seed(self, seed: int) -> None:
pass
def sample_action(self) -> torch.Tensor:
pass
def render(self) -> Any:
pass
def close(self) -> None:
pass
from omnisafe.envs.core import CMDP, env_register, make, support_envs
from omnisafe.envs.mujoco_env import MujocoEnv
from omnisafe.envs.safety_gymnasium_env import SafetyGymnasiumEnv
from omnisafe.envs.safety_gymnasium_modelbased import SafetyGymnasiumModelBased
from omnisafe.envs.custenv import CustomEnv
python train_policy.py --algo PPO --env-id Custom0-v0 As there is nothing in this empty environment, I exit the code in class OnlineAdapter:
"""Online Adapter for OmniSafe.
OmniSafe is a framework for safe reinforcement learning. It is designed to be compatible with
any existing RL algorithms. The online adapter is used to adapt the environment to the
framework.
Args:
env_id (str): The environment id.
num_envs (int): The number of parallel environments.
seed (int): The random seed.
cfgs (Config): The configuration.
"""
def __init__( # pylint: disable=too-many-arguments
self,
env_id: str,
num_envs: int,
seed: int,
cfgs: Config,
) -> None:
"""Initialize an instance of :class:`OnlineAdapter`."""
assert env_id in support_envs(), f'Env {env_id} is not supported.'
self._cfgs: Config = cfgs
self._device: torch.device = get_device(cfgs.train_cfgs.device)
self._env_id: str = env_id
self._env: CMDP = make(env_id, num_envs=num_envs, device=self._device)
exit() The code served me well and there is no |
Thank you a lot! I found that the key is to run the terminal. Now the terminal run has been successfully introduced into my environment. I used Spyder before, but the error with "did not have env_id". I don’t know if it is related to the windows system I used. I did not use the ubtun system.
|
Good evening, I followed the guide you provided step by step. Having encountered the same errors as this problem, I'm sharing my code and the error I'm seeing. Thank you in advance for your help. from future import annotations from typing import Any, ClassVar from omnisafe.envs.core import CMDP, env_register @env_register
The error I'm getting is as follows: AssertionError: Custom1-v0 doesn't exist. , even though I have added in init.py the line from omnisafe.envs.custom_env import CustomEnvcode: |
@NoemiF99 Hello. Your issue seems a little bit disorganized. Could you give us more detailed error messages, file structures, etc., so we can help you better identify the cause? |
Thank you for your kind reply, it helps me a lot that you can not imagine. I have a new problem and need your help. self._observation_space = Box(low=0, high=1.0, shape=(8,), dtype=np.float32) Although we have set the aboving in our environment, Why the print action exceed the range 0-1? What else should be set to gurateen the range of observation and action in 0-1? The observation range is limites into 0-1 because we introduce the following normalization in our environment, If we delete the following normalization, the observation also exceeds the range 0-1, So What's the function of the omnisafe\common\normalizer.py? Thank you ~ def normalization(self,data): |
@NoemiF99 |
@charleswangyanan Thanks for your recognition and your helpful response. The design of OmniSafe's |
You can refer to def _wrapper(
self,
obs_normalize: bool = True,
reward_normalize: bool = True,
cost_normalize: bool = True,
) -> None:
# Some code here.
self._env = ActionScale(self._env, low=-1.0, high=1.0, device=self._device)
self._eval_env = ActionScale(self._eval_env, low=-1.0, high=1.0, device=self._device)
if self._env.num_envs == 1:
self._env = Unsqueeze(self._env, device=self._device)
self._eval_env = Unsqueeze(self._eval_env, device=self._device) It seems your implementation is basically the same, so I think it is ok. |
The env=CustomEnv(env_id='Custom1-v0', num_envs=1)
env=ActionScale(env, low=0, high=1.0) |
Yes, of course. I am using Docker to implement autonomous drone navigation through safe deep reinforcement learning algorithms. I have installed Omnisafe, and before using my environment, I am trying to create custom environments. I followed the guide and created a new Python file in the /omnisafe/envs/custom_env.py folder. The custom_env.py file is as follows: from future import annotations from typing import Any, ClassVar from omnisafe.envs.core import CMDP, env_register @env_register
After that, I modified the init.py file in the /omnisafe/envs/ folder as follows: from omnisafe.envs.core import CMDP, env_register, make, support_envs After running the following command in my terminal: |
@NoemiF99 Thanks to the detailed terminal information you provided, I think I know how to solve your problem. Your omnisafe run path is |
Okay, thank you. Honestly, I don't understand why I keep getting an error even though I use @env_register to register my custom environment. In the algo_wrapper.py code in the directory usr/local/lib/python3.8/dist-packages/omnisafe, at lines 72 and 140, it simply tells me that the environment has not been registered correctly. Is there any other step I should take to create and register my custom environment? |
|
I mean, you can place the |
@Gaiejj It seems that by doing it this way, the error related to environment registration is not there. Now I will try to implement my environment for autonomous drone guidance. Thank you very much for the help you have provided, you have been very kind and helpful. If there are any further issues regarding the implementation of my environment, I will write here. |
|
My bad. I double-checked the code, and I think this step helped solve the action scale issue:
|
Sorry, I don'y understand your meaning, whatever the scale of the environment, finally, after the actor of OmniSafe, it will be changed as [-1, 1]? it can't be modified? |
So, without the |
|
It seems that the function of "predict" in "gaussian_learning_actor.py" doesn't control the action value within the range you defined. But Isuppose it can be recongnized as a constraint in SRL. |
Good evening, I wanted to know if it's possible to implement a callback function to save my training data. Thank you very much. |
I don't quite understand what you mean by |
Hello, with my statement, I meant to ask if there is a way to save the progress of my agent during and after training. This way, I can resume training from where it was interrupted, thus avoiding long training times. Subsequently, I can use a function like agent.load(). I read that it's possible with TensorFlow using a function called checkpoint_callback. I wanted to know if it's possible to implement something similar in Omnisafe. Thank you very much. |
Currently, OmniSafe does not support training with checkpoints. We think this is a valuable feature and will support it in the near future. As this involves many code changes, it will take a long time. We'll inform you of some possible directions first.
The above is only a theoretical level of advice, the actual implementation may encounter problems, welcome to keep in touch with us. |
Good evening. I am encountering difficulties in saving the .pt files in the torch_save folder. Specifically, it only saves the .pt file corresponding to epoch 0, while for the remaining epochs, the corresponding .pt file is not generated. Studying the logger.py file in the common folder, it seems that the value of self._epoch is not being correctly updated. I noticed that in the Test folder you shared, even though there are multiple epochs in the progress.csv file, only the epoch-0.pt file is present in the torch_save folder. How can I generate the corresponding .pt files for each epoch? Thank you very much. |
You can set the |
Rerun |
|
Good morning, I would like to understand how to test my custom environment? |
You can refer to |
Good evening, I would like to understand how I can include the number of collisions that occurred with obstacles among the saved data. In particular, I have defined in my custom environment the function that allows me to calculate the number of collisions and the obstacles with which they occur. def check_collisions(self):
Now I need to plot the total number of collisions for each epoch. I tried to modify the policy_gradient.py file, but I can't visualize the result. |
Good evening, model = PPOLag.load("./xx.pt")
I would greatly appreciate it if you could provide guidance on what exactly I need to implement in order to achieve this. Thank you in advance for your assistance. |
@NoemiF99 You can modify the code concerning Omnisafe's logging of environmental parameters such as rewards and costs. You can just add an item named @tjruan You can refer to the entrance file |
Good evening, thank you very much for responding. I would like to understand in which file I can add the number of collisions that I have calculated, so that I can append it to the progress.csv file. |
@NoemiF99 You can:
|
Good afternoon.
Unfortunately, this modification didn't solve the problem. I sincerely appreciate any further assistance you can provide. Thank you. |
Thank you so much for the support and advice you provided. They were very helpful and allowed me to input the data as I wanted. You have been very kind. |
@tjruan The inclination of omnisafe's |
Hello, I would like to ask a question. After inserting the number of collisions in the _log_metrics, _log_value, and _log_reset functions in the onpolicy_adapter.py file and recording my data within the _init_log function of policy_gradient by saving the number of collisions like this: self._logger.register_key('Metrics/EpNumCollisions'). |
Set the |
We need to implement decisions at two time scales, such as 1 time step decision and 4 time step decision making. How to set up two state spaces? Such as the following example, but this example is wrong.
Error is "AssertionError: Observation space must be Box". How to set up two state spaces? Thank you. |
This issue pertains to multi-agent safety reinforcement learning, which is currently unsupported by OmniSafe. The following code base might be of assistance:
|
Issue with Video Saving During Training ################################################## I have checked that the folder structure is correct and there are no obvious errors, but the video is still not appearing. I would like to understand if there is any specific step that could be causing this issue or if there is something I could modify in the code to ensure the proper saving of the video. Thank you in advance for your help and assistance. I am available to provide further details. |
@NoemiF99, my team was having a similar issue when running a video-saving script from inside a Docker container. It seemed to be failing silently, as you described. We used the |
Closed due to no further questions. You're welcome to reopen it if you happen to have more issues. |
Required prerequisites
Questions
Question1: I just want used our own environment, and find the omnisafe-main/tests/simple_env.py, so attempt to modify the simple_env into our own environment. However, when run the omnisafe-main/examples/train_policy.py, the simple_env couldn't be introduced?What shoule I use to train the simple_env.py?
Question2: I learn from issue #273#263#255 and the github README.md, find the following, could you give a specific environment example?It is the same with omnisafe-main/tests/simple_env? Thank you very much!
The text was updated successfully, but these errors were encountered: