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

mjx.put_data() and mjx.put_model() takes lot of time to resolve #1670

Open
AnxSnip opened this issue May 16, 2024 · 4 comments
Open

mjx.put_data() and mjx.put_model() takes lot of time to resolve #1670

AnxSnip opened this issue May 16, 2024 · 4 comments
Labels
question Request for help or information

Comments

@AnxSnip
Copy link

AnxSnip commented May 16, 2024

Hi,

I'm a student and I'm trying to use MuJoCo to model forces applied on a cargo shipment.

I'm looking for some help with the loading of the data from mujoco to mjx.

Every is working fine with the mujoco model and the simulation works well (takes 1 to 10 seconds depending on the force). But I wanted it to be faster so I began using MJX. But I noticed that the conversion to jax array and the jitted step method takes way longer time than the classic mujoco version.

Am I doing something wrong ? or it is just that MJX cannot handle so many shapes?

Here's the code I use:
`
def run_jitted(xml):
duration = 30
jit_step = jax.jit(mjx.step)

mj_model = mujoco.MjModel.from_xml_string(xml)
mj_data = mujoco.MjData(mj_model)
mjx_model = mjx.put_model(mj_model)

mujoco.mj_resetData(mj_model, mj_data)
mjx_data = mjx.put_data(mj_model, mj_data)
while mjx_data.time < duration:
    mjx_data = jit_step(mjx_model, mjx_data)
return mj_model, mjx.get_data(mj_model, mjx_data)

`

Here is a model which explains my question:

https://pastebin.com/W0xQXiFL

@AnxSnip AnxSnip added the question Request for help or information label May 16, 2024
@btaba
Copy link
Collaborator

btaba commented May 16, 2024

Hi @AnxSnip , see here for more info about the performance characteristics of MJX: https://mujoco.readthedocs.io/en/stable/mjx.html#mjx-the-sharp-bits

Seems like you are trying to simulate a bunch of boxes? Try to fiddle with some of these parameters (NB: max contacts, the solver iters):

<option impratio="10" iterations="1" ls_iterations="4" timestep="0.001">
<flag eulerdamp="disable"/>
</option>
<custom>
<numeric data="15" name="max_contact_points"/>
<numeric data="15" name="max_geom_pairs"/>
</custom>

The timestep seems really high, but I notice you use RK4. Consider lowering the timestep and using Newton for MJX. Hope that helps.

@AnxSnip
Copy link
Author

AnxSnip commented May 21, 2024

Hello @btaba, thank you for your quick response.
I am currently trying to do some supervised learning on the stability of a bunch of boxes in a container under acceleration force (as if they were in a truck)
My goal would be to be automatically load a setup from my database to mujoco and then get the state before and after the simulation to compute the stability of the model. The boxes will be under force for around 10 to 30s. But I need it to be able to do fast simulation.
The simulation with this xml takes 10s to complete, but I wanted it to be faster so I looked into MJX but even after 30min it is still not finishing.
I choose these parameters because it seemed to me that they were close to what I expected to happen in real life. But because it is the first time I use physics simulation, I don't really know the meaning of each parameter
I tried changing the values of the parameters you provided but It behave weirdly: some blocks are flying, or exploding without any apparent reason.
If you have any lead or reading that would help me, it will be really kind.
Thank you!

@btaba
Copy link
Collaborator

btaba commented Jun 7, 2024

Hi @AnxSnip , sorry this slipped through. Could you re-paste a full example XML so we can help debug? Looks like the previous link expired

@AnxSnip
Copy link
Author

AnxSnip commented Jun 10, 2024

Hello @btaba, no need to worry, I managed to stabilize my model and realized that handling collisions with multiple items doesn't work well with MJX. So, I think I'll stick with the classic version. However, I now have some questions about how to apply force to a body. I noticed that we can use site and actuator in the XML or xfrc_applied in the code, but there aren't many examples or much documentation on this. Can you point me to some resources?
This is my new model : https://pastebin.com/4xEFzr1r
I want to add movement to the container, ideally at the bottom toward any horizontal direction.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Request for help or information
Projects
None yet
Development

No branches or pull requests

2 participants