-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_following_NN.py
165 lines (140 loc) · 4.91 KB
/
path_following_NN.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
from dreal import *
from Functions import *
import torch
import torch.nn.functional as F
import numpy as np
import timeit
class Net(torch.nn.Module):
def __init__(self,n_input,n_hidden,n_output,lqr):
super(Net, self).__init__()
torch.manual_seed(2)
self.layer1 = torch.nn.Linear(n_input, n_hidden)
self.layer2 = torch.nn.Linear(n_hidden,n_output)
self.control = torch.nn.Linear(n_input,1,bias=False)
self.control.weight = torch.nn.Parameter(lqr)
def forward(self,x):
sigmoid = torch.nn.Tanh()
h_1 = sigmoid(self.layer1(x))
out = sigmoid(self.layer2(h_1))
u = self.control(x)
return out,u
def f_value(x,u):
v = 6
l = 1
y = []
for r in range(0,len(x)):
f = [v*torch.sin(x[r][1]),
v*torch.tan(u[r][0])/l -(torch.cos(x[r][1])/(1-x[r][0]))]
y.append(f)
y = torch.tensor(y)
return y
'''
For learning
'''
N = 500 # sample size
D_in = 2 # input dimension
H1 = 6 # hidden dimension
D_out = 1 # output dimension
torch.manual_seed(10)
lqr = torch.tensor([[-0.8471 , -1.6414]]) # lqr solution
x = torch.Tensor(N, D_in).uniform_(-1, 1)
x_0 = torch.zeros([1, 2])
x = torch.cat((x, x_0), 0)
'''
For verifying
'''
x1 = Variable("x1")
x2 = Variable("x2")
vars_ = [x1,x2]
v = 6
l = 1
config = Config()
config.use_polytope_in_forall = True
config.use_local_optimization = True
config.precision = 1e-2
epsilon = 0
# Checking candidate V within a ball around the origin (ball_lb ≤ sqrt(∑xᵢ²) ≤ ball_ub)
ball_lb = 0.2
ball_ub = 0.8
out_iters = 0
valid = False
while out_iters < 2 and not valid:
start = timeit.default_timer()
lqr = torch.tensor([[-23.58639732, -5.31421063]]) # lqr solution
model = Net(D_in,H1, D_out,lqr)
L = []
i = 0
t = 0
max_iters = 2000
learning_rate = 0.01
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
while i < max_iters and not valid:
V_candidate, u = model(x)
X0,u0 = model(x_0)
f = f_value(x,u)
Circle_Tuning = Tune(x)
# Compute lie derivative of V : L_V = ∑∂V/∂xᵢ*fᵢ
L_V = torch.diagonal(torch.mm(torch.mm(torch.mm(dtanh(V_candidate),model.layer2.weight)\
*dtanh(torch.tanh(torch.mm(x,model.layer1.weight.t())+model.layer1.bias)),model.layer1.weight),f.t()),0)
# With tuning
Lyapunov_risk = (F.relu(-V_candidate)+ 2*F.relu(L_V+0.8)).mean()\
+1.5*((Circle_Tuning-V_candidate).pow(2)).mean()+ 1.2*(X0).pow(2)
print(i, "Lyapunov Risk=",Lyapunov_risk.item())
L.append(Lyapunov_risk.item())
optimizer.zero_grad()
Lyapunov_risk.backward()
optimizer.step()
w1 = model.layer1.weight.data.numpy()
w2 = model.layer2.weight.data.numpy()
b1 = model.layer1.bias.data.numpy()
b2 = model.layer2.bias.data.numpy()
q = model.control.weight.data.numpy()
# Falsification
if i % 10 == 0:
u_NN = (q.item(0)*x1 + q.item(1)*x2)
f = [v*sin(x2),
v*tan(u_NN)/l -(cos(x2)/(1-x1))]
# Candidate V
z1 = np.dot(vars_,w1.T)+b1
a1 = []
for j in range(0,len(z1)):
a1.append(tanh(z1[j]))
z2 = np.dot(a1,w2.T)+b2
V_learn = tanh(z2.item(0))
print('===========Verifying==========')
start_ = timeit.default_timer()
result= CheckLyapunov(vars_, f, V_learn, ball_lb, ball_ub, config,epsilon)
stop_ = timeit.default_timer()
if (result):
print("Not a Lyapunov function. Found counterexample: ")
print(result)
x = AddCounterexamples(x,result,10)
else:
valid = True
print("Satisfy conditions!!")
print(V_learn, " is a Lyapunov function.")
t += (stop_ - start_)
print('==============================')
i += 1
stop = timeit.default_timer()
np.savetxt("w1_p.txt", model.layer1.weight.data, fmt="%s")
np.savetxt("w2_p.txt", model.layer2.weight.data, fmt="%s")
np.savetxt("b1_p.txt", model.layer1.bias.data, fmt="%s")
np.savetxt("b2_p.txt", model.layer2.bias.data, fmt="%s")
np.savetxt("q_p.txt", model.control.weight.data, fmt="%s")
print('\n')
print("Total time: ", stop - start)
print("Verified time: ", t)
out_iters+=1
epsilon = -0.00001
start_ = timeit.default_timer()
result = CheckLyapunov(vars_, f, V_learn, ball_lb, ball_ub, config, epsilon)
stop_ = timeit.default_timer()
if (result):
print("Not a Lyapunov function. Found counterexample: ")
print(result)
else:
print("Satisfy conditions with epsilon= ",epsilon)
print(V_learn, " is a Lyapunov function.")
t += (stop_ - start_)