-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengym_.py
44 lines (28 loc) · 1004 Bytes
/
opengym_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""OpenGym .ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1V2mbyHyGSD_ON8gABIz4Bve-ygTgaSz7
## **IMPORT**
"""
pip install gym
"""# Create the CartPole environment"""
import gym
env = gym.make('CartPole-v1')
# Reset the environment to get the initial state
observation = env.reset()
"""# Run the environment for 100 timesteps
"""
for _ in range(100):
# Render the current state (comment this line out if you don't want to visualize)
env.render()
# Take a random action (0 for pushing the cart left, 1 for pushing the cart right)
action = env.action_space.sample()
# Step the environment by taking the chosen action
observation, reward, done, info = env.step(action)
# If the episode is done, reset the environment
if done:
print("Episode finished after {} timesteps".format(_ + 1))
observation = env.reset()
# Close the environment
env.close()