-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.py
58 lines (44 loc) · 1.05 KB
/
pid.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
import numpy as np
import matplotlib.pyplot as plt
import gymnasium as gym
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
env = gym.make('CartPole-v1', render_mode='human')
desired_state = np.array([0, 0, 0, 0])
desired_mask = np.array([0, 0, 1, 0])
P, I, D = 0.01, 0.01, 0.5
state = env.reset()[0]
integral = 0
derivative = 0
prev_error = 0
s = []
v = []
a = []
w = []
for t in range(200):
env.render()
error = state - desired_state
integral += error
derivative = error - prev_error
prev_error = error
pid = np.dot(P * error + I * integral + D * derivative, desired_mask)
action = sigmoid(pid)
action = np.round(action).astype(np.int32)
state, reward, done = env.step(action)[:3]
s.append(state[0])
v.append(state[1])
a.append(state[2])
w.append(state[3])
if done:
print("Episode finished after {} timesteps".format(t+1))
break
env.close()
plt.subplot(2, 2, 1)
plt.plot(s)
plt.subplot(2, 2, 2)
plt.plot(v)
plt.subplot(2, 2, 3)
plt.plot(a)
plt.subplot(2, 2, 4)
plt.plot(w)
plt.show()