2 Example of optimal control resolution by direct optimization of a single trajectory.
8 import matplotlib.pyplot
as plt
10 from scipy.optimize
import fmin_l_bfgs_b
12 from pendulum
import Pendulum
16 x0 = env.reset().copy()
20 """Cost for a trajectory starting at state X0 with control U"""
23 for t
in range(NSTEPS):
24 u = U[env.nu * t : env.nu * (t + 1)]
31 """Display the trajectory on Gepetto viewer."""
34 print(
"U = ",
" ".join(map(
lambda u:
"%.1f" % u, np.asarray(U).flatten())))
35 for i
in range(len(U) / env.nu):
36 env.dynamics(x, U[env.nu * i : env.nu * (i + 1)],
True)
44 """Call back function used to follow optimizer steps."""
51 def __call__(self, U):
55 " ".join(map(
lambda u:
"%.1f" % u, np.asarray(U).flatten())),
59 self.
h_rwdh_rwd.append(cost(U))
63 def setWithDisplay(self, boolean=None):
68 signal.signal(signal.SIGTSTP,
lambda x, y: callback.setWithDisplay())
71 U0 = np.zeros(NSTEPS * env.nu) - env.umax
74 [-env.umax, env.umax],
81 U, c, info = fmin_l_bfgs_b(
82 cost, x0=U0, callback=callback, approx_grad=
True, bounds=bounds
88 plt.plot(callback.h_rwd)
def display(U, verbose=False)