Change State and Model In Mujoco-Py and Sim Forward

Discussion in 'Simulation' started by John, Jan 15, 2020.

  1. Hi,

    My question is about mujoco-py so I understand it may not be able to be answered here but perhaps I can have some guidance about approaches to explore.

    I would like to take the state of simulation and change another simulation to that state so I can simulate forward in time without taking a step in the first environment.

    I've seen this question[1], this question[2], this question[3], and this question[4].

    It seems like I need to edit the qpos and qvel but I don't think that everything I need is stored in those two.

    The environment I'm using has 2 categories of state and 1 model parameter I would like to change:
    1. Position and velocities of robot joints
    2. Position and velocity of robot center
    3. Position of objects in scene (these are static)

    I believe that 1. is stored in qpos and qvel and I have changed that successfully by calling

    ```
    self.sim.set_state(other_sim.sim.get_state())
    ```
    to change 2. and 3. I edit the data directly
    ```
    self.data.body_xpos.flags.writeable = True
    self.data.body_xquat.flags.writeable = True
    self.data.body_xmat.flags.writeable = True
    self.data.body_xvelp.flags.writeable = True
    self.data.body_xvelr.flags.writeable = True
    self.data.geom_xpos.flags.writeable = True
    self.data.geom_xmat.flags.writeable = True
    self.data.geom_xvelp.flags.writeable = True
    self.data.geom_xvelr.flags.writeable = True

    self.data.body_xpos[0:] = state['body_xpos'] # copy of other simulation's body xpos etc.
    self.data.body_xquat[0:] = state['body_xquat']
    self.data.body_xmat[0:] = state['body_xmat']
    self.data.body_xvelp[0:] = state['body_xvelp']
    self.data.body_xvelr[0:] = state['body_xvelr']

    self.data.geom_xpos[0:] = state['geom_xpos']
    self.data.geom_xmat[0:] = state['geom_xmat']
    self.data.geom_xvelp[0:] = state['geom_xvelp']
    self.data.geom_xvelr[0:] = state['geom_xvelr']
    ```

    and I edit the model
    ```
    self.model.body_pos.flags.writeable = True
    self.model.body_quat.flags.writeable = True
    self.model.geom_pos.flags.writeable = True
    self.model.geom_quat.flags.writeable = True
    self.model.body_pos[0:] = state['body_pos']
    self.model.body_quat[0:] = state['body_quat']
    self.model.geom_pos[0:] = state['geom_pos']
    self.model.geom_quat[0:] = state['geom_quat']
    ```

    and step forward the simulation:
    ```
    self.model.compute_subtree()
    self.sim.forward()
    ```

    this seems to successfully accomplish 1 and 3 but after testing sometimes after thousands of steps the robot position and velocity does not agree between the two simulations. Perhaps I am misunderstanding something but I would think I have set all the data necessary.

    I apologize for my ignorance, I am a beginner at robot simulation.

    My questions are:
    1. Is there something else I need to set in order for both simulations to behave the same?
    2. What does self.sim.forward() do?
     
  2. I have zero experience concerning mujoco_py but anything you modify in data other than the state (that is time qpos qvel warmstartdata and act will be overwritten as soon as you call forward kinematics / dynamics ....

    self.data.body_xpos.flags.writeable = True
    self.data.body_xquat.flags.writeable = True
    self.data.body_xmat.flags.writeable = True
    self.data.body_xvelp.flags.writeable = True
    self.data.body_xvelr.flags.writeable = True
    self.data.geom_xpos.flags.writeable = True
    self.data.geom_xmat.flags.writeable = True
    self.data.geom_xvelp.flags.writeable = True
    self.data.geom_xvelr.flags.writeable = True

    self.data.body_xpos[0:] = state['body_xpos'] # copy of other simulation's body xpos etc.
    self.data.body_xquat[0:] = state['body_xquat']
    self.data.body_xmat[0:] = state['body_xmat']
    self.data.body_xvelp[0:] = state['body_xvelp']
    self.data.body_xvelr[0:] = state['body_xvelr']

    self.data.geom_xpos[0:] = state['geom_xpos']
    self.data.geom_xmat[0:] = state['geom_xmat']
    self.data.geom_xvelp[0:] = state['geom_xvelp']
    self.data.geom_xvelr[0:] = state['geom_xvelr']

    Setting these values therefore has zero effect on your simulation.

    If you really want to change the static geometry you have to do this in the mj_model data structure (or its equivalent in mujoco_py).
    Remember that you might have to call mj_setConst() to recompute relevant data.

    2. this -> http://mujoco.org/book/computation.html#piForward