-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_run_place.py
72 lines (55 loc) · 1.88 KB
/
test_run_place.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# Demonstration of a linear controller using full
# state feedback. See file control_place.py for
# how the gain matrix was calculated.
import gym
import gym_CartPole_BT
import numpy as np
import argparse
# Parse any arguments provided at the command-line
parser = argparse.ArgumentParser(description='Test this gym environment.')
parser.add_argument('-e', '--env', type=str, default='CartPole-BT-dL-v0',
help="gym environment")
parser.add_argument('-s', "--show", help="display output",
action="store_true")
parser.add_argument('-r', "--render", help="render animation",
action="store_true")
args = parser.parse_args()
# Create and initialize environment
if args.show: print(f"\nInitializing environment '{args.env}'...")
env = gym.make(args.env)
env.reset()
# Control vector (shape (1, ) in this case)
u = np.zeros(1)
# Open graphics window and draw animation
if args.render: env.render()
# We will keep track of the cumulative rewards
cum_reward = 0.0
if args.show:
print(f"{'k':>3s} {'u':>5s} {'reward':>6s} {'cum_reward':>10s}")
print("-"*28)
# Gain matrix (K)
# You can estimate this using control_place.py
gain = np.array([-25. , -53.5, 480. , 210. ])
# Run one episode
done = False
while not done:
# Access the full state
x, x_dot, theta, theta_dot = env.state
# Linear quadratic regulator
# u[t] = -Ky[t]
u[:] = -np.dot(gain, env.state - env.goal_state)
# Run simulation one time-step
observation, reward, done, info = env.step(u)
# Update the animation
if args.render: env.render()
# Process the reward
cum_reward += reward
# Print updates
if args.show:
print(f"{env.time_step:3d}: {u[0]:5.1f} {reward:6.2f} "
f"{cum_reward:10.1f}")
if args.render:
input("Press enter to close animation window")
# Close animation window
env.close()