gymnasium.snippets (644B)
1 snippet gym "gym loop" b 2 from itertools import count 3 4 import gymnasium as gym 5 6 7 env = gym.make("${1:CartPole-v1}"`!p 8 snip.rv = ', render_mode="' if t[2] else "" 9 `${2:human}`!p 10 snip.rv = '"' if t[2] else "" 11 `) 12 13 for episode in count(): 14 observation, info = env.reset(`!p snip.rv = "seed=" if t[3] else ""`${3:42}) 15 16 for t in count(): 17 # Select an action 18 action = env.action_space.sample() 19 20 # Perform action in the enviroment 21 next_observation, reward, terminated, truncated, info = env.step( 22 action 23 ) 24 25 $0 26 27 if terminated or truncated: 28 break 29 observation = next_observation 30 31 print(f"{episode=}, {t+1=}") 32 33 env.close() 34 endsnippet