2 Example of optimal control resolution by direct optimization of a single trajectory.
5 from pendulum
import Pendulum
8 import pinocchio
as pin
9 from numpy.linalg
import norm
12 import matplotlib.pyplot
as plt
16 x0 = env.reset().copy()
19 '''Cost for a trajectory starting at state X0 with control U'''
22 for t
in range(NSTEPS):
23 u = U[env.nu*t:env.nu*(t+1)]
29 '''Display the trajectory on Gepetto viewer.'''
31 if verbose:
print "U = ",
" ".join(map(
lambda u:
'%.1f'%u,np.asarray(U).flatten()))
32 for i
in range(len(U)/env.nu):
33 env.dynamics(x,U[env.nu*i:env.nu*(i+1)],
True)
36 if verbose:
print "X%d"%i,x.T
39 '''Call back function used to follow optimizer steps.'''
45 print 'Iteration ',self.
iter,
" ".join(map(
lambda u:
'%.1f'%u,np.asarray(U).flatten()))
48 self.
h_rwd.append(cost(U))
50 def setWithDisplay(self,boolean = None):
54 signal.signal(signal.SIGTSTP,
lambda x,y:callback.setWithDisplay())
57 U0 = zero(NSTEPS*env.nu)-env.umax
58 bounds = [ [-env.umax,env.umax], ]*env.nu*NSTEPS
61 U,c,info = fmin_l_bfgs_b(cost,x0=U0,callback=callback,
62 approx_grad=
True,bounds=bounds)
67 plt.plot(callback.h_rwd)