Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 2 | |
| 3 | # This is an initial, hacky implementation of the extended LQR paper. It's just a proof of concept, |
| 4 | # so don't trust it too much. |
| 5 | |
| 6 | import numpy |
| 7 | import scipy.optimize |
| 8 | from matplotlib import pylab |
| 9 | import sys |
| 10 | |
| 11 | from frc971.control_loops.python import controls |
| 12 | |
| 13 | l = 100 |
| 14 | width = 0.2 |
| 15 | dt = 0.05 |
| 16 | num_states = 3 |
| 17 | num_inputs = 2 |
| 18 | x_hat_initial = numpy.matrix([[0.10], [1.0], [0.0]]) |
| 19 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 20 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 21 | def dynamics(X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 22 | """Calculates the dynamics for a 2 wheeled robot. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 23 | |
| 24 | Args: |
| 25 | X, numpy.matrix(3, 1), The state. [x, y, theta] |
| 26 | U, numpy.matrix(2, 1), The input. [left velocity, right velocity] |
| 27 | |
| 28 | Returns: |
| 29 | numpy.matrix(3, 1), The derivative of the dynamics. |
| 30 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 31 | #return numpy.matrix([[X[1, 0]], |
| 32 | # [X[2, 0]], |
| 33 | # [U[0, 0]]]) |
| 34 | return numpy.matrix([[(U[0, 0] + U[1, 0]) * numpy.cos(X[2, 0]) / 2.0], |
| 35 | [(U[0, 0] + U[1, 0]) * numpy.sin(X[2, 0]) / 2.0], |
| 36 | [(U[1, 0] - U[0, 0]) / width]]) |
| 37 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 38 | |
| 39 | def RungeKutta(f, x, dt): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 40 | """4th order RungeKutta integration of F starting at X.""" |
| 41 | a = f(x) |
| 42 | b = f(x + dt / 2.0 * a) |
| 43 | c = f(x + dt / 2.0 * b) |
| 44 | d = f(x + dt * c) |
| 45 | return x + dt * (a + 2.0 * b + 2.0 * c + d) / 6.0 |
| 46 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 47 | |
| 48 | def discrete_dynamics(X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 49 | return RungeKutta(lambda startingX: dynamics(startingX, U), X, dt) |
| 50 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 51 | |
| 52 | def inverse_discrete_dynamics(X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 53 | return RungeKutta(lambda startingX: -dynamics(startingX, U), X, dt) |
| 54 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 55 | |
| 56 | def numerical_jacobian_x(fn, X, U, epsilon=1e-4): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 57 | """Numerically estimates the jacobian around X, U in X. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 58 | |
| 59 | Args: |
| 60 | fn: A function of X, U. |
| 61 | X: numpy.matrix(num_states, 1), The state vector to take the jacobian |
| 62 | around. |
| 63 | U: numpy.matrix(num_inputs, 1), The input vector to take the jacobian |
| 64 | around. |
| 65 | |
| 66 | Returns: |
| 67 | numpy.matrix(num_states, num_states), The jacobian of fn with X as the |
| 68 | variable. |
| 69 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 70 | num_states = X.shape[0] |
| 71 | nominal = fn(X, U) |
| 72 | answer = numpy.matrix(numpy.zeros((nominal.shape[0], num_states))) |
| 73 | # It's more expensive, but +- epsilon will be more reliable |
| 74 | for i in range(0, num_states): |
| 75 | dX_plus = X.copy() |
| 76 | dX_plus[i] += epsilon |
| 77 | dX_minus = X.copy() |
| 78 | dX_minus[i] -= epsilon |
| 79 | answer[:, i] = (fn(dX_plus, U) - fn(dX_minus, U)) / epsilon / 2.0 |
| 80 | return answer |
| 81 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 82 | |
| 83 | def numerical_jacobian_u(fn, X, U, epsilon=1e-4): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 84 | """Numerically estimates the jacobian around X, U in U. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 85 | |
| 86 | Args: |
| 87 | fn: A function of X, U. |
| 88 | X: numpy.matrix(num_states, 1), The state vector to take the jacobian |
| 89 | around. |
| 90 | U: numpy.matrix(num_inputs, 1), The input vector to take the jacobian |
| 91 | around. |
| 92 | |
| 93 | Returns: |
| 94 | numpy.matrix(num_states, num_inputs), The jacobian of fn with U as the |
| 95 | variable. |
| 96 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 97 | num_states = X.shape[0] |
| 98 | num_inputs = U.shape[0] |
| 99 | nominal = fn(X, U) |
| 100 | answer = numpy.matrix(numpy.zeros((nominal.shape[0], num_inputs))) |
| 101 | for i in range(0, num_inputs): |
| 102 | dU_plus = U.copy() |
| 103 | dU_plus[i] += epsilon |
| 104 | dU_minus = U.copy() |
| 105 | dU_minus[i] -= epsilon |
| 106 | answer[:, i] = (fn(X, dU_plus) - fn(X, dU_minus)) / epsilon / 2.0 |
| 107 | return answer |
| 108 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 109 | |
| 110 | def numerical_jacobian_x_x(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 111 | return numerical_jacobian_x( |
| 112 | lambda X_inner, U_inner: numerical_jacobian_x(fn, X_inner, U_inner).T, |
| 113 | X, U) |
| 114 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 115 | |
| 116 | def numerical_jacobian_x_u(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 117 | return numerical_jacobian_x( |
| 118 | lambda X_inner, U_inner: numerical_jacobian_u(fn, X_inner, U_inner).T, |
| 119 | X, U) |
| 120 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 121 | |
| 122 | def numerical_jacobian_u_x(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 123 | return numerical_jacobian_u( |
| 124 | lambda X_inner, U_inner: numerical_jacobian_x(fn, X_inner, U_inner).T, |
| 125 | X, U) |
| 126 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 127 | |
| 128 | def numerical_jacobian_u_u(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 129 | return numerical_jacobian_u( |
| 130 | lambda X_inner, U_inner: numerical_jacobian_u(fn, X_inner, U_inner).T, |
| 131 | X, U) |
| 132 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 133 | |
| 134 | # Simple implementation for a quadratic cost function. |
| 135 | class CostFunction: |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 136 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 137 | def __init__(self): |
| 138 | self.num_states = num_states |
| 139 | self.num_inputs = num_inputs |
| 140 | self.dt = dt |
| 141 | self.Q = numpy.matrix([[0.1, 0, 0], [0, 0.6, 0], [0, 0, 0.1] |
| 142 | ]) / dt / dt |
| 143 | self.R = numpy.matrix([[0.40, 0], [0, 0.40]]) / dt / dt |
| 144 | |
| 145 | def estimate_Q_final(self, X_hat): |
| 146 | """Returns the quadraticized final Q around X_hat. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 147 | |
| 148 | This is calculated by evaluating partial^2 cost(X_hat) / (partial X * partial X) |
| 149 | |
| 150 | Args: |
| 151 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 152 | |
| 153 | Result: |
| 154 | numpy.matrix(self.num_states, self.num_states) |
| 155 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 156 | zero_U = numpy.matrix(numpy.zeros((self.num_inputs, 1))) |
| 157 | return numerical_jacobian_x_x(self.final_cost, X_hat, zero_U) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 158 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 159 | def estimate_partial_cost_partial_x_final(self, X_hat): |
| 160 | """Returns \frac{\partial cost}{\partial X}(X_hat) for the final cost. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 161 | |
| 162 | Args: |
| 163 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 164 | |
| 165 | Result: |
| 166 | numpy.matrix(self.num_states, 1) |
| 167 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 168 | return numerical_jacobian_x( |
| 169 | self.final_cost, X_hat, |
| 170 | numpy.matrix(numpy.zeros((self.num_inputs, 1)))).T |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 171 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 172 | def estimate_q_final(self, X_hat): |
| 173 | """Returns q evaluated at X_hat for the final cost function.""" |
| 174 | return self.estimate_partial_cost_partial_x_final( |
| 175 | X_hat) - self.estimate_Q_final(X_hat) * X_hat |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 176 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 177 | def final_cost(self, X, U): |
| 178 | """Computes the final cost of being at X |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 179 | |
| 180 | Args: |
| 181 | X: numpy.matrix(self.num_states, 1) |
| 182 | U: numpy.matrix(self.num_inputs, 1), ignored |
| 183 | |
| 184 | Returns: |
| 185 | numpy.matrix(1, 1), The quadratic cost of being at X |
| 186 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 187 | return X.T * self.Q * X * 1000 |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 188 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 189 | def cost(self, X, U): |
| 190 | """Computes the incremental cost given a position and U. |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 191 | |
| 192 | Args: |
| 193 | X: numpy.matrix(self.num_states, 1) |
| 194 | U: numpy.matrix(self.num_inputs, 1) |
| 195 | |
| 196 | Returns: |
| 197 | numpy.matrix(1, 1), The quadratic cost of evaluating U. |
| 198 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 199 | return U.T * self.R * U + X.T * self.Q * X |
| 200 | |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 201 | |
| 202 | cost_fn_obj = CostFunction() |
| 203 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 204 | S_bar_t = [ |
| 205 | numpy.matrix(numpy.zeros((num_states, num_states))) for _ in range(l + 1) |
| 206 | ] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 207 | s_bar_t = [numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1)] |
| 208 | s_scalar_bar_t = [numpy.matrix(numpy.zeros((1, 1))) for _ in range(l + 1)] |
| 209 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 210 | L_t = [ |
| 211 | numpy.matrix(numpy.zeros((num_inputs, num_states))) for _ in range(l + 1) |
| 212 | ] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 213 | l_t = [numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1)] |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 214 | L_bar_t = [ |
| 215 | numpy.matrix(numpy.zeros((num_inputs, num_states))) for _ in range(l + 1) |
| 216 | ] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 217 | l_bar_t = [numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1)] |
| 218 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 219 | S_t = [ |
| 220 | numpy.matrix(numpy.zeros((num_states, num_states))) for _ in range(l + 1) |
| 221 | ] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 222 | s_t = [numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1)] |
| 223 | s_scalar_t = [numpy.matrix(numpy.zeros((1, 1))) for _ in range(l + 1)] |
| 224 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 225 | last_x_hat_t = [ |
| 226 | numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1) |
| 227 | ] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 228 | |
| 229 | for a in range(15): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 230 | x_hat = x_hat_initial |
| 231 | u_t = L_t[0] * x_hat + l_t[0] |
| 232 | S_bar_t[0] = numpy.matrix(numpy.zeros((num_states, num_states))) |
| 233 | s_bar_t[0] = numpy.matrix(numpy.zeros((num_states, 1))) |
| 234 | s_scalar_bar_t[0] = numpy.matrix([[0]]) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 235 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 236 | last_x_hat_t[0] = x_hat_initial |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 237 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 238 | Q_t = numerical_jacobian_x_x(cost_fn_obj.cost, x_hat_initial, u_t) |
| 239 | P_t = numerical_jacobian_x_u(cost_fn_obj.cost, x_hat_initial, u_t) |
| 240 | R_t = numerical_jacobian_u_u(cost_fn_obj.cost, x_hat_initial, u_t) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 241 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 242 | q_t = numerical_jacobian_x(cost_fn_obj.cost, x_hat_initial, |
| 243 | u_t).T - Q_t * x_hat_initial - P_t.T * u_t |
| 244 | r_t = numerical_jacobian_u(cost_fn_obj.cost, x_hat_initial, |
| 245 | u_t).T - P_t * x_hat_initial - R_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 246 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 247 | q_scalar_t = cost_fn_obj.cost( |
| 248 | x_hat_initial, |
| 249 | u_t) - 0.5 * (x_hat_initial.T * |
| 250 | (Q_t * x_hat_initial + P_t.T * u_t) + u_t.T * |
| 251 | (P_t * x_hat_initial + R_t * u_t) |
| 252 | ) - x_hat_initial.T * q_t - u_t.T * r_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 253 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 254 | start_A_t = numerical_jacobian_x(discrete_dynamics, x_hat_initial, u_t) |
| 255 | start_B_t = numerical_jacobian_u(discrete_dynamics, x_hat_initial, u_t) |
| 256 | x_hat_next = discrete_dynamics(x_hat_initial, u_t) |
| 257 | start_c_t = x_hat_next - start_A_t * x_hat_initial - start_B_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 258 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 259 | B_svd_u, B_svd_sigma_diag, B_svd_v = numpy.linalg.svd(start_B_t) |
| 260 | B_svd_sigma = numpy.matrix(numpy.zeros(start_B_t.shape)) |
| 261 | B_svd_sigma[0:B_svd_sigma_diag.shape[0], |
| 262 | 0:B_svd_sigma_diag.shape[0]] = numpy.diag(B_svd_sigma_diag) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 263 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 264 | B_svd_sigma_inv = numpy.matrix(numpy.zeros(start_B_t.shape)).T |
| 265 | B_svd_sigma_inv[0:B_svd_sigma_diag.shape[0], |
| 266 | 0:B_svd_sigma_diag.shape[0]] = numpy.linalg.inv( |
| 267 | numpy.diag(B_svd_sigma_diag)) |
| 268 | B_svd_inv = B_svd_v.T * B_svd_sigma_inv * B_svd_u.T |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 269 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 270 | L_bar_t[1] = B_svd_inv |
| 271 | l_bar_t[1] = -B_svd_inv * (start_A_t * x_hat_initial + start_c_t) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 272 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 273 | S_bar_t[1] = L_bar_t[1].T * R_t * L_bar_t[1] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 274 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 275 | TotalS_1 = start_B_t.T * S_t[1] * start_B_t + R_t |
| 276 | Totals_1 = start_B_t.T * S_t[1] * ( |
| 277 | start_c_t + start_A_t * |
| 278 | x_hat_initial) + start_B_t.T * s_t[1] + P_t * x_hat_initial + r_t |
| 279 | Totals_scalar_1 = 0.5 * ( |
| 280 | start_c_t.T + x_hat_initial.T * start_A_t.T |
| 281 | ) * S_t[1] * (start_c_t + start_A_t * x_hat_initial) + s_scalar_t[ |
| 282 | 1] + x_hat_initial.T * q_t + q_scalar_t + 0.5 * x_hat_initial.T * Q_t * x_hat_initial + ( |
| 283 | start_c_t.T + x_hat_initial.T * start_A_t.T) * s_t[1] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 284 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 285 | optimal_u_1 = -numpy.linalg.solve(TotalS_1, Totals_1) |
| 286 | optimal_x_1 = start_A_t * x_hat_initial + start_B_t * optimal_u_1 + start_c_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 287 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 288 | S_bar_1_eigh_eigenvalues, S_bar_1_eigh_eigenvectors = numpy.linalg.eigh( |
| 289 | S_bar_t[1]) |
| 290 | S_bar_1_eigh = numpy.matrix(numpy.diag(S_bar_1_eigh_eigenvalues)) |
| 291 | S_bar_1_eigh_eigenvalues_stiff = S_bar_1_eigh_eigenvalues.copy() |
| 292 | for i in range(S_bar_1_eigh_eigenvalues_stiff.shape[0]): |
| 293 | if abs(S_bar_1_eigh_eigenvalues_stiff[i]) < 1e-8: |
| 294 | S_bar_1_eigh_eigenvalues_stiff[i] = max( |
| 295 | S_bar_1_eigh_eigenvalues_stiff) * 1.0 |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 296 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 297 | #print('eigh eigenvalues of S bar', S_bar_1_eigh_eigenvalues) |
| 298 | #print('eigh eigenvectors of S bar', S_bar_1_eigh_eigenvectors.T) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 299 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 300 | #print('S bar eig recreate', S_bar_1_eigh_eigenvectors * numpy.matrix(numpy.diag(S_bar_1_eigh_eigenvalues)) * S_bar_1_eigh_eigenvectors.T) |
| 301 | #print('S bar eig recreate error', (S_bar_1_eigh_eigenvectors * numpy.matrix(numpy.diag(S_bar_1_eigh_eigenvalues)) * S_bar_1_eigh_eigenvectors.T - S_bar_t[1])) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 302 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 303 | S_bar_stiff = S_bar_1_eigh_eigenvectors * numpy.matrix( |
| 304 | numpy.diag( |
| 305 | S_bar_1_eigh_eigenvalues_stiff)) * S_bar_1_eigh_eigenvectors.T |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 306 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 307 | print('Min u', -numpy.linalg.solve(TotalS_1, Totals_1)) |
| 308 | print('Min x_hat', optimal_x_1) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 309 | s_bar_t[1] = -s_t[1] - (S_bar_stiff + S_t[1]) * optimal_x_1 |
| 310 | s_scalar_bar_t[1] = 0.5 * ( |
| 311 | optimal_u_1.T * TotalS_1 * optimal_u_1 - optimal_x_1.T * |
| 312 | (S_bar_stiff + S_t[1]) * |
| 313 | optimal_x_1) + optimal_u_1.T * Totals_1 - optimal_x_1.T * ( |
| 314 | s_bar_t[1] + s_t[1]) - s_scalar_t[1] + Totals_scalar_1 |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 315 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 316 | print('optimal_u_1', optimal_u_1) |
| 317 | print('TotalS_1', TotalS_1) |
| 318 | print('Totals_1', Totals_1) |
| 319 | print('Totals_scalar_1', Totals_scalar_1) |
| 320 | print( |
| 321 | 'overall cost 1', 0.5 * (optimal_u_1.T * TotalS_1 * optimal_u_1) + |
| 322 | optimal_u_1.T * Totals_1 + Totals_scalar_1) |
| 323 | print( |
| 324 | 'overall cost 0', 0.5 * (x_hat_initial.T * S_t[0] * x_hat_initial) + |
| 325 | x_hat_initial.T * s_t[0] + s_scalar_t[0]) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 326 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 327 | print('t forward 0') |
| 328 | print('x_hat_initial[ 0]: %s' % (x_hat_initial)) |
| 329 | print('x_hat[%2d]: %s' % (0, x_hat.T)) |
| 330 | print('x_hat_next[%2d]: %s' % (0, x_hat_next.T)) |
| 331 | print('u[%2d]: %s' % (0, u_t.T)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 332 | print('L[ 0]: %s' % (L_t[0], )).replace('\n', '\n ') |
| 333 | print('l[ 0]: %s' % (l_t[0], )).replace('\n', '\n ') |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 334 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 335 | print('A_t[%2d]: %s' % (0, start_A_t)).replace('\n', '\n ') |
| 336 | print('B_t[%2d]: %s' % (0, start_B_t)).replace('\n', '\n ') |
| 337 | print('c_t[%2d]: %s' % (0, start_c_t)).replace('\n', '\n ') |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 338 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 339 | # TODO(austin): optimal_x_1 is x_hat |
| 340 | x_hat = -numpy.linalg.solve((S_t[1] + S_bar_stiff), (s_t[1] + s_bar_t[1])) |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 341 | print('new xhat', x_hat) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 342 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 343 | S_bar_t[1] = S_bar_stiff |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 344 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 345 | last_x_hat_t[1] = x_hat |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 346 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 347 | for t in range(1, l): |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 348 | print('t forward', t) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 349 | u_t = L_t[t] * x_hat + l_t[t] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 350 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 351 | x_hat_next = discrete_dynamics(x_hat, u_t) |
| 352 | A_bar_t = numerical_jacobian_x(inverse_discrete_dynamics, x_hat_next, |
| 353 | u_t) |
| 354 | B_bar_t = numerical_jacobian_u(inverse_discrete_dynamics, x_hat_next, |
| 355 | u_t) |
| 356 | c_bar_t = x_hat - A_bar_t * x_hat_next - B_bar_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 357 | |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 358 | print('x_hat[%2d]: %s' % (t, x_hat.T)) |
| 359 | print('x_hat_next[%2d]: %s' % (t, x_hat_next.T)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 360 | print('L[%2d]: %s' % ( |
| 361 | t, |
| 362 | L_t[t], |
| 363 | )).replace('\n', '\n ') |
| 364 | print('l[%2d]: %s' % ( |
| 365 | t, |
| 366 | l_t[t], |
| 367 | )).replace('\n', '\n ') |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 368 | print('u[%2d]: %s' % (t, u_t.T)) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 369 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 370 | print('A_bar_t[%2d]: %s' % (t, A_bar_t)).replace( |
| 371 | '\n', '\n ') |
| 372 | print('B_bar_t[%2d]: %s' % (t, B_bar_t)).replace( |
| 373 | '\n', '\n ') |
| 374 | print('c_bar_t[%2d]: %s' % (t, c_bar_t)).replace( |
| 375 | '\n', '\n ') |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 376 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 377 | Q_t = numerical_jacobian_x_x(cost_fn_obj.cost, x_hat, u_t) |
| 378 | P_t = numerical_jacobian_x_u(cost_fn_obj.cost, x_hat, u_t) |
| 379 | R_t = numerical_jacobian_u_u(cost_fn_obj.cost, x_hat, u_t) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 380 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 381 | q_t = numerical_jacobian_x(cost_fn_obj.cost, x_hat, |
| 382 | u_t).T - Q_t * x_hat - P_t.T * u_t |
| 383 | r_t = numerical_jacobian_u(cost_fn_obj.cost, x_hat, |
| 384 | u_t).T - P_t * x_hat - R_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 385 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 386 | q_scalar_t = cost_fn_obj.cost(x_hat, u_t) - 0.5 * ( |
| 387 | x_hat.T * (Q_t * x_hat + P_t.T * u_t) + u_t.T * |
| 388 | (P_t * x_hat + R_t * u_t)) - x_hat.T * q_t - u_t.T * r_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 389 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 390 | C_bar_t = B_bar_t.T * (S_bar_t[t] + Q_t) * A_bar_t + P_t * A_bar_t |
| 391 | D_bar_t = A_bar_t.T * (S_bar_t[t] + Q_t) * A_bar_t |
| 392 | E_bar_t = B_bar_t.T * ( |
| 393 | S_bar_t[t] + |
| 394 | Q_t) * B_bar_t + R_t + P_t * B_bar_t + B_bar_t.T * P_t.T |
| 395 | d_bar_t = A_bar_t.T * (s_bar_t[t] + q_t) + A_bar_t.T * (S_bar_t[t] + |
| 396 | Q_t) * c_bar_t |
| 397 | e_bar_t = r_t + P_t * c_bar_t + B_bar_t.T * s_bar_t[t] + B_bar_t.T * ( |
| 398 | S_bar_t[t] + Q_t) * c_bar_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 399 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 400 | L_bar_t[t + 1] = -numpy.linalg.inv(E_bar_t) * C_bar_t |
| 401 | l_bar_t[t + 1] = -numpy.linalg.inv(E_bar_t) * e_bar_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 402 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 403 | S_bar_t[t + 1] = D_bar_t + C_bar_t.T * L_bar_t[t + 1] |
| 404 | s_bar_t[t + 1] = d_bar_t + C_bar_t.T * l_bar_t[t + 1] |
| 405 | s_scalar_bar_t[t + 1] = -0.5 * e_bar_t.T * numpy.linalg.inv( |
| 406 | E_bar_t) * e_bar_t + 0.5 * c_bar_t.T * ( |
| 407 | S_bar_t[t] + Q_t) * c_bar_t + c_bar_t.T * s_bar_t[ |
| 408 | t] + c_bar_t.T * q_t + s_scalar_bar_t[t] + q_scalar_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 409 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 410 | x_hat = -numpy.linalg.solve((S_t[t + 1] + S_bar_t[t + 1]), |
| 411 | (s_t[t + 1] + s_bar_t[t + 1])) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 412 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 413 | S_t[l] = cost_fn_obj.estimate_Q_final(x_hat) |
| 414 | s_t[l] = cost_fn_obj.estimate_q_final(x_hat) |
| 415 | x_hat = -numpy.linalg.inv(S_t[l] + S_bar_t[l]) * (s_t[l] + s_bar_t[l]) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 416 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 417 | for t in reversed(range(l)): |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 418 | print('t backward', t) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 419 | # TODO(austin): I don't think we can use L_t like this here. |
| 420 | # I think we are off by 1 somewhere... |
| 421 | u_t = L_bar_t[t + 1] * x_hat + l_bar_t[t + 1] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 422 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 423 | x_hat_prev = inverse_discrete_dynamics(x_hat, u_t) |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 424 | print('x_hat[%2d]: %s' % (t, x_hat.T)) |
| 425 | print('x_hat_prev[%2d]: %s' % (t, x_hat_prev.T)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 426 | print('L_bar[%2d]: %s' % (t + 1, L_bar_t[t + 1])).replace( |
| 427 | '\n', '\n ') |
| 428 | print('l_bar[%2d]: %s' % (t + 1, l_bar_t[t + 1])).replace( |
| 429 | '\n', '\n ') |
Austin Schuh | ea5f0a7 | 2024-09-02 15:02:36 -0700 | [diff] [blame^] | 430 | print('u[%2d]: %s' % (t, u_t.T)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 431 | # Now compute the linearized A, B, and C |
| 432 | # Start by doing it numerically, and then optimize. |
| 433 | A_t = numerical_jacobian_x(discrete_dynamics, x_hat_prev, u_t) |
| 434 | B_t = numerical_jacobian_u(discrete_dynamics, x_hat_prev, u_t) |
| 435 | c_t = x_hat - A_t * x_hat_prev - B_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 436 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 437 | print('A_t[%2d]: %s' % (t, A_t)).replace('\n', '\n ') |
| 438 | print('B_t[%2d]: %s' % (t, B_t)).replace('\n', '\n ') |
| 439 | print('c_t[%2d]: %s' % (t, c_t)).replace('\n', '\n ') |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 440 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 441 | Q_t = numerical_jacobian_x_x(cost_fn_obj.cost, x_hat_prev, u_t) |
| 442 | P_t = numerical_jacobian_x_u(cost_fn_obj.cost, x_hat_prev, u_t) |
| 443 | P_T_t = numerical_jacobian_u_x(cost_fn_obj.cost, x_hat_prev, u_t) |
| 444 | R_t = numerical_jacobian_u_u(cost_fn_obj.cost, x_hat_prev, u_t) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 445 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 446 | q_t = numerical_jacobian_x(cost_fn_obj.cost, x_hat_prev, |
| 447 | u_t).T - Q_t * x_hat_prev - P_T_t * u_t |
| 448 | r_t = numerical_jacobian_u(cost_fn_obj.cost, x_hat_prev, |
| 449 | u_t).T - P_t * x_hat_prev - R_t * u_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 450 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 451 | q_scalar_t = cost_fn_obj.cost(x_hat_prev, u_t) - 0.5 * ( |
| 452 | x_hat_prev.T * (Q_t * x_hat_prev + P_t.T * u_t) + u_t.T * |
| 453 | (P_t * x_hat_prev + R_t * u_t)) - x_hat_prev.T * q_t - u_t.T * r_t |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 454 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 455 | C_t = P_t + B_t.T * S_t[t + 1] * A_t |
| 456 | D_t = Q_t + A_t.T * S_t[t + 1] * A_t |
| 457 | E_t = R_t + B_t.T * S_t[t + 1] * B_t |
| 458 | d_t = q_t + A_t.T * s_t[t + 1] + A_t.T * S_t[t + 1] * c_t |
| 459 | e_t = r_t + B_t.T * s_t[t + 1] + B_t.T * S_t[t + 1] * c_t |
| 460 | L_t[t] = -numpy.linalg.inv(E_t) * C_t |
| 461 | l_t[t] = -numpy.linalg.inv(E_t) * e_t |
| 462 | s_t[t] = d_t + C_t.T * l_t[t] |
| 463 | S_t[t] = D_t + C_t.T * L_t[t] |
| 464 | s_scalar_t[t] = q_scalar_t - 0.5 * e_t.T * numpy.linalg.inv( |
| 465 | E_t) * e_t + 0.5 * c_t.T * S_t[t + 1] * c_t + c_t.T * s_t[ |
| 466 | t + 1] + s_scalar_t[t + 1] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 467 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 468 | x_hat = -numpy.linalg.solve((S_t[t] + S_bar_t[t]), |
| 469 | (s_t[t] + s_bar_t[t])) |
| 470 | if t == 0: |
| 471 | last_x_hat_t[t] = x_hat_initial |
| 472 | else: |
| 473 | last_x_hat_t[t] = x_hat |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 474 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 475 | x_hat_t = [x_hat_initial] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 476 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 477 | pylab.figure('states %d' % a) |
| 478 | pylab.ion() |
| 479 | for dim in range(num_states): |
| 480 | pylab.plot(numpy.arange(len(last_x_hat_t)), |
| 481 | [x_hat_loop[dim, 0] for x_hat_loop in last_x_hat_t], |
| 482 | marker='o', |
| 483 | label='Xhat[%d]' % dim) |
| 484 | pylab.legend() |
| 485 | pylab.draw() |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 486 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 487 | pylab.figure('xy %d' % a) |
| 488 | pylab.ion() |
| 489 | pylab.plot([x_hat_loop[0, 0] for x_hat_loop in last_x_hat_t], |
| 490 | [x_hat_loop[1, 0] for x_hat_loop in last_x_hat_t], |
| 491 | marker='o', |
| 492 | label='trajectory') |
| 493 | pylab.legend() |
| 494 | pylab.draw() |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 495 | |
| 496 | final_u_t = [numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1)] |
| 497 | cost_to_go = [] |
| 498 | cost_to_come = [] |
| 499 | cost = [] |
| 500 | for t in range(l): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 501 | cost_to_go.append((0.5 * last_x_hat_t[t].T * S_t[t] * last_x_hat_t[t] + |
| 502 | last_x_hat_t[t].T * s_t[t] + s_scalar_t[t])[0, 0]) |
| 503 | cost_to_come.append( |
| 504 | (0.5 * last_x_hat_t[t].T * S_bar_t[t] * last_x_hat_t[t] + |
| 505 | last_x_hat_t[t].T * s_bar_t[t] + s_scalar_bar_t[t])[0, 0]) |
| 506 | cost.append(cost_to_go[-1] + cost_to_come[-1]) |
| 507 | final_u_t[t] = L_t[t] * last_x_hat_t[t] + l_t[t] |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 508 | |
| 509 | for t in range(l): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 510 | A_t = numerical_jacobian_x(discrete_dynamics, last_x_hat_t[t], |
| 511 | final_u_t[t]) |
| 512 | B_t = numerical_jacobian_u(discrete_dynamics, last_x_hat_t[t], |
| 513 | final_u_t[t]) |
| 514 | c_t = discrete_dynamics( |
| 515 | last_x_hat_t[t], |
| 516 | final_u_t[t]) - A_t * last_x_hat_t[t] - B_t * final_u_t[t] |
| 517 | print("Infeasability at", t, "is", |
| 518 | ((A_t * last_x_hat_t[t] + B_t * final_u_t[t] + c_t) - |
| 519 | last_x_hat_t[t + 1]).T) |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 520 | |
| 521 | pylab.figure('u') |
| 522 | samples = numpy.arange(len(final_u_t)) |
| 523 | for i in range(num_inputs): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 524 | pylab.plot(samples, [u[i, 0] for u in final_u_t], |
| 525 | label='u[%d]' % i, |
| 526 | marker='o') |
| 527 | pylab.legend() |
Austin Schuh | 0038f8e | 2016-07-20 19:57:01 -0700 | [diff] [blame] | 528 | |
| 529 | pylab.figure('cost') |
| 530 | cost_samples = numpy.arange(len(cost)) |
| 531 | pylab.plot(cost_samples, cost_to_go, label='cost to go', marker='o') |
| 532 | pylab.plot(cost_samples, cost_to_come, label='cost to come', marker='o') |
| 533 | pylab.plot(cost_samples, cost, label='cost', marker='o') |
| 534 | pylab.legend() |
| 535 | |
| 536 | pylab.ioff() |
| 537 | pylab.show() |
| 538 | |
| 539 | sys.exit(1) |