-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
283 lines (196 loc) · 7.86 KB
/
solver.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from shared import Shared
from get_gradient import GetGradient
from optimizer import GradientMethod, MomentumMethod, NesterovMethod
from minimum_energy_control import MinimumEnergyControl
from optimizer import OptimizerForGuidance
from constraints_for_input import ConstraintsForInput
from evaluate import Evaluator
import pycuda.autoinit
import numpy as np
import math
class LeastSquare:
def __init__(self, A, b, learning_rate, beta=0, epoches=10, iteration=5, optimize_method="GD", constrained=None):
## shared
self.shared = Shared(A, b, learning_rate, beta=beta)
## gradient
self.get_gradient = GetGradient(self.shared)
## optimizer
if optimize_method == "GD":
self.optimizer = GradientMethod(self.shared)
elif optimize_method == "momentum":
self.optimizer = MomentumMethod(self.shared)
self.shared.momentum(beta)
elif optimize_method == "Nesterov":
self.optimizer = NesterovMethod(self.shared)
self.shared.nesterov(beta)
else:
return NotImplementedError()
## epoches, iteration
self.epoches = epoches
self.iteration = iteration
## constrained
if constrained == None:
pass
else:
self.shared.constrained_unpacking(constrained)
## error log
self.error = np.zeros(epoches*iteration)
def solve(self):
for epoch in range(self.epoches):
for iter in range(self.iteration):
## get gradient
self.get_gradient.run()
## optimize
self.optimizer.run()
def solve_with_record(self):
for epoch in range(self.epoches):
for iter in range(self.iteration):
## record
self.record_error(epoch, iter)
## get gradient
self.get_gradient.run()
## optimize
self.optimizer.run()
def record_error(self, epoch, iter):
index = epoch * self.iteration + iter
self.get_gradient.initialize()
self.get_gradient.first(self.shared.GPU_out,
self.shared.GPU_A,
self.shared.GPU_theta,
self.shared.GPU_b,
np.int32(self.shared.length),
np.int32(self.shared.width),
block=(self.shared.TPB,1,1),
grid=(self.shared.BPG,1,1))
self.error[index] = np.linalg.norm(self.shared.GPU_out.get())
class MinimumEnergyControlSolver:
def __init__(self, x_des, x_0, upper_boundary, downer_boundary, lambdas, dt=0.1, step=300, learning_rate=1e-3, max_epoch=50, max_iteration=100):
## important constants
self.axis = 3
self.DOF = 6
self.initial_step = step
## step size
self.step = step
## max epoch
self.max_epoch = max_epoch
## max iteration
self.max_iteration = max_iteration
## initialize MEC(minimum energy control)
self.MEC = MinimumEnergyControl(x_des, x_0, dt, lambdas)
## initialize optimizer
self.optimizer = OptimizerForGuidance(self.MEC, learning_rate)
## constraint
self.upper_boundary = upper_boundary
self.downer_boundary = downer_boundary
self.constraint = ConstraintsForInput(self.MEC, self.upper_boundary, self.downer_boundary)
## evaluate
self.evaluator = Evaluator(self.MEC, self.optimizer)
self.error = 0
## initial kernel size
self.TPB = int(math.sqrt(step))
self.iteration = int(math.sqrt(step))
################################################################################
def solve(self):
##define problem: fit matrices for left step
self.define_problem()
## iteration
epoch = 0
while (epoch < self.max_epoch):
## initialize
iteration = 0
## learning
while (iteration < self.max_iteration):
## get gradient
self.MEC.run(self.step)
## optimize
self.optimizer.run(self.step)
## tune learning rate
error = self.evaluator.evaluate_error(self.error,
self.iteration,
self.step,
self.TPB)
self.error = error
iteration += 1
## constraint
self.constraint.projection(self.step)
## evaluate gradient
value = self.evaluator.evaluate_gradient(self.step)
if value:
break
else:
pass
## update
epoch += 1
## free memory
# self.memory_free()
## unpack opt_u, other variables
matrices = self.copy_and_unpack_result()
return matrices["u"], matrices
################################################################################
def solve_all_constraint(self):
##define problem: fit matrices for left step
self.define_problem()
## iteration
epoch = 0
while (epoch < self.max_epoch):
## initialize
iteration = 0
## learning
while (iteration < self.max_iteration):
## get gradient
self.MEC.run(self.step)
## optimize
self.optimizer.run(self.step)
## tune learning rate
error = self.evaluator.evaluate_error(self.error,
self.iteration,
self.step,
self.TPB)
self.error = error
iteration += 1
## constraint
self.constraint.projection(self.step)
## evaluate gradient
value = self.evaluator.evaluate_gradient(self.step)
if value:
break
else:
pass
## update
epoch += 1
## free memory
# self.memory_free()
## unpack opt_u, other variables
matrices = self.copy_and_unpack_result()
return matrices["u"], matrices
################################################################################
def define_problem(self):
## define problem
self.MEC.define_problem(self.step)
## define error vector
self.evaluator.define_error_vector(self.step)
## kernel size
self.TPB, self.iteration = self.define_optimal_kernel_size(self.axis*self.step)
def define_optimal_kernel_size(self, n):
thread_per_block = int(math.sqrt(n / 2))
iteration = int(n / thread_per_block) + 1
return thread_per_block, np.int32(iteration)
################################################################################
def memory_free(self):
self.evaluator.memory_free()
def memory_freeall(self):
try:
self.MEC.memory_freeall()
self.evaluator.memory_free()
except:
pass
################################################################################
def copy_and_unpack_result(self):
## unpack matrix
try:
matrices = self.MEC.copy_and_unpack_result(self.step)
except:
matrices = dict()
## delete all memory
self.memory_freeall()
return matrices