forked from araujolma/SOAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobZer.py
340 lines (270 loc) · 9.24 KB
/
probZer.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for the Zermelo Problem, which is a classical minimum-time navigation
problem:
Find the steering program of a boat that navigates from a given initial
position to a given terminal position in minimal time. The stream moves with a
constant velocity and the boat moves with a constant magnitude velocity
relative to the stream.
"""
import numpy
from sgra import sgra
from utils import simp
import matplotlib.pyplot as plt
class prob(sgra):
probName = 'probZer'
def initGues(self,opt={}):
# The parameters that go here are the ones that cannot be simply
# altered from an external configuration file... at least not
# without a big increase in the complexity of the code...
# matrix sizes
n = 2
m = 1
p = 1
q = 4
s = 1
self.n = n
self.m = m
self.p = p
self.q = q
self.s = s
self.Ns = 2*n*s + p
initMode = opt.get('initMode','default')
if initMode == 'default':
N = 5000+1
self.N = N
dt = 1.0/(N-1)
t = numpy.arange(0,1.0+dt,dt)
self.dt = dt
self.t = t
# Payload mass
#self.mPayl = 100.0
#prepare tolerances
tolP = 1.0e-7#8
tolQ = 1.0e-7#5
tol = dict()
tol['P'] = tolP
tol['Q'] = tolQ
self.tol = tol
self.constants['gradStepSrchCte'] = 1.0e-4
# Get initialization mode
x = numpy.zeros((N,n,s))
u = numpy.arctanh(0.5*numpy.ones((N,m,s)))
x[:,0,0] = t.copy()
x[:,1,0] = x[:,0,0]
lam = 0.0*x.copy()
mu = numpy.zeros(q)
pi = numpy.array([1.0])
self.x = x
self.u = u
self.pi = pi
self.lam = lam
self.mu= mu
solInit = self.copy()
self.log.printL("\nInitialization complete.\n")
return solInit
elif initMode == 'extSol':
inpFile = opt.get('confFile','')
# Get parameters from file
self.loadParsFromFile(file=inpFile)
# The actual "initial guess"
N,m,n,p,q,s = self.N,self.m,self.n,self.p,self.q,self.s
x = numpy.zeros((N,n,s))
gama = numpy.zeros((N,m,s))#.25 * numpy.pi * numpy.ones((N,m,s))
u = numpy.arctanh(gama / (numpy.pi))
x[:,0,0] = 5.0 * self.t.copy()
#x[:,1,0] = x[:,0,0]
lam = 0.0 * x.copy()
mu = numpy.zeros(q)
pi = numpy.array([2.5])
self.x = x
self.u = u
self.pi = pi
self.lam = lam
self.mu = mu
solInit = self.copy()
self.log.printL("\nInitialization complete.\n")
return solInit
#%%
def calcPhi(self):
N = self.N
n = self.n
s = self.s
phi = numpy.empty((N,n,s))
u = self.u
pi = self.pi
gama = numpy.pi * numpy.tanh(u)
phi[:,0,0] = pi[0] * (numpy.cos(gama[:,0,0]) + 1.0)
phi[:,1,0] = pi[0] * numpy.sin(gama[:,0,0])
return phi
#%%
def calcGrads(self,calcCostTerm=False):
Grads = dict()
N = self.N
n = self.n
m = self.m
p = self.p
q = self.q
s = self.s
#q = sizes['q']
#N0 = sizes['N0']
u = self.u
pi = self.pi
gama = 0.5 * numpy.pi * numpy.tanh(u)
# Pre-assign functions
Grads['dt'] = 1.0/(N-1)
phix = numpy.zeros((N,n,n,s))
if p>0:
phip = numpy.zeros((N,n,p,s))
else:
phip = numpy.zeros((N,n,1,s))
phiu = numpy.zeros((N,n,m,s))
sinGama = numpy.sin(gama[:,0,0])
cosGama = numpy.cos(gama[:,0,0])
dGama_du = (1.0-numpy.tanh(u[:,0,0])**2) * numpy.pi
phiu[:,0,0,0] = - pi[0] * sinGama * dGama_du
phiu[:,1,0,0] = pi[0] * cosGama * dGama_du
phip[:,0,0,0] = cosGama + 1.0
phip[:,1,0,0] = sinGama
psiy = numpy.zeros((q,2*n*s))
psiy[0,0] = 1.0
psiy[1,1] = 1.0
psiy[2,2] = 1.0
psiy[3,3] = 1.0
if p>0:
psip = numpy.zeros((q,p))
else:
psip = numpy.zeros((q,1))
fx = numpy.zeros((N,n,s))
fu = numpy.zeros((N,m,s))
if p>0:
fp = numpy.zeros((N,p,s))
else:
fp = numpy.zeros((N,1,s))
fp[:,0,0] = numpy.ones(N)
Grads['phix'] = phix
Grads['phiu'] = phiu
Grads['phip'] = phip
Grads['fx'] = fx
Grads['fu'] = fu
Grads['fp'] = fp
# Grads['gx'] = gx
# Grads['gp'] = gp
Grads['psiy'] = psiy
Grads['psip'] = psip
return Grads
#%%
def calcPsi(self):
x = self.x
N = self.N
return numpy.array([x[0,0,0],\
x[0,1,0],\
x[N-1,0,0]-5.0,\
x[N-1,1,0]-5.0])
def calcF(self):
N,s = self.N,self.s
f = numpy.empty((N,s))
for arc in range(s):
f[:,arc] = self.pi[arc] * numpy.ones(N)
return f, f, 0.0*f
def calcI(self):
# N,s = self.N,self.s
# f, _, _ = self.calcF()
#
# Ivec = numpy.empty(s)
# for arc in range(s):
# Ivec[arc] += simp(f[:,arc],N)
Ivec = self.pi
I = Ivec.sum()
return I, I, 0.0
#%%
def plotSol(self,opt={},intv=None,piIsTime=True,mustSaveFig=True,\
subPlotAdjs={}):
t = self.t
x = self.x
u = self.u
pi = self.pi
gama = numpy.pi * numpy.tanh(u)
if intv is None:
intv = numpy.arange(0,self.N,1,dtype='int')
else:
intv = list(intv)
if opt.get('mode','sol') == 'sol':
I,_,_ = self.calcI()
titlStr = "Current solution: I = {:.4E}".format(I) + \
" P = {:.4E} ".format(self.P) + " Q = {:.4E} ".format(self.Q)
titlStr += "\n(grad iter #" + str(self.NIterGrad) + ")"
plt.subplot2grid((8,4),(0,0),colspan=5)
plt.plot(t[intv],x[intv,0,0],)
plt.grid(True)
plt.ylabel("x")
plt.title(titlStr)
plt.subplot2grid((8,4),(1,0),colspan=5)
plt.plot(t[intv],x[intv,1,0],'g')
plt.grid(True)
plt.ylabel("y")
plt.subplot2grid((8,4),(2,0),colspan=5)
plt.plot(t[intv],u[intv,0,0],'k')
plt.grid(True)
plt.ylabel("u")
plt.subplot2grid((8,4),(3,0),colspan=5)
plt.plot(t[intv],gama[intv,0,0]*180/numpy.pi,'y')
plt.grid(True)
plt.ylabel("gama")
plt.subplots_adjust(0.0125,0.0,0.9,2.5,0.2,0.2)
self.savefig(keyName='currSol',fullName='solution')
self.log.printL("pi ="+str(pi))
elif opt['mode'] == 'var':
dx = opt['x']
du = opt['u']
dp = opt['pi']
dgama = numpy.pi * numpy.tanh(du)
plt.subplot2grid((8,4),(0,0),colspan=5)
plt.plot(t[intv],dx[intv,0,0],)
plt.grid(True)
plt.ylabel("x")
titlStr = "Proposed variations\n"+"Delta pi: "
for i in range(self.p):
titlStr += "{:.4E}, ".format(dp[i])
titlStr += "\n(grad iter #" + str(self.NIterGrad) + ")"
plt.title(titlStr)
plt.subplot2grid((8,4),(1,0),colspan=5)
plt.plot(t[intv],dx[intv,1,0],'g')
plt.grid(True)
plt.ylabel("y")
plt.subplot2grid((8,4),(2,0),colspan=5)
plt.plot(t[intv],du[intv,0,0],'k')
plt.grid(True)
plt.ylabel("u")
plt.subplot2grid((8,4),(3,0),colspan=5)
plt.plot(t[intv],dgama[intv,0,0]*180/numpy.pi,'y')
plt.grid(True)
plt.ylabel("gama")
plt.subplots_adjust(0.0125,0.0,0.9,2.5,0.2,0.2)
self.savefig(keyName='corr',fullName='corrections')
else:
titlStr = opt['mode']
#
def plotTraj(self,mustSaveFig=True,altSol=None,name=None):
"""Plot the trajectory of the sliding mass."""
# TODO: these arguments altSol and "name" are not being used. Use them!
X = self.x[:,0,0]
Y = self.x[:,1,0]
plt.plot(X,Y)
plt.plot(X[0],Y[0],'o')
plt.plot(X[-1],Y[-1],'s')
plt.axis('equal')
plt.grid(True)
plt.xlabel("X [m]")
plt.ylabel("Y [m]")
titlStr = "Trajectory "
titlStr += "(grad iter #" + str(self.NIterGrad) + ")\n"
plt.title(titlStr)
#plt.legend(loc="upper left", bbox_to_anchor=(1,1))
if mustSaveFig:
self.savefig(keyName='traj',fullName='trajectory')
else:
plt.show()
plt.clf()