-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionPlot.py
More file actions
61 lines (46 loc) · 1.8 KB
/
Copy pathactionPlot.py
File metadata and controls
61 lines (46 loc) · 1.8 KB
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
from complexPendulum.agents.neuralAgents import *
from complexPendulum.assets import *
from complexPendulum.agents import LQAgent
from complexPendulum.agents.NeuralAgent import NeuralAgent
from complexPendulum.envs import ComplexPendulum
import matplotlib.pyplot as plt
import numpy as np
def actionplot(model1, model2, name1: str, name2: str) -> None:
"""
Method that compares two policies within a reduced state space by plotting the corresponding actions.
Inputs:
model1: LQAgent
The model to compare against.
model2: NeuralAgent
The learned model.
name1: str
Name of 1. model.
name2: str
Name of 2. model.
"""
fig = plt.figure()
ax = plt.axes(projection='3d')
theta = np.arange(-0.25, 0.25, 0.05)
x = np.arange(-0.4, 0.4, 0.05)
xs, ts = np.meshgrid(x, theta)
ts = ts.flatten()
xs = xs.flatten()
Z1 = [np.clip(-model1.predict(np.array([xs[i], 0, ts[i], 0])) @ np.array([xs[i], 0, ts[i], 0]), -0.5, 0.5) for i in range(0, len(ts))]
Z2 = [model2.predict(np.array([xs[i], 0, ts[i], 0]))[0] for i in range(0, len(ts))]
ax.plot_trisurf(xs, ts, Z1, edgecolor='none', color='tab:green', label=name1)
ax.plot_trisurf(xs, ts, Z2, edgecolor='none', color='tab:blue', label=name2)
ax.set_xlabel('x')
ax.set_ylabel('θ')
ax.set_zlabel('pwm')
ax.view_init(elev=10., azim=-40)
plt.legend()
plt.xticks(np.arange(-0.4, 0.42, 0.4))
plt.yticks(np.array([-0.25, 0, 0.25]))
ax.set_zticks(np.array([-0.5, 0, 0.5]))
plt.show()
if __name__ == "__main__":
plt.rc('font', size=13)
plt.rcParams["figure.figsize"] = (4,3)
a1 = LQAgent(ComplexPendulum(Q=Setup3.Q, R=Setup3.R))
a2 = NeuralAgent(DirectQR3, None)
actionplot(a1, a2, 'LQ3', 'QR3')