mujoco_mpc icon indicating copy to clipboard operation
mujoco_mpc copied to clipboard

Proposal: ComposeOverrideModel() in Task to allow programmingly constructed model building using mjSpec API

Open Tadinu opened this issue 1 year ago • 0 comments

Hi MuJoCo team & everyone,

  • Given the current state that, MjMPC could not be easily updated to using the latest mujoco + menagerie repos, largely due to manually crafted XML patches for tasks, it would be nice to allow custom model building per task using mjSpec API

  • An example implementation for that could be as follows: In Task class:

  // Compose & configue override model
  UniqueMjModel ComposeOverrideModel() {
    mjModel* model = ConstructModel();
    model_programmingly_built_ = static_cast<bool>(model);
    if (model) {
      ConfigureModel(model);
      return {model, mj_deleteModel};
    }
    return {nullptr, nullptr};
  }

  // Programmingly construct model
  virtual mjModel* ConstructModel() {
    // Construct model from either or both of pre-created XML & mjSpec API
    return nullptr;
  }

  // Customizingly configure model options (timestep, gravity, etc.)
  virtual void ConfigureModel(mjModel* model) {
  }

agent.h:

// Currently selected task on GUI
Task* GuiTask() const { return tasks_[gui_task_id].get(); }

app.cc:

mjModel* LoadModel(const mjpc::Agent* agent, mj::Simulate& sim) {
   // Set sim.agent's [model_override_] from task, but only if not already provided
   if (nullptr == sim.agent->GetOverrideModel()) {
      sim.agent->OverrideModel(sim.agent->GuiTask()->ComposeOverrideModel());
   }

   // Then load the model
   mjpc::Agent::LoadModelResult load_model = sim.agent->LoadModel();
   ...
}

Would love to hear your thoughts! Thanks

Tadinu avatar Feb 14 '25 19:02 Tadinu