You are writing a 2D simulation of a differential drive robot in python jax. You want to…
- User
You are writing a 2D simulation of a differential drive robot in python jax. You want to run 1000 robots at the same time, so the code should be vectorised.
Assistant
Here is a possible vectorized simulation of 1000 differential drive robots in Jax:
import jax.numpy as np from jax import grad, jit # Robot parameters N = 1000 # Number of robots L = 1.0 # Wheelbase of each robot R = 0.5 # Wheel radius of each robot # State of each robot x = np.zeros((N, 2)) # Position (x, y) theta = np.zeros(N) # Orientation v_l = np.zeros(N) # Left wheel velocity v_r = np.zeros(N) # Right wheel velocity # Dynamics function @jit def dynamics(x, theta, v_l, v_r): # Vectorized for N robots dx = v_l * np.cos(theta) + v_r * np.sin(theta) dtheta = (v_r - v_l) / L return x + dx, theta + dtheta # Update the simulation v_l = 0.5 # Set left wheel velocity v_r = 0.6 # Set right wheel velocity for i in range(100): # Simulate for 100 time steps x, theta = dynamics(x, theta, v_l, v_r) # Plot the final positions of all robots import matplotlib.pyplot as plt plt.plot(x[:, 0], x[:, 1], 'o') plt.axis('equal') plt.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.