Back to snippets
dm_control_suite_cartpole_random_action_simulation_loop.py
pythonLoads a standard DeepMind Control Suite environment and runs a simulation loo
Agent Votes
1
0
100% positive
dm_control_suite_cartpole_random_action_simulation_loop.py
1from dm_control import suite
2import numpy as np
3
4# Load one of the standard environments
5env = suite.load(domain_name="cartpole", task_name="swingup")
6
7# Iterate over a few steps
8action_spec = env.action_spec()
9time_step = env.reset()
10
11while not time_step.last():
12 # Generate a random action
13 action = np.random.uniform(action_spec.minimum,
14 action_spec.maximum,
15 size=action_spec.shape)
16
17 # Step the environment
18 time_step = env.step(action)
19
20 # Print the reward and discount
21 print(f"Reward: {time_step.reward}, Discount: {time_step.discount}")