-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractOCProblem.py
More file actions
132 lines (112 loc) · 3.92 KB
/
AbstractOCProblem.py
File metadata and controls
132 lines (112 loc) · 3.92 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import torch
import torch.autograd as autograd
import matplotlib.pyplot as plt
class AbstractOCProblem:
def __init__(self):
return self
def calH(self,s,z,p,u,M=None):
"""
calH = f(s,z,u)'*p - L(s,z,u) + trace(sigma^2 M)
:param s: time
:param z: state
:param p: adjoint state
:param u: control
:param M: adjoint variable in stochastic OC problems
:return: calH
"""
H = - torch.sum(p*self.f(s,z,u),dim=1,keepdim=True) - self.L(s,z,u)
if M is not None:
H = H + self.sigma2_Lap(s,z,M)
return H
def test_Hamiltonian(self,s,z,p,M=None):
"""
Test 1: | calH(s,z,p,u_star) - Hamiltonian(s,z,p) |
Test 2: derivative check for gradpH computed in Hamiltonian
:param s:
:param z:
:param p:
:param M:
:return:
"""
print("\n =============== test_Hamiltonian =============== ")
H_true = self.calH(s, z, p, self.u_star(s, z, p))
assert H_true.dim() == 2
assert H_true.shape[0] == z.shape[0]
assert H_true.shape[1] == 1
H,gradpH = self.Hamiltonian(s,z,p)
assert H.dim() == 2
assert H.shape[0] == z.shape[0]
assert H.shape[1] == 1
assert gradpH.shape[0] == z.shape[0]
assert gradpH.shape[1] == p.shape[1]
err_H = torch.norm(H_true - H)
print("err_H = %1.2e" % (err_H))
dp = torch.randn_like(p)
dd = torch.sum(dp*gradpH,dim=1,keepdim=True)
h_arr, E0_arr, E1_arr = [], [], []
for k in range(20):
h = (0.5)**(k)
Ht = self.Hamiltonian(s,z,p+h*dp)[0]
E0 = torch.norm(H-Ht)
E1 = torch.norm(H + h*dd -Ht)
print("h=%1.2e\tE0=%1.2e\tE1=%1.2e" %(h,E0,E1))
h_arr.append(h)
E0_arr.append(E0)
E1_arr.append(E1)
plt.loglog(h_arr,E0_arr, label='f')
plt.loglog(h_arr,E1_arr, label='gradf')
plt.legend()
plt.title("test_Hamiltonian")
plt.show()
def test_g(self,z):
g,gradg = self.g(z)
# verify sizes
assert g.dim()==2
assert g.shape[0]==z.shape[0]
assert g.shape[1]==1
assert gradg.shape[0]==z.shape[0]
assert gradg.shape[1]==z.shape[1]
dz = torch.randn_like(z)
dgdz = torch.sum(gradg*dz,dim=1,keepdim=True)
h_arr, E0_arr, E1_arr = [], [], []
print("\n =============== test_g =============== ")
for k in range(20):
h = (0.5)**(k)
gt = self.g(z+h*dz)[0]
E0 = torch.norm(g-gt)
E1 = torch.norm(g + h*dgdz -gt)
print("h=%1.2e\tE0=%1.2e\tE1=%1.2e" %(h,E0,E1))
h_arr.append(h)
E0_arr.append(E0)
E1_arr.append(E1)
plt.loglog(h_arr,E0_arr, label='f')
plt.loglog(h_arr,E1_arr, label='gradf')
plt.legend()
plt.title("test_g")
plt.show()
def test_u_star(self,s,z,p):
"""
test the feedback form. Note that
u_star = argmax_u calH(s,z,p,u)
and therefore |\nabla_u calH(s,z,p,u_star)| \approx 0
:param s:
:param z:
:param p:
:return:
"""
# ut = torch.tensor(self.u_star(s,z,p),requires_grad=True)
ut = self.u_star(s,z,p).clone().detach().requires_grad_(True)
H = self.calH(s,z,p,ut)
dH = autograd.grad(H, ut, torch.ones([ut.shape[0], 1], device=ut.device, dtype=ut.dtype), retain_graph=True,
create_graph=True)[0]
err = torch.norm(dH)
# du = torch.randn_like(ut)
# dd = torch.sum(du*dH,dim=1,keepdim=True)
# for k in range(20):
# h = (0.5)**(k)
# Ht = self.calH(s, z, p, ut + h*du)
#
# E0 = torch.norm(H-Ht)
# E1 = torch.norm(H + h*dd -Ht)
# print("h=%1.2e\tE0=%1.2e\tE1=%1.2e" %(h,E0,E1))
return err