0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mujoco (python)レンダリングサイズによるシミュレーション時間の検証

Posted at

はじめに

レンダリングの有無、画像サイズによってのシミュレーション時間の変化が知りたい

環境

mujoco python

Name: mujoco
Version: 3.1.4

パソコン

Intel(R) Core(TM) i7-10875H CPU @ 2.30GHz
NVIDIA Corporation TU106 [GeForce RTX 2060]

プログラム

レンダリングのオンオフ、レンダリングサイズを切り替えられるように作成

import mujoco
import cv2

TOTAL_TIME = 3.0  # (seconds)
FRAMERATE = 20.0  # (Hz)
IS_RENDERING = True # レンダリングを行なうかどうか
WIDTH = 96
HEIGHT = 96

def show_image(img):
    cv2.imshow('Image', img)
    key = cv2.waitKey(0)
    cv2.destroyAllWindows()

def show_video(frames, fps):
    print(f"Frame size : {len(frames)}, fps : {fps}")
    for img in frames:
        cv2.imshow('Video', img)
        key = cv2.waitKey(int(1.0/fps*1000))

    cv2.destroyAllWindows()

xml = """
<mujoco>

  <worldbody>
    <light name="top" pos="0 0 1"/>
    <body name="box_and_sphere" euler="0 0 -30">
      <joint name="swing" type="hinge" axis="1 -1 0" pos="-.2 -.2 -.2"/>
      <geom name="red_box" type="box" size=".2 .2 .2" rgba="1 0 0 1"/>
      <geom name="green_sphere" pos=".2 .2 .2" size=".1" rgba="0 1 0 1"/>
    </body>
  </worldbody>

  <visual>
    <global offwidth="2000" offheight="2000"/>
  </visual>
</mujoco>
"""

model = mujoco.MjModel.from_xml_string(xml)
data = mujoco.MjData(model)
renderer = mujoco.Renderer(model)

print("Time step", model.opt.timestep)

timestep = model.opt.timestep
total_steps = TOTAL_TIME/timestep
skip_frames = 1.0/FRAMERATE/timestep
if skip_frames < 1:
   print("Too fast framerate")
   exit()
print("Durattion", TOTAL_TIME)
print("Framerate", FRAMERATE)
print("Total steps", total_steps)
print("Skip frames", skip_frames)

frames = []
mujoco.mj_resetData(model, data)

count = 0
import time
print("Sim start")
start_time = time.perf_counter()
while count < total_steps:
  count += 1
  mujoco.mj_step(model, data)

  if IS_RENDERING:
    if count%int(skip_frames) == 0:
        with mujoco.Renderer(model, HEIGHT, WIDTH) as r:
            r.update_scene(data)
            pixels = r.render()
            frames.append(pixels)
print("Sim stop", time.perf_counter() - start_time)

show_video(frames, fps=60)

実験

処理時間は実行毎に少し変動あり

設定1

TOTAL_TIME = 3.0  # (seconds)
FRAMERATE = 20.0  # (Hz)
IS_RENDERING = True
WIDTH = 96
HEIGHT = 96

Sim stop 4.019544602999304

設定2

TOTAL_TIME = 3.0  # (seconds)
FRAMERATE = 20.0  # (Hz)
IS_RENDERING = True
WIDTH = 640
HEIGHT = 480

Sim stop 3.9852752740007418

設定3

TOTAL_TIME = 3.0  # (seconds)
FRAMERATE = 20.0  # (Hz)
IS_RENDERING = True
WIDTH = 2000
HEIGHT = 2000

Sim stop 5.18554060400038

設定4

TOTAL_TIME = 3.0  # (seconds)
FRAMERATE = 20.0  # (Hz)
IS_RENDERING = False

Sim stop 0.002062821000436088

結論

レンダリングサイズの変更では大きく変化しない。
レンダリングの有無により100倍近く早くなる。

センサ系についてもためしてみたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?