-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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 change the parameters of a mujoco model on the fly? #234
Comments
Changing model parameters is not supported, even by Mujoco's C API (which mujoco-py is a wrapper around). See http://mujoco.org/book/overview.html#Instance. You can change model parameters between simulation runs by creating new XML files and loading them. For example, you could modify the line https://github.com/openai/gym/blob/master/gym/envs/mujoco/half_cheetah.py#L7 to create and load a new XML file each time you create an environment. |
@tlbtlbtlb Are you sure about this? There are some model parameters in env.model.data for example. I am not sure if changing those actually affects the simulation or if it is just a dictionary for keeping track. But this is a very useful functionality for a number of applications. For example, you may want to test the robustness of the agent using perturbations on these model parameters. It would be a shame if we are required to generate multiple xml files to do this simple test. Thanks. |
The supported mechanism is changing the model XML file, which is simple and fast. If you change things inside env.model or env.model.data, those are internal data structures to Mujoco. There is a compilation step between the XML file and the data structure where a single number in the XML file might get translated into several matrices. It may be very hard to change some parameters correctly. So you really need read the Mujoco manual (start with http://mujoco.org/book/modeling.html#Compile) and ask their maintainers for advice about this. |
(Don't actually change the XML file in place: write new files to /tmp and load them) |
@tlbtlbtlb We asked this in the MuJoCo forum, and as we suspected, this functionality is actually available in MJC. We can modify any model parameter on the fly, and these changes take effect before a step is taken in the environment. The same when done through mujoco-py doesn't take effect -- I assume this is because the wrapper doesn't communicate back with the mjModel. Would it be possible to add this functionality, if it isn't too much trouble? My guess is that it shouldn't be hard. |
Can you give an example of what doesn't take effect? x = model.body_mass
x[1] = 3.6
model.body_mass = x does that work? |
The array you get from reading model parameters is read-only, but you can make a copy of it and assign it back. This looks like it works: >>> import gym,numpy as np
>>> e=gym.make('HalfCheetah-v1')
[2016-07-14 15:11:55,441] Making new env: HalfCheetah-v1
>>> mb = e.model.body_mass
>>> mb
array([[ 0. ],
[ 6.36031332],
[ 1.53524804],
[ 1.58093995],
[ 1.0691906 ],
[ 1.42558747],
[ 1.17885117],
[ 0.84986945]])
>>> mb = np.array(mb) # make mutable copy
>>> mb[1,0] = 7.7777
>>> mb
array([[ 0. ],
[ 7.7777 ],
[ 1.53524804],
[ 1.58093995],
[ 1.0691906 ],
[ 1.42558747],
[ 1.17885117],
[ 0.84986945]])
>>> e.model.body_mass = mb
>>> e.model.body_mass
array([[ 0. ],
[ 7.7777 ],
[ 1.53524804],
[ 1.58093995],
[ 1.0691906 ],
[ 1.42558747],
[ 1.17885117],
[ 0.84986945]]) |
@aravindr93 does this workaround work for you? |
model parameters are now read-write! (for better or worse) Note that this doesn't guarantee MuJoCo detects or respects the update. I recommend comparing dynamically changed models to re-loaded XML models, to ensure the behavior is sufficiently similar. |
@machinaut Hi, could you explain more by read-write? does it mean you can change model parameters after you created the environment in python? |
Here's an example, where the gravity vector (a model parameter) is changed on-the-fly, resulting in a ball rolling around the floor: https://gist.github.com/machinaut/9bd1473c763554086c176d39062700b0 Read-write is just how we handle the accessing the memory which model parameters are stored. The arrays (e.g. in the given example |
So I tried emulating the things that were suggested above and this is what I notice:
Is there a way to make the model parameters (such as body_mass and so on) read-write with the latest version of mujoco-py? Thanks for the help! |
My observations are the same as vigneshramk. @aravindr93 @sarvjeetsinghghotra would you care to share how you eventually handled the parameter changes for your study back in 2016? Was your implementation based on mujoco-py 0.5.7? Thank you in advance! |
@algebraic-mouse I was able to modify the parameters in-place, like model.opt.gravity[-1] = 0 |
i was able to modify all xml parameters(mass,geom,...) with mujoco-py 1.50 . |
was this done using mujoco-1.50 ? |
Hi! |
You can change many of the parameters in the model on the fly and a lot of it works. I've used it using mujoco_py as well. You can expect some wierd behavior though if you change certain parameters on the fly during simulation. Some other things such as meshes and probably other object definitions can not be changed because they are compiled at runtime so changes have no effect. |
thank you for your comment. |
I have tried this way and it works. Thank you. |
@Likangxidian |
@yusukeurakami |
Hey, my gym version and mujoco-py version are 0.10.5 and 0.5.7, respectively. You may try it and it works in my computer.
…------------------ 原始邮件 ------------------
发件人: "Yusuke Urakami"<notifications@github.com>;
发送时间: 2020年3月21日(星期六) 中午1:15
收件人: "openai/gym"<gym@noreply.github.com>;
抄送: "江山何处奏管弦"<xdlk@foxmail.com>;"Mention"<mention@noreply.github.com>;
主题: Re: [openai/gym] How to change the parameters of a mujoco model on the fly? (#234)
@Likangxidian
What version of Mujoco-py and Gym are you using??
I am using Mujoco-py 2.0.2.5 and Gym 0.15.7 but it returns "AttributeError: attribute 'body_mass' of 'mujoco_py.cymj.PyMjModel' objects is not writable".
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
is it also possible to change friction related parameters such as |
I asked the same question on Gitter channel but didn't get a solution. So I am putting it here.
By parameters I mean like the gear of a motor, length of a part of the agent, friction etc. Is there a way to change these on the fly through mujoco-py?
The text was updated successfully, but these errors were encountered: