-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqcqp.py
executable file
·181 lines (163 loc) · 9.06 KB
/
qcqp.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 19:37:27 2020
@author: quentin
"""
import torch
from torch.autograd import Function, Variable
import torch.nn as nn
torch.set_default_dtype(torch.double)
import torch.autograd.profiler as profiler
from diffqcqp import solveQP, solveBoxQP, solveQCQP, solveDerivativesQP, solveDerivativesBoxQP, solveDerivativesQCQP, solveSignedBoxQP
import time
import timeit
class QPFn2(Function):
@staticmethod
def forward(ctx,P,q,warm_start,eps,max_iter,mu_prox =1e-7):
batch_size = q.size()[0]
l_2 =torch.zeros(q.size())
adaptative_rho =True
Pi, qi, warm_starti = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi,qi,warm_starti = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(), warm_start[i,:,:].detach().numpy()
l_2[i,:,0] = torch.from_numpy(solveQP(Pi,qi, warm_starti, eps,mu_prox,max_iter, adaptative_rho))
ctx.save_for_backward(P,q,l_2)
return l_2
@staticmethod
def backward(ctx, grad_l):
'''
Compute derivatives of the solution of the QCQP with respect to
'''
P,q,l = ctx.saved_tensors
batch_size = q.size()[0]
grad_P, grad_q = None, None
dl = torch.zeros(l.size())
Pi,qi,li,grad_li = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi,qi,li, grad_li = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(), l[i,:,:].detach().numpy(),grad_l[i,:,:].detach().numpy()
dl[i,:,0] = torch.from_numpy(solveDerivativesQP(Pi,qi,li,grad_li))
if ctx.needs_input_grad[0]:
grad_P = -torch.bmm(dl, torch.transpose(l,1,2))
if ctx.needs_input_grad[1]:
grad_q = - dl
return grad_P, grad_q, None, None, None, None
class BoxQPFn2(Function):
@staticmethod
def forward(ctx,P,q,l_min,l_max,warm_start,eps,max_iter,mu_prox =1e-7):
batch_size = q.size()[0]
l_2 =torch.zeros(q.size())
adaptative_rho =True
Pi, qi, l_mini, l_maxi, warm_starti = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi,qi,l_mini,l_maxi,warm_starti = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(), l_min[i,:,:].detach().numpy(), l_max[i,:,:].detach().numpy(), warm_start[i,:,:].detach().numpy()
l_2[i,:,0] = torch.from_numpy(solveBoxQP(Pi, qi, l_mini, l_maxi, warm_starti, eps, mu_prox, max_iter, adaptative_rho))
ctx.save_for_backward(P, q, l_min, l_max, l_2)
return l_2
@staticmethod
def backward(ctx, grad_l):
'''
Compute derivatives of the solution of the QCQP with respect to
'''
P,q,l_max,l_min,l = ctx.saved_tensors
batch_size = q.size()[0]
grad_P, grad_q, grad_l_min, grad_l_max = None, None, None, None
dl = torch.zeros(l.size())
dgamma = torch.zeros(batch_size, 2*l.size(1), 1)
gamma = torch.zeros(batch_size, 2*l.size(1), 1)
Pi,qi,l_mini,l_maxi,li,grad_li = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi, qi, l_mini, l_maxi, li, grad_li = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(),l_min[i,:,:].detach().numpy(), l_max[i,:,:].detach().numpy(), l[i,:,:].detach().numpy(),grad_l[i,:,:].detach().numpy()
dlgamma, gammai = solveDerivativesBoxQP(Pi,qi, l_mini, l_maxi,li,grad_li)
dlgamma = torch.from_numpy(dlgamma)
dl[i,:,0] = dlgamma[2*l.size(1):]
dgamma[i,:,0] = dlgamma[:2*l.size(1)]
gamma[i,:,0] = torch.tensor(gammai)
if ctx.needs_input_grad[0]:
grad_P = -torch.bmm(dl, torch.transpose(l,1,2))
if ctx.needs_input_grad[1]:
grad_q = - dl
if ctx.needs_input_grad[2]:
grad_l_min = - torch.bmm(dgamma[:,:l.size(1),:].asDiagonal(),gamma[:,:l.size(1),:])
if ctx.needs_input_grad[3]:
grad_l_max = - torch.bmm(dgamma[:,l.size(1):,:].asDiagonal(),gamma[:,l.size(1):,:])
return grad_P, grad_q, grad_l_min, grad_l_max, None, None, None, None
class SignedBoxQPFn2(Function):
@staticmethod
def forward(ctx,P,q,l_min,l_max,v,warm_start,eps,max_iter,mu_prox =1e-7):
batch_size = q.size()[0]
l_2 =torch.zeros(q.size())
adaptative_rho =True
Pi, qi, l_mini, l_maxi, vi, warm_starti = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1), torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi,qi,l_mini,l_maxi,vi,warm_starti = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(), l_min[i,:,:].detach().numpy(), l_max[i,:,:].detach().numpy(), v[i,:,:].detach().numpy(), warm_start[i,:,:].detach().numpy()
l_2[i,:,0] = torch.from_numpy(solveSignedBoxQP(Pi, qi, l_mini, l_maxi, vi, warm_starti, eps, mu_prox, max_iter, adaptative_rho))
ctx.save_for_backward(P, q, l_min, l_max, v, l_2)
return l_2
@staticmethod
def backward(ctx, grad_l): #npt implemented
'''
Compute derivatives of the solution of the QCQP with respect to
'''
P,q,l_max,l_min,l = ctx.saved_tensors
batch_size = q.size()[0]
grad_P, grad_q, grad_l_min, grad_l_max = None, None, None, None
dl = torch.zeros(l.size())
dgamma = torch.zeros(batch_size, 2*l.size(1), 1)
gamma = torch.zeros(batch_size, 2*l.size(1), 1)
Pi,qi,l_mini,l_maxi,li,grad_li = torch.zeros(1,P.size()[1],P.size()[2]),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1),torch.zeros(1,P.size()[2],1)
for i in range(batch_size):
Pi, qi, l_mini, l_maxi, li, grad_li = P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(),l_min[i,:,:].detach().numpy(), l_max[i,:,:].detach().numpy(), l[i,:,:].detach().numpy(),grad_l[i,:,:].detach().numpy()
dlgamma, gammai = solveDerivativesBoxQP(Pi,qi, l_mini, l_maxi,li,grad_li)
dlgamma = torch.from_numpy(dlgamma)
dl[i,:,0] = dlgamma[2*l.size(1):]
dgamma[i,:,0] = dlgamma[:2*l.size(1)]
gamma[i,:,0] = torch.tensor(gammai)
if ctx.needs_input_grad[0]:
grad_P = -torch.bmm(dl, torch.transpose(l,1,2))
if ctx.needs_input_grad[1]:
grad_q = - dl
if ctx.needs_input_grad[2]:
grad_l_min = - torch.bmm(dgamma[:,:l.size(1),:].asDiagonal(),gamma[:,:l.size(1),:])
if ctx.needs_input_grad[3]:
grad_l_max = - torch.bmm(dgamma[:,l.size(1):,:].asDiagonal(),gamma[:,l.size(1):,:])
return grad_P, grad_q, grad_l_min, grad_l_max, None, None, None, None, None
class QCQPFn2(Function):
@staticmethod
def forward(ctx,P,q,l_n,mu,warm_start,eps,max_iter,mu_prox =1e-7):
durations = {"power iter":[], "iters":[], "l update":[],"u update":[],"res update":[], "batch prox":[]}
batch_size = q.size()[0]
l_2 =torch.zeros(q.size())
adaptative_rho =True
for i in range(batch_size):
t0 = time.time()
l_2[i,:,0] = torch.from_numpy(solveQCQP(P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(),l_n[i,:,:].detach().numpy(), mu[i,:,:].detach().numpy(), warm_start[i,:,:].detach().numpy(), eps, mu_prox, max_iter,adaptative_rho))
ctx.save_for_backward(P,q,l_n,mu,l_2)
return l_2
@staticmethod
def backward(ctx, grad_l):
'''
Compute derivatives of the solution of the QCQP with respect to
'''
P,q,l_n,mu,l = ctx.saved_tensors
num_contact = mu.size()[1]
batch_size = q.size()[0]
grad_P, grad_q, grad_l_n, grad_mu = None, None, None, None
dl = torch.zeros(l.size())
dgamma = torch.zeros(l_n.size())
E1,E2 = torch.zeros((batch_size,num_contact,num_contact)), torch.zeros((batch_size,num_contact,num_contact))
for i in range(batch_size):
E1_i,E2_i,dlgamma = solveDerivativesQCQP(P[i,:,:].detach().numpy(),q[i,:,:].detach().numpy(),l_n[i,:,:].detach().numpy(),mu[i,:,:].detach().numpy(),l[i,:,:].detach().numpy(),grad_l[i,:,:].detach().numpy())
dlgamma = torch.from_numpy(dlgamma)
dl[i,:,0] = dlgamma[num_contact:]
dgamma[i,:,0] = dlgamma[:num_contact]
E1[i,:,:],E2[i,:,:] = torch.tensor(E1_i),torch.tensor(E2_i)
if ctx.needs_input_grad[0]:
grad_P = -torch.bmm(dl, torch.transpose(l,1,2))
if ctx.needs_input_grad[1]:
grad_q = - dl
if ctx.needs_input_grad[2]:
grad_l_n = torch.bmm(E2,dgamma) #avoid transposing by directly returning E2^T and E1^T
if ctx.needs_input_grad[3]:
grad_mu = torch.bmm(E1,dgamma)
return grad_P, grad_q, grad_l_n, grad_mu, None, None, None, None