5def _generate_problem_ss(
25 U = cs.SX.sym(
"U", n_inputs, N_hor)
28 for k
in range(N_hor):
30 err = xk - desired_state
31 objective += err.T @ Q @ err + uk.T @ R @ uk
32 next_euler = xk + Ts *
f(xk, uk)
34 state_constr = cs.vertcat(state_constr, xk)
35 collision_constr = cs.vertcat(
36 collision_constr, xk[0:2].T @ xk[0:2]
41 err = xk - desired_state
42 objective += 10 * err.T @ Q @ err
43 state_constr = cs.vertcat(state_constr, xk)
44 collision_constr = cs.vertcat(collision_constr, xk[0:2].T @ xk[0:2])
46 states_constr_lb = np.tile(states_lb, N_hor)
47 states_constr_ub = np.tile(states_ub, N_hor)
49 collision_constr_lb = R_obstacle ** 2 * np.ones((N_hor,))
50 collision_constr_ub = +np.inf * np.ones((N_hor,))
54 "g": cs.vertcat(state_constr, collision_constr),
55 "x": U.reshape((N_hor * n_inputs, 1)),
56 "p": cs.vertcat(initial_state, desired_state),
59 "lbx": np.tile(inputs_lb, N_hor),
60 "ubx": np.tile(inputs_ub, N_hor),
61 "lbg": np.concatenate((states_constr_lb, collision_constr_lb)),
62 "ubg": np.concatenate((states_constr_ub, collision_constr_ub)),
66 return f, nlp, bounds, n_states, n_inputs, first_input_idx
69def _generate_problem_ms(
85 X, U = cs.SX.sym(
"X", n_states, N_hor), cs.SX.sym(
"U", n_inputs, N_hor)
86 X0X = cs.horzcat(initial_state, X)
92 for k
in range(N_hor):
93 xk, uk = X0X[:, k], U[:, k]
95 err = xk - desired_state
96 objective += err.T @ Q @ err + uk.T @ R @ uk
97 next_euler = xk + Ts *
f(xk, uk)
98 dynamics_constr = cs.vertcat(dynamics_constr, xkp1 - next_euler)
100 collision_constr = cs.vertcat(
101 collision_constr, xk[0:2].T @ xk[0:2]
105 err = xN - desired_state
106 objective += 10 * err.T @ Q @ err
107 collision_constr = cs.vertcat(collision_constr, xN[0:2].T @ xN[0:2])
109 dynamics_constr_lb = np.zeros((N_hor * n_states,))
110 dynamics_constr_ub = np.zeros((N_hor * n_states,))
112 collision_constr_lb = R_obstacle ** 2 * np.ones((N_hor,))
113 collision_constr_ub = +np.inf * np.ones((N_hor,))
115 assert dynamics_constr_lb.size == dynamics_constr.size1()
116 assert dynamics_constr_ub.size == dynamics_constr.size1()
117 assert collision_constr_lb.size == collision_constr.size1()
118 assert collision_constr_ub.size == collision_constr.size1()
122 "g": cs.vertcat(dynamics_constr, collision_constr),
124 X.reshape((N_hor * n_states, 1)), U.reshape((N_hor * n_inputs, 1))
126 "p": cs.vertcat(initial_state, desired_state),
129 "lbx": np.concatenate((np.tile(states_lb, N_hor), np.tile(inputs_lb, N_hor))),
130 "ubx": np.concatenate((np.tile(states_ub, N_hor), np.tile(inputs_ub, N_hor))),
131 "lbg": np.concatenate((dynamics_constr_lb, collision_constr_lb)),
132 "ubg": np.concatenate((dynamics_constr_ub, collision_constr_ub)),
134 first_input_idx = N_hor * n_states
136 return f, nlp, bounds, n_states, n_inputs, first_input_idx
153 initial_state = cs.SX.sym(
"x0", n_states)
154 desired_state = cs.SX.sym(
"xd", n_states)
157 Q = cs.diagcat(100, 100, 10, 10)
159 R = cs.diagcat(1e-1, 1e-5)
167 states_lb = np.array([-10, -10, -np.pi / 3, -3])
168 states_ub = np.array([+10, +10, +np.pi / 3, +3])
170 inputs_lb = np.array([-5, -np.pi / 4])
171 inputs_ub = np.array([+5, +np.pi / 4])
173 x, u = cs.SX.sym(
"x", n_states), cs.SX.sym(
"u", n_inputs)
174 p_x, p_y, ψ, v = cs.vertsplit(x)
175 a, δ_f = cs.vertsplit(u)
176 β = cs.atan(l_r / (l_f + l_r) * cs.tan(δ_f))
177 continuous_dynamics = cs.vertcat(
183 f = cs.Function(
"f", [x, u], [continuous_dynamics])
185 gen = _generate_problem_ms
if multipleshooting
else _generate_problem_ss
def generate_problem(Ts, N_hor, R_obstacle, multipleshooting)