2 Example of optimal control resolution by direct optimization of a single trajectory.
8 import matplotlib.pyplot
as plt
10 from pendulum
import Pendulum
11 from scipy.optimize
import fmin_l_bfgs_b
15 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)]
30 """Display the trajectory on Gepetto viewer."""
33 print(
"U = ",
" ".join(map(
lambda u: f
"{u:.1f}", np.asarray(U).flatten())))
34 for i
in range(len(U) / env.nu):
35 env.dynamics(x, U[env.nu * i : env.nu * (i + 1)],
True)
43 """Call back function used to follow optimizer steps."""
50 def __call__(self, U):
54 " ".join(map(
lambda u: f
"{u:.1f}", np.asarray(U).flatten())),
58 self.
h_rwdh_rwd.append(cost(U))
62 def setWithDisplay(self, boolean=None):
67 signal.signal(signal.SIGTSTP,
lambda x, y: callback.setWithDisplay())
70 U0 = np.zeros(NSTEPS * env.nu) - env.umax
73 [-env.umax, env.umax],
80 U, c, info = fmin_l_bfgs_b(
81 cost, x0=U0, callback=callback, approx_grad=
True, bounds=bounds
87 plt.plot(callback.h_rwd)
def display(U, verbose=False)