-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcartpole.py
42 lines (30 loc) · 1.08 KB
/
cartpole.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
"""Cartpole controller that interacts with Blender environment through OpenAI.
Run
python cartpole.py
from this directory.
Note, the custom environment is registered in `cartpole_gym/__init__.py`. The
file `cartpole_gym/cartpole_env.py` defines a OpenAI compatible Blender environment.
The actual environment logic is implemented in Blender script
`cartpole_gym/cartpole.blend.py` and scene `cartpole_gym/cartpole.blend`.
"""
import gym
import cartpole_gym
KAPPA = 30
def control(obs):
# Simple P controller defining the error as xpole-xcart
xcart, xpole, _ = obs
return (xpole - xcart) * KAPPA
def main():
# Create the environment. The environment is registered in
# `cartpole_gym/__init__.py`. Set `real_time=True` when you
# want to keep the simulation running until the agent response.
env = gym.make("blendtorch-cartpole-v0", real_time=False)
obs = env.reset()
while True:
obs, reward, done, info = env.step(control(obs))
env.render()
if done:
obs = env.reset()
env.done()
if __name__ == "__main__":
main()