9 import matplotlib.pyplot
as plt
10 from datetime
import timedelta
14 sys.path.append(os.path.dirname(__file__))
16 from bicycle_model
import generate_problem
22 multipleshooting =
False
26 Ts, N_hor, R_obstacle, multipleshooting
32 from tempfile
import TemporaryDirectory
35 f_prob = cs.Function(
"f", [nlp[
"x"], nlp[
"p"]], [nlp[
"f"]])
36 g_prob = cs.Function(
"g", [nlp[
"x"], nlp[
"p"]], [nlp[
"g"]])
37 cgen, n, m, num_p = pa.generate_casadi_problem(f_prob, g_prob, name=name)
39 with TemporaryDirectory(prefix=
"")
as tmpdir:
40 cfile = cgen.generate(os.path.join(tmpdir,
''))
41 sofile = os.path.join(tmpdir, f
"{name}.so")
42 os.system(f
"cc -fPIC -shared -O3 -march=native {cfile} -o {sofile}")
44 prob = pa.load_casadi_problem_with_param(sofile, n, m)
46 prob.C.lowerbound = bounds[
"lbx"]
47 prob.C.upperbound = bounds[
"ubx"]
48 prob.D.lowerbound = bounds[
"lbg"]
49 prob.D.upperbound = bounds[
"ubg"]
59 "max_time": timedelta(seconds=0.5),
60 "print_interval": 10
if verbose
else 0,
61 "stop_crit": pa.PANOCStopCrit.ProjGradUnitNorm,
62 "update_lipschitz_in_linesearch":
True,
72 max_time=timedelta(seconds=1),
73 print_interval=1
if verbose
else 0,
74 preconditioning=
False,
86 state = np.array([-5, 0, 0, 0])
87 dest = np.array([5, 0.1, 0, 0])
90 x_sol = np.concatenate((np.tile(state, N_hor), np.zeros((n_inputs * N_hor,))))
92 x_sol = np.zeros((n,))
93 assert x_sol.size == n
94 y_sol = np.zeros((m,))
98 state = np.reshape(state, (n_states,))
99 prob.param = np.concatenate((state, dest))
100 t0 = time.perf_counter()
101 x_sol, y_sol, stats =
solver(problem=prob, x=x_sol, y=y_sol)
102 t1 = time.perf_counter()
103 return t1 - t0, stats, state, y_sol, x_sol
106 xs = np.zeros((N_sim, n_states))
107 times = np.zeros((N_sim,))
108 for k
in range(N_sim):
109 t, stats, state, y_sol, x_sol =
solve_ocp(state, y_sol, x_sol)
114 stats[
"elapsed_time"],
115 stats[
"outer_iterations"],
116 stats[
"inner"][
"iterations"],
118 input = x_sol[first_input_idx : first_input_idx + n_inputs]
119 state += Ts *
f(state, input)
123 fig_trajectory, ax = plt.subplots(1, 1)
124 c = plt.Circle((0, 0), R_obstacle)
127 ax.plot(xs[:, 0], xs[:, 1],
"r.-")
128 ax.set_title(
'Trajectory')
130 fig_time, ax = plt.subplots(1, 1)
132 ax.set_title(
"Run time")
133 ax.set_xlabel(
"MPC time step")
134 ax.set_ylabel(
"Run time [s]")