Accelerating Mujoco Simulations in Unity #2502
-
IntroHello! I am a Unity hobbyist learning about Mujoco for Unity. My setupI am using the Mujoco for Unity version 3.3.0. I am developing on M2 Macbook, running on Sequoia 15.3.1 My questionI'm using Mujoco for Unity to run physics simulations in my project, where a model is trained within a Mujoco physics environment. To speed up training, I needed a way to advance the simulation manually rather than relying on Unity’s standard
# MjScene.cs
protected unsafe void FixedUpdate() {
// preUpdateEvent?.Invoke(this, new MjStepArgs(Model, Data));
// StepScene();
// postUpdateEvent?.Invoke(this, new MjStepArgs(Model, Data));
}
public unsafe void ManualStep() {
preUpdateEvent?.Invoke(this, new MjStepArgs(Model, Data));
StepScene();
postUpdateEvent?.Invoke(this, new MjStepArgs(Model, Data));
} I then do the following in a separate class: # TrainingManager.cs
// Training Simulation (Coroutine-Based)
IEnumerator TrainingLoop() {
while (!terminated) {
// Manually step the simulation
MjScene.Instance.ManualStep();
var observations = environment.GetObservations();
if (environment.ShouldTerminate(observations))
environment.Reset();
break;
environment.UpdateReward(observations);
var actions = agent.GetActions(observations);
environment.ApplyActions(actions);
// In the real code, I yield ~30 times per second.
yield return null;
}
}
// "Real" Simulation (FixedUpdate-Based)
void FixedUpdate() {
// Skip FixedUpdate while training
if (isTraining || agent == null) return;
// Manually step the simulation (same as in training)
MjScene.Instance.ManualStep();
var observations = environment.GetObservations();
if (environment.ShouldTerminate(observations)) {
environment.Reset();
return;
}
environment.UpdateReward(observations);
var actions = agent.GetActions(observations);
environment.ApplyActions(actions);
} I don't modify ProblemEven though both the training and real environments use the same Questions
Minimal model and/or code that explain my questionNo response Confirmations
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @Balint-H , could I get your thoughts on this? |
Beta Was this translation helpful? Give feedback.
The first thing that comes to mind is upping the time scale (essentially calls FixedUpdate more frequently than what dT would indicate). For single-environment scenes 2 or 3 times speed should be managed easily on most systems and models.
Let me think about what might cause the coroutine method to have different results. Does any non-mujoco script or component impact the observations/rewards?