-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgym.py
More file actions
33 lines (24 loc) · 816 Bytes
/
Copy pathgym.py
File metadata and controls
33 lines (24 loc) · 816 Bytes
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
import gym # pip install -U gym pygame
# https://gym.openai.com/docs/
env = gym.make('CartPole-v1')
env.action_space # Discrete(2)
obs = env.reset() # [-0.01258566, -0.00156614, 0.04207708, -0.00180545]
env.render()
img = env.render(mode='rgb_array')
img.shape # (800, 1200, 3) - height, width, channels (3 = Red, Green, Blue)
action = 1 # accelerate right
obs, reward, done, info = env.step(action)
obs # [-0.01261699, 0.19292789, 0.04204097, -0.28092127]
reward # 1.0
done # False
info #{}
# basic example
env = gym.make('CartPole-v1')
observation = env.reset()
for _ in range(1000):
env.render()
action = env.action_space.sample() # your agent here (this takes random actions)
observation, reward, done, info = env.step(action)
if done:
observation = env.reset()
env.close()