10import matplotlib.pyplot
as plt
11from datetime
import timedelta
15sys.path.append(os.path.dirname(__file__))
17from bicycle_model
import generate_problem
23multipleshooting =
False
27 Ts, N_hor, R_obstacle, multipleshooting
33from tempfile
import TemporaryDirectory
36f_prob = cs.Function(
"f", [nlp[
"x"], nlp[
"p"]], [nlp[
"f"]])
37g_prob = cs.Function(
"g", [nlp[
"x"], nlp[
"p"]], [nlp[
"g"]])
38prob = pa.generate_and_compile_casadi_problem(f_prob, g_prob, name=name)
40prob.C.lowerbound = bounds[
"lbx"]
41prob.C.upperbound = bounds[
"ubx"]
42prob.D.lowerbound = bounds[
"lbg"]
43prob.D.upperbound = bounds[
"ubg"]
53 "max_time": timedelta(seconds=0.5),
54 "print_interval": 10
if verbose
else 0,
55 "stop_crit": pa.PANOCStopCrit.ProjGradUnitNorm,
56 "update_lipschitz_in_linesearch":
True,
59innersolver = pa.PANOCSolver(
60 pa.PANOCParams(**panocparams),
61 pa.LBFGSParams(memory=lbfgsmem),
64almparams = pa.ALMParams(
66 max_time=timedelta(seconds=1),
67 print_interval=1
if verbose
else 0,
68 preconditioning=
False,
78solver = pa.ALMSolver(almparams, innersolver)
80state = np.array([-5, 0, 0, 0])
81dest = np.array([5, 0.1, 0, 0])
84 x_sol = np.concatenate((np.tile(state, N_hor), np.zeros((n_inputs * N_hor,))))
86 x_sol = np.zeros((prob.n,))
87assert x_sol.size == prob.n
88y_sol = np.zeros((prob.m,))
92 state = np.reshape(state, (n_states,))
93 prob.param = np.concatenate((state, dest))
94 t0 = time.perf_counter()
95 x_sol, y_sol, stats =
solver(problem=prob, x=x_sol, y=y_sol)
96 t1 = time.perf_counter()
97 return t1 - t0, stats, state, y_sol, x_sol
100xs = np.zeros((N_sim, n_states))
101times = np.zeros((N_sim,))
102for k
in range(N_sim):
103 t, stats, state, y_sol, x_sol =
solve_ocp(state, y_sol, x_sol)
108 stats[
"elapsed_time"],
109 stats[
"outer_iterations"],
110 stats[
"inner"][
"iterations"],
112 input = x_sol[first_input_idx : first_input_idx + n_inputs]
113 state += Ts *
f(state, input)
117fig_trajectory, ax = plt.subplots(1, 1)
118c = plt.Circle((0, 0), R_obstacle)
121ax.plot(xs[:, 0], xs[:, 1],
"r.-")
122ax.set_title(
'Trajectory')
124fig_time, ax = plt.subplots(1, 1)
126ax.set_title(
"Run time")
127ax.set_xlabel(
"MPC time step")
128ax.set_ylabel(
"Run time [s]")
def solve_ocp(state, y_sol, x_sol)
def generate_problem(Ts, N_hor, R_obstacle, multipleshooting)