google-deepmind / dm_control

Google DeepMind's software stack for physics-based simulation and Reinforcement Learning environments, using MuJoCo.
Apache License 2.0
3.76k stars 666 forks source link

Changing physics.model attributes? #106

Closed MishaLaskin closed 5 years ago

MishaLaskin commented 5 years ago

I'm wondering if there's an API to change model attributes. For example, take the point mass env - I'd like to change the point mass's color depending on some other variable (e.g. it's position, proximity to target, or reward). Is it possible to do this?

When I try to set physics.model.{some_attribute} = {some_value} explicitly I get a AttributeError: can't set attribute error.

alimuldal commented 5 years ago

Hi Misha,

Most of the properties of physics.model and physics.data are numpy arrays that expose direct views onto the underlying C structs used by MuJoCo. To mutate them you should write into the contents of the array rather than assigning over the top of it. For example:

from dm_control import suite

env = suite.load('cartpole', 'swingup')
print(repr(env.physics.data.qvel))  # (nq,) array of joint velocities
# array([ 0.,  0.])
env.physics.data.qvel[:] = 0.1, 0.2  # Set the velocities of the slide and hinge joints
# env.physics.data.qvel = 0.1, 0.2  # This won't work (AttributeError)

To set just the velocity of the first joint you could write into qvel[0] and so forth. Addressing elements by their indices can be error-prone, so we also provide a way to address them by their names given in the XML. For example:

env.physics.named.data.qvel['slider'] = -0.5
print(repr(env.physics.named.data.qvel))
# FieldIndexer(qvel):
# 0  slider [-0.5     ]
# 1 hinge_1 [ 0.2     ]

See section 5 in our tech report and the task implementations in the dm_control/suite directory for more examples. I'd also recommend taking a look at the MuJoCo docs in case you're unfamiliar with MuJoCo's API (and in particular the distinction between model and data).

MishaLaskin commented 5 years ago

Got it thank you!

On Mon, Jul 1, 2019 at 12:58 PM Alistair Muldal notifications@github.com wrote:

Hi Misha,

Most of the properties of physics.model and physics.data are numpy arrays that expose direct views onto the underlying C structs used by MuJoCo. To mutate them you should write into the contents of the array rather than assigning over the top of it. For example:

from dm_control import suite

env = suite.load('cartpole', 'swingup')print(repr(env.physics.data.qvel)) # (nq,) array of joint velocities# array([ 0., 0.]) env.physics.data.qvel[:] = 0.1, 0.2 # Set the velocities of the slide and hinge joints# env.physics.data.qvel = 0.1, 0.2 # This won't work (AttributeError)

To set just the velocity of the first joint you could write into qvel[0] and so forth. Addressing elements by their indices can be error-prone, so we also provide a way to address them by their names given in the XML. For example:

env.physics.named.data.qvel['slider'] = -0.5print(repr(env.physics.named.data.qvel))# FieldIndexer(qvel):# 0 slider [-0.5 ]# 1 hinge_1 [ 0.2 ]

See section 5 in our tech report https://arxiv.org/pdf/1801.00690.pdf and the task implementations in the dm_control/suite https://github.com/deepmind/dm_control/tree/master/dm_control/suite directory for more examples. I'd also recommend taking a look at the MuJoCo docs http://mujoco.org/book/index.html in case you're unfamiliar with MuJoCo's API (and in particular the distinction between model and data).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/deepmind/dm_control/issues/106?email_source=notifications&email_token=ABHWQWN2PBRMRLNXOQHFWMLP5JOVTA5CNFSM4H4ITIC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY7FYFA#issuecomment-507403284, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHWQWN52NBVD45UHFCZCZDP5JOVTANCNFSM4H4ITICQ .