Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 2 | # This is an initial, hacky implementation of the extended LQR paper. It's just |
| 3 | # a proof of concept, so don't trust it too much. |
| 4 | |
| 5 | import numpy |
| 6 | import scipy.optimize |
| 7 | from matplotlib import pylab |
| 8 | import sys |
| 9 | |
| 10 | from frc971.control_loops.python import controls |
| 11 | |
| 12 | |
| 13 | class ArmDynamics(object): |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 14 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 15 | def __init__(self, dt): |
| 16 | self.dt = dt |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 17 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 18 | self.l1 = 1.0 |
| 19 | self.l2 = 0.8 |
| 20 | self.num_states = 4 |
| 21 | self.num_inputs = 2 |
| 22 | |
| 23 | def dynamics(self, X, U): |
| 24 | """Calculates the dynamics for a double jointed arm. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 25 | |
| 26 | Args: |
| 27 | X, numpy.matrix(4, 1), The state. [theta1, omega1, theta2, omega2] |
| 28 | U, numpy.matrix(2, 1), The input. [torque1, torque2] |
| 29 | |
| 30 | Returns: |
| 31 | numpy.matrix(4, 1), The derivative of the dynamics. |
| 32 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 33 | return numpy.matrix([[X[1, 0]], [U[0, 0]], [X[3, 0]], [U[1, 0]]]) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 34 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 35 | def discrete_dynamics(self, X, U): |
| 36 | return RungeKutta(lambda startingX: self.dynamics(startingX, U), X, dt) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 37 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 38 | def inverse_discrete_dynamics(self, X, U): |
| 39 | return RungeKutta(lambda startingX: -self.dynamics(startingX, U), X, |
| 40 | dt) |
| 41 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 42 | |
| 43 | # Simple implementation for a quadratic cost function. |
| 44 | class ArmCostFunction: |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 45 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 46 | def __init__(self, dt, dynamics): |
| 47 | self.num_states = 4 |
| 48 | self.num_inputs = 2 |
| 49 | self.dt = dt |
| 50 | self.dynamics = dynamics |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 51 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 52 | q_pos = 0.5 |
| 53 | q_vel = 1.65 |
| 54 | self.Q = numpy.matrix( |
| 55 | numpy.diag([ |
| 56 | 1.0 / (q_pos**2.0), 1.0 / (q_vel**2.0), 1.0 / (q_pos**2.0), |
| 57 | 1.0 / (q_vel**2.0) |
| 58 | ])) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 59 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 60 | self.R = numpy.matrix( |
| 61 | numpy.diag([1.0 / (12.0**2.0), 1.0 / (12.0**2.0)])) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 62 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 63 | final_A = numerical_jacobian_x(self.dynamics.discrete_dynamics, |
| 64 | numpy.matrix(numpy.zeros((4, 1))), |
| 65 | numpy.matrix(numpy.zeros((2, 1)))) |
| 66 | final_B = numerical_jacobian_u(self.dynamics.discrete_dynamics, |
| 67 | numpy.matrix(numpy.zeros((4, 1))), |
| 68 | numpy.matrix(numpy.zeros((2, 1)))) |
| 69 | print 'Final A', final_A |
| 70 | print 'Final B', final_B |
| 71 | K, self.S = controls.dlqr(final_A, |
| 72 | final_B, |
| 73 | self.Q, |
| 74 | self.R, |
| 75 | optimal_cost_function=True) |
| 76 | print 'Final eig:', numpy.linalg.eig(final_A - final_B * K) |
| 77 | |
| 78 | def final_cost(self, X, U): |
| 79 | """Computes the final cost of being at X |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 80 | |
| 81 | Args: |
| 82 | X: numpy.matrix(self.num_states, 1) |
| 83 | U: numpy.matrix(self.num_inputs, 1), ignored |
| 84 | |
| 85 | Returns: |
| 86 | numpy.matrix(1, 1), The quadratic cost of being at X |
| 87 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 88 | return 0.5 * X.T * self.S * X |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 89 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 90 | def cost(self, X, U): |
| 91 | """Computes the incremental cost given a position and U. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 92 | |
| 93 | Args: |
| 94 | X: numpy.matrix(self.num_states, 1) |
| 95 | U: numpy.matrix(self.num_inputs, 1) |
| 96 | |
| 97 | Returns: |
| 98 | numpy.matrix(1, 1), The quadratic cost of evaluating U. |
| 99 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 100 | return U.T * self.R * U + X.T * self.Q * X |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 101 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 102 | def estimate_Q_final(self, X_hat): |
| 103 | """Returns the quadraticized final Q around X_hat. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 104 | |
| 105 | This is calculated by evaluating partial^2 cost(X_hat) / (partial X * partial X) |
| 106 | |
| 107 | Args: |
| 108 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 109 | |
| 110 | Result: |
| 111 | numpy.matrix(self.num_states, self.num_states) |
| 112 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 113 | zero_U = numpy.matrix(numpy.zeros((self.num_inputs, 1))) |
| 114 | print 'S', self.S |
| 115 | print 'Q_final', numerical_jacobian_x_x(self.final_cost, X_hat, zero_U) |
| 116 | return numerical_jacobian_x_x(self.final_cost, X_hat, zero_U) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 117 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 118 | def estimate_partial_cost_partial_x_final(self, X_hat): |
| 119 | """Returns \frac{\partial cost}{\partial X}(X_hat) for the final cost. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 120 | |
| 121 | Args: |
| 122 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 123 | |
| 124 | Result: |
| 125 | numpy.matrix(self.num_states, 1) |
| 126 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 127 | return numerical_jacobian_x( |
| 128 | self.final_cost, X_hat, |
| 129 | numpy.matrix(numpy.zeros((self.num_inputs, 1)))).T |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 130 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 131 | def estimate_q_final(self, X_hat): |
| 132 | """Returns q evaluated at X_hat for the final cost function.""" |
| 133 | return self.estimate_partial_cost_partial_x_final( |
| 134 | X_hat) - self.estimate_Q_final(X_hat) * X_hat |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 135 | |
| 136 | |
| 137 | class SkidSteerDynamics(object): |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 138 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 139 | def __init__(self, dt): |
| 140 | self.width = 0.2 |
| 141 | self.dt = dt |
| 142 | self.num_states = 3 |
| 143 | self.num_inputs = 2 |
| 144 | |
| 145 | def dynamics(self, X, U): |
| 146 | """Calculates the dynamics for a 2 wheeled robot. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 147 | |
| 148 | Args: |
| 149 | X, numpy.matrix(3, 1), The state. [x, y, theta] |
| 150 | U, numpy.matrix(2, 1), The input. [left velocity, right velocity] |
| 151 | |
| 152 | Returns: |
| 153 | numpy.matrix(3, 1), The derivative of the dynamics. |
| 154 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 155 | #return numpy.matrix([[X[1, 0]], |
| 156 | # [X[2, 0]], |
| 157 | # [U[0, 0]]]) |
| 158 | return numpy.matrix([[(U[0, 0] + U[1, 0]) * numpy.cos(X[2, 0]) / 2.0], |
| 159 | [(U[0, 0] + U[1, 0]) * numpy.sin(X[2, 0]) / 2.0], |
| 160 | [(U[1, 0] - U[0, 0]) / self.width]]) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 161 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 162 | def discrete_dynamics(self, X, U): |
| 163 | return RungeKutta(lambda startingX: self.dynamics(startingX, U), X, dt) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 164 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 165 | def inverse_discrete_dynamics(self, X, U): |
| 166 | return RungeKutta(lambda startingX: -self.dynamics(startingX, U), X, |
| 167 | dt) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 168 | |
| 169 | |
| 170 | # Simple implementation for a quadratic cost function. |
| 171 | class CostFunction: |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 172 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 173 | def __init__(self, dt): |
| 174 | self.num_states = 3 |
| 175 | self.num_inputs = 2 |
| 176 | self.dt = dt |
| 177 | self.Q = numpy.matrix([[0.1, 0, 0], [0, 0.6, 0], [0, 0, 0.1] |
| 178 | ]) / self.dt / self.dt |
| 179 | self.R = numpy.matrix([[0.40, 0], [0, 0.40]]) / self.dt / self.dt |
| 180 | |
| 181 | def final_cost(self, X, U): |
| 182 | """Computes the final cost of being at X |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 183 | |
| 184 | Args: |
| 185 | X: numpy.matrix(self.num_states, 1) |
| 186 | U: numpy.matrix(self.num_inputs, 1), ignored |
| 187 | |
| 188 | Returns: |
| 189 | numpy.matrix(1, 1), The quadratic cost of being at X |
| 190 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 191 | return X.T * self.Q * X * 1000 |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 192 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 193 | def cost(self, X, U): |
| 194 | """Computes the incremental cost given a position and U. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 195 | |
| 196 | Args: |
| 197 | X: numpy.matrix(self.num_states, 1) |
| 198 | U: numpy.matrix(self.num_inputs, 1) |
| 199 | |
| 200 | Returns: |
| 201 | numpy.matrix(1, 1), The quadratic cost of evaluating U. |
| 202 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 203 | return U.T * self.R * U + X.T * self.Q * X |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 204 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 205 | def estimate_Q_final(self, X_hat): |
| 206 | """Returns the quadraticized final Q around X_hat. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 207 | |
| 208 | This is calculated by evaluating partial^2 cost(X_hat) / (partial X * partial X) |
| 209 | |
| 210 | Args: |
| 211 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 212 | |
| 213 | Result: |
| 214 | numpy.matrix(self.num_states, self.num_states) |
| 215 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 216 | zero_U = numpy.matrix(numpy.zeros((self.num_inputs, 1))) |
| 217 | return numerical_jacobian_x_x(self.final_cost, X_hat, zero_U) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 218 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 219 | def estimate_partial_cost_partial_x_final(self, X_hat): |
| 220 | """Returns \frac{\partial cost}{\partial X}(X_hat) for the final cost. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 221 | |
| 222 | Args: |
| 223 | X_hat: numpy.matrix(self.num_states, 1), The state to quadraticize around. |
| 224 | |
| 225 | Result: |
| 226 | numpy.matrix(self.num_states, 1) |
| 227 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 228 | return numerical_jacobian_x( |
| 229 | self.final_cost, X_hat, |
| 230 | numpy.matrix(numpy.zeros((self.num_inputs, 1)))).T |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 231 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 232 | def estimate_q_final(self, X_hat): |
| 233 | """Returns q evaluated at X_hat for the final cost function.""" |
| 234 | return self.estimate_partial_cost_partial_x_final( |
| 235 | X_hat) - self.estimate_Q_final(X_hat) * X_hat |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 236 | |
| 237 | |
| 238 | def RungeKutta(f, x, dt): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 239 | """4th order RungeKutta integration of F starting at X.""" |
| 240 | a = f(x) |
| 241 | b = f(x + dt / 2.0 * a) |
| 242 | c = f(x + dt / 2.0 * b) |
| 243 | d = f(x + dt * c) |
| 244 | return x + dt * (a + 2.0 * b + 2.0 * c + d) / 6.0 |
| 245 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 246 | |
| 247 | def numerical_jacobian_x(fn, X, U, epsilon=1e-4): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 248 | """Numerically estimates the jacobian around X, U in X. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 249 | |
| 250 | Args: |
| 251 | fn: A function of X, U. |
| 252 | X: numpy.matrix(num_states, 1), The state vector to take the jacobian |
| 253 | around. |
| 254 | U: numpy.matrix(num_inputs, 1), The input vector to take the jacobian |
| 255 | around. |
| 256 | |
| 257 | Returns: |
| 258 | numpy.matrix(num_states, num_states), The jacobian of fn with X as the |
| 259 | variable. |
| 260 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 261 | num_states = X.shape[0] |
| 262 | nominal = fn(X, U) |
| 263 | answer = numpy.matrix(numpy.zeros((nominal.shape[0], num_states))) |
| 264 | # It's more expensive, but +- epsilon will be more reliable |
| 265 | for i in range(0, num_states): |
| 266 | dX_plus = X.copy() |
| 267 | dX_plus[i] += epsilon |
| 268 | dX_minus = X.copy() |
| 269 | dX_minus[i] -= epsilon |
| 270 | answer[:, i] = (fn(dX_plus, U) - fn(dX_minus, U)) / epsilon / 2.0 |
| 271 | return answer |
| 272 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 273 | |
| 274 | def numerical_jacobian_u(fn, X, U, epsilon=1e-4): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 275 | """Numerically estimates the jacobian around X, U in U. |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 276 | |
| 277 | Args: |
| 278 | fn: A function of X, U. |
| 279 | X: numpy.matrix(num_states, 1), The state vector to take the jacobian |
| 280 | around. |
| 281 | U: numpy.matrix(num_inputs, 1), The input vector to take the jacobian |
| 282 | around. |
| 283 | |
| 284 | Returns: |
| 285 | numpy.matrix(num_states, num_inputs), The jacobian of fn with U as the |
| 286 | variable. |
| 287 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 288 | num_states = X.shape[0] |
| 289 | num_inputs = U.shape[0] |
| 290 | nominal = fn(X, U) |
| 291 | answer = numpy.matrix(numpy.zeros((nominal.shape[0], num_inputs))) |
| 292 | for i in range(0, num_inputs): |
| 293 | dU_plus = U.copy() |
| 294 | dU_plus[i] += epsilon |
| 295 | dU_minus = U.copy() |
| 296 | dU_minus[i] -= epsilon |
| 297 | answer[:, i] = (fn(X, dU_plus) - fn(X, dU_minus)) / epsilon / 2.0 |
| 298 | return answer |
| 299 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 300 | |
| 301 | def numerical_jacobian_x_x(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 302 | return numerical_jacobian_x( |
| 303 | lambda X_inner, U_inner: numerical_jacobian_x(fn, X_inner, U_inner).T, |
| 304 | X, U) |
| 305 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 306 | |
| 307 | def numerical_jacobian_x_u(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 308 | return numerical_jacobian_x( |
| 309 | lambda X_inner, U_inner: numerical_jacobian_u(fn, X_inner, U_inner).T, |
| 310 | X, U) |
| 311 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 312 | |
| 313 | def numerical_jacobian_u_x(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 314 | return numerical_jacobian_u( |
| 315 | lambda X_inner, U_inner: numerical_jacobian_x(fn, X_inner, U_inner).T, |
| 316 | X, U) |
| 317 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 318 | |
| 319 | def numerical_jacobian_u_u(fn, X, U): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 320 | return numerical_jacobian_u( |
| 321 | lambda X_inner, U_inner: numerical_jacobian_u(fn, X_inner, U_inner).T, |
| 322 | X, U) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 323 | |
| 324 | |
| 325 | class ELQR(object): |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 326 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 327 | def __init__(self, dynamics, cost): |
| 328 | self.dynamics = dynamics |
| 329 | self.cost = cost |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 330 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 331 | def Solve(self, x_hat_initial, horizon, iterations): |
| 332 | l = horizon |
| 333 | num_states = self.dynamics.num_states |
| 334 | num_inputs = self.dynamics.num_inputs |
| 335 | self.S_bar_t = [ |
| 336 | numpy.matrix(numpy.zeros((num_states, num_states))) |
| 337 | for _ in range(l + 1) |
| 338 | ] |
| 339 | self.s_bar_t = [ |
| 340 | numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1) |
| 341 | ] |
| 342 | self.s_scalar_bar_t = [ |
| 343 | numpy.matrix(numpy.zeros((1, 1))) for _ in range(l + 1) |
| 344 | ] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 345 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 346 | self.L_t = [ |
| 347 | numpy.matrix(numpy.zeros((num_inputs, num_states))) |
| 348 | for _ in range(l + 1) |
| 349 | ] |
| 350 | self.l_t = [ |
| 351 | numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1) |
| 352 | ] |
| 353 | self.L_bar_t = [ |
| 354 | numpy.matrix(numpy.zeros((num_inputs, num_states))) |
| 355 | for _ in range(l + 1) |
| 356 | ] |
| 357 | self.l_bar_t = [ |
| 358 | numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1) |
| 359 | ] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 360 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 361 | self.S_t = [ |
| 362 | numpy.matrix(numpy.zeros((num_states, num_states))) |
| 363 | for _ in range(l + 1) |
| 364 | ] |
| 365 | self.s_t = [ |
| 366 | numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1) |
| 367 | ] |
| 368 | self.s_scalar_t = [ |
| 369 | numpy.matrix(numpy.zeros((1, 1))) for _ in range(l + 1) |
| 370 | ] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 371 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 372 | self.last_x_hat_t = [ |
| 373 | numpy.matrix(numpy.zeros((num_states, 1))) for _ in range(l + 1) |
| 374 | ] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 375 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 376 | # Iterate the solver |
| 377 | for a in range(iterations): |
| 378 | x_hat = x_hat_initial |
| 379 | u_t = self.L_t[0] * x_hat + self.l_t[0] |
| 380 | self.S_bar_t[0] = numpy.matrix( |
| 381 | numpy.zeros((num_states, num_states))) |
| 382 | self.s_bar_t[0] = numpy.matrix(numpy.zeros((num_states, 1))) |
| 383 | self.s_scalar_bar_t[0] = numpy.matrix([[0]]) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 384 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 385 | self.last_x_hat_t[0] = x_hat_initial |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 386 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 387 | Q_t = numerical_jacobian_x_x(self.cost.cost, x_hat_initial, u_t) |
| 388 | P_t = numerical_jacobian_x_u(self.cost.cost, x_hat_initial, u_t) |
| 389 | R_t = numerical_jacobian_u_u(self.cost.cost, x_hat_initial, u_t) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 390 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 391 | q_t = numerical_jacobian_x(self.cost.cost, x_hat_initial, u_t).T \ |
| 392 | - Q_t * x_hat_initial - P_t.T * u_t |
| 393 | r_t = numerical_jacobian_u(self.cost.cost, x_hat_initial, u_t).T \ |
| 394 | - P_t * x_hat_initial - R_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 395 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 396 | q_scalar_t = self.cost.cost(x_hat_initial, u_t) \ |
| 397 | - 0.5 * (x_hat_initial.T * (Q_t * x_hat_initial + P_t.T * u_t) \ |
| 398 | + u_t.T * (P_t * x_hat_initial + R_t * u_t)) \ |
| 399 | - x_hat_initial.T * q_t - u_t.T * r_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 400 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 401 | start_A_t = numerical_jacobian_x(self.dynamics.discrete_dynamics, |
| 402 | x_hat_initial, u_t) |
| 403 | start_B_t = numerical_jacobian_u(self.dynamics.discrete_dynamics, |
| 404 | x_hat_initial, u_t) |
| 405 | x_hat_next = self.dynamics.discrete_dynamics(x_hat_initial, u_t) |
| 406 | start_c_t = x_hat_next - start_A_t * x_hat_initial - start_B_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 407 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 408 | B_svd_u, B_svd_sigma_diag, B_svd_v = numpy.linalg.svd(start_B_t) |
| 409 | B_svd_sigma = numpy.matrix(numpy.zeros(start_B_t.shape)) |
| 410 | B_svd_sigma[0:B_svd_sigma_diag.shape[0], 0:B_svd_sigma_diag.shape[0]] = \ |
| 411 | numpy.diag(B_svd_sigma_diag) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 412 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 413 | B_svd_sigma_inv = numpy.matrix(numpy.zeros(start_B_t.shape)).T |
| 414 | B_svd_sigma_inv[0:B_svd_sigma_diag.shape[0], |
| 415 | 0:B_svd_sigma_diag.shape[0]] = \ |
| 416 | numpy.linalg.inv(numpy.diag(B_svd_sigma_diag)) |
| 417 | B_svd_inv = B_svd_v.T * B_svd_sigma_inv * B_svd_u.T |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 418 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 419 | self.L_bar_t[1] = B_svd_inv |
| 420 | self.l_bar_t[1] = -B_svd_inv * (start_A_t * x_hat_initial + |
| 421 | start_c_t) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 422 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 423 | self.S_bar_t[1] = self.L_bar_t[1].T * R_t * self.L_bar_t[1] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 424 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 425 | TotalS_1 = start_B_t.T * self.S_t[1] * start_B_t + R_t |
| 426 | Totals_1 = start_B_t.T * self.S_t[1] * (start_c_t + start_A_t * x_hat_initial) \ |
| 427 | + start_B_t.T * self.s_t[1] + P_t * x_hat_initial + r_t |
| 428 | Totals_scalar_1 = 0.5 * (start_c_t.T + x_hat_initial.T * start_A_t.T) * self.S_t[1] * (start_c_t + start_A_t * x_hat_initial) \ |
| 429 | + self.s_scalar_t[1] + x_hat_initial.T * q_t + q_scalar_t \ |
| 430 | + 0.5 * x_hat_initial.T * Q_t * x_hat_initial \ |
| 431 | + (start_c_t.T + x_hat_initial.T * start_A_t.T) * self.s_t[1] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 432 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 433 | optimal_u_1 = -numpy.linalg.solve(TotalS_1, Totals_1) |
| 434 | optimal_x_1 = start_A_t * x_hat_initial \ |
| 435 | + start_B_t * optimal_u_1 + start_c_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 436 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 437 | # TODO(austin): Disable this if we are controlable. It should not be needed then. |
| 438 | S_bar_1_eigh_eigenvalues, S_bar_1_eigh_eigenvectors = \ |
| 439 | numpy.linalg.eigh(self.S_bar_t[1]) |
| 440 | S_bar_1_eigh = numpy.matrix(numpy.diag(S_bar_1_eigh_eigenvalues)) |
| 441 | S_bar_1_eigh_eigenvalues_stiff = S_bar_1_eigh_eigenvalues.copy() |
| 442 | for i in range(S_bar_1_eigh_eigenvalues_stiff.shape[0]): |
| 443 | if abs(S_bar_1_eigh_eigenvalues_stiff[i]) < 1e-8: |
| 444 | S_bar_1_eigh_eigenvalues_stiff[i] = max( |
| 445 | S_bar_1_eigh_eigenvalues_stiff) * 1.0 |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 446 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 447 | S_bar_stiff = S_bar_1_eigh_eigenvectors * numpy.matrix( |
| 448 | numpy.diag(S_bar_1_eigh_eigenvalues_stiff) |
| 449 | ) * S_bar_1_eigh_eigenvectors.T |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 450 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 451 | print 'Min u', -numpy.linalg.solve(TotalS_1, Totals_1) |
| 452 | print 'Min x_hat', optimal_x_1 |
| 453 | self.s_bar_t[1] = -self.s_t[1] - (S_bar_stiff + |
| 454 | self.S_t[1]) * optimal_x_1 |
| 455 | self.s_scalar_bar_t[1] = 0.5 * (optimal_u_1.T * TotalS_1 * optimal_u_1 \ |
| 456 | - optimal_x_1.T * (S_bar_stiff + self.S_t[1]) * optimal_x_1) \ |
| 457 | + optimal_u_1.T * Totals_1 \ |
| 458 | - optimal_x_1.T * (self.s_bar_t[1] + self.s_t[1]) \ |
| 459 | - self.s_scalar_t[1] + Totals_scalar_1 |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 460 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 461 | print 'optimal_u_1', optimal_u_1 |
| 462 | print 'TotalS_1', TotalS_1 |
| 463 | print 'Totals_1', Totals_1 |
| 464 | print 'Totals_scalar_1', Totals_scalar_1 |
| 465 | print 'overall cost 1', 0.5 * (optimal_u_1.T * TotalS_1 * optimal_u_1) \ |
| 466 | + optimal_u_1.T * Totals_1 + Totals_scalar_1 |
| 467 | print 'overall cost 0', 0.5 * (x_hat_initial.T * self.S_t[0] * x_hat_initial) \ |
| 468 | + x_hat_initial.T * self.s_t[0] + self.s_scalar_t[0] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 469 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 470 | print 't forward 0' |
| 471 | print 'x_hat_initial[ 0]: %s' % (x_hat_initial) |
| 472 | print 'x_hat[%2d]: %s' % (0, x_hat.T) |
| 473 | print 'x_hat_next[%2d]: %s' % (0, x_hat_next.T) |
| 474 | print 'u[%2d]: %s' % (0, u_t.T) |
| 475 | print('L[ 0]: %s' % (self.L_t[0], )).replace('\n', '\n ') |
| 476 | print('l[ 0]: %s' % (self.l_t[0], )).replace('\n', '\n ') |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 477 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 478 | print('A_t[%2d]: %s' % (0, start_A_t)).replace('\n', '\n ') |
| 479 | print('B_t[%2d]: %s' % (0, start_B_t)).replace('\n', '\n ') |
| 480 | print('c_t[%2d]: %s' % (0, start_c_t)).replace('\n', '\n ') |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 481 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 482 | # TODO(austin): optimal_x_1 is x_hat |
| 483 | x_hat = -numpy.linalg.solve((self.S_t[1] + S_bar_stiff), |
| 484 | (self.s_t[1] + self.s_bar_t[1])) |
| 485 | print 'new xhat', x_hat |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 486 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 487 | self.S_bar_t[1] = S_bar_stiff |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 488 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 489 | self.last_x_hat_t[1] = x_hat |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 490 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 491 | for t in range(1, l): |
| 492 | print 't forward', t |
| 493 | u_t = self.L_t[t] * x_hat + self.l_t[t] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 494 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 495 | x_hat_next = self.dynamics.discrete_dynamics(x_hat, u_t) |
| 496 | A_bar_t = numerical_jacobian_x( |
| 497 | self.dynamics.inverse_discrete_dynamics, x_hat_next, u_t) |
| 498 | B_bar_t = numerical_jacobian_u( |
| 499 | self.dynamics.inverse_discrete_dynamics, x_hat_next, u_t) |
| 500 | c_bar_t = x_hat - A_bar_t * x_hat_next - B_bar_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 501 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 502 | print 'x_hat[%2d]: %s' % (t, x_hat.T) |
| 503 | print 'x_hat_next[%2d]: %s' % (t, x_hat_next.T) |
| 504 | print('L[%2d]: %s' % ( |
| 505 | t, |
| 506 | self.L_t[t], |
| 507 | )).replace('\n', '\n ') |
| 508 | print('l[%2d]: %s' % ( |
| 509 | t, |
| 510 | self.l_t[t], |
| 511 | )).replace('\n', '\n ') |
| 512 | print 'u[%2d]: %s' % (t, u_t.T) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 513 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 514 | print('A_bar_t[%2d]: %s' % (t, A_bar_t)).replace( |
| 515 | '\n', '\n ') |
| 516 | print('B_bar_t[%2d]: %s' % (t, B_bar_t)).replace( |
| 517 | '\n', '\n ') |
| 518 | print('c_bar_t[%2d]: %s' % (t, c_bar_t)).replace( |
| 519 | '\n', '\n ') |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 520 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 521 | Q_t = numerical_jacobian_x_x(self.cost.cost, x_hat, u_t) |
| 522 | P_t = numerical_jacobian_x_u(self.cost.cost, x_hat, u_t) |
| 523 | R_t = numerical_jacobian_u_u(self.cost.cost, x_hat, u_t) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 524 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 525 | q_t = numerical_jacobian_x(self.cost.cost, x_hat, u_t).T \ |
| 526 | - Q_t * x_hat - P_t.T * u_t |
| 527 | r_t = numerical_jacobian_u(self.cost.cost, x_hat, u_t).T \ |
| 528 | - P_t * x_hat - R_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 529 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 530 | q_scalar_t = self.cost.cost(x_hat, u_t) \ |
| 531 | - 0.5 * (x_hat.T * (Q_t * x_hat + P_t.T * u_t) \ |
| 532 | + u_t.T * (P_t * x_hat + R_t * u_t)) - x_hat.T * q_t - u_t.T * r_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 533 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 534 | C_bar_t = B_bar_t.T * (self.S_bar_t[t] + |
| 535 | Q_t) * A_bar_t + P_t * A_bar_t |
| 536 | D_bar_t = A_bar_t.T * (self.S_bar_t[t] + Q_t) * A_bar_t |
| 537 | E_bar_t = B_bar_t.T * (self.S_bar_t[t] + Q_t) * B_bar_t + R_t \ |
| 538 | + P_t * B_bar_t + B_bar_t.T * P_t.T |
| 539 | d_bar_t = A_bar_t.T * (self.s_bar_t[t] + q_t) \ |
| 540 | + A_bar_t.T * (self.S_bar_t[t] + Q_t) * c_bar_t |
| 541 | e_bar_t = r_t + P_t * c_bar_t + B_bar_t.T * self.s_bar_t[t] \ |
| 542 | + B_bar_t.T * (self.S_bar_t[t] + Q_t) * c_bar_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 543 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 544 | self.L_bar_t[t + 1] = -numpy.linalg.inv(E_bar_t) * C_bar_t |
| 545 | self.l_bar_t[t + 1] = -numpy.linalg.inv(E_bar_t) * e_bar_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 546 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 547 | self.S_bar_t[t + 1] = D_bar_t + C_bar_t.T * self.L_bar_t[t + 1] |
| 548 | self.s_bar_t[t + 1] = d_bar_t + C_bar_t.T * self.l_bar_t[t + 1] |
| 549 | self.s_scalar_bar_t[t + 1] = \ |
| 550 | -0.5 * e_bar_t.T * numpy.linalg.inv(E_bar_t) * e_bar_t \ |
| 551 | + 0.5 * c_bar_t.T * (self.S_bar_t[t] + Q_t) * c_bar_t \ |
| 552 | + c_bar_t.T * self.s_bar_t[t] + c_bar_t.T * q_t \ |
| 553 | + self.s_scalar_bar_t[t] + q_scalar_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 554 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 555 | x_hat = -numpy.linalg.solve( |
| 556 | (self.S_t[t + 1] + self.S_bar_t[t + 1]), |
| 557 | (self.s_t[t + 1] + self.s_bar_t[t + 1])) |
| 558 | self.S_t[l] = self.cost.estimate_Q_final(x_hat) |
| 559 | self.s_t[l] = self.cost.estimate_q_final(x_hat) |
| 560 | x_hat = -numpy.linalg.inv(self.S_t[l] + self.S_bar_t[l]) \ |
| 561 | * (self.s_t[l] + self.s_bar_t[l]) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 562 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 563 | for t in reversed(range(l)): |
| 564 | print 't backward', t |
| 565 | # TODO(austin): I don't think we can use L_t like this here. |
| 566 | # I think we are off by 1 somewhere... |
| 567 | u_t = self.L_bar_t[t + 1] * x_hat + self.l_bar_t[t + 1] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 568 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 569 | x_hat_prev = self.dynamics.inverse_discrete_dynamics( |
| 570 | x_hat, u_t) |
| 571 | print 'x_hat[%2d]: %s' % (t, x_hat.T) |
| 572 | print 'x_hat_prev[%2d]: %s' % (t, x_hat_prev.T) |
| 573 | print('L_bar[%2d]: %s' % (t + 1, self.L_bar_t[t + 1])).replace( |
| 574 | '\n', '\n ') |
| 575 | print('l_bar[%2d]: %s' % (t + 1, self.l_bar_t[t + 1])).replace( |
| 576 | '\n', '\n ') |
| 577 | print 'u[%2d]: %s' % (t, u_t.T) |
| 578 | # Now compute the linearized A, B, and C |
| 579 | # Start by doing it numerically, and then optimize. |
| 580 | A_t = numerical_jacobian_x(self.dynamics.discrete_dynamics, |
| 581 | x_hat_prev, u_t) |
| 582 | B_t = numerical_jacobian_u(self.dynamics.discrete_dynamics, |
| 583 | x_hat_prev, u_t) |
| 584 | c_t = x_hat - A_t * x_hat_prev - B_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 585 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 586 | print('A_t[%2d]: %s' % (t, A_t)).replace('\n', '\n ') |
| 587 | print('B_t[%2d]: %s' % (t, B_t)).replace('\n', '\n ') |
| 588 | print('c_t[%2d]: %s' % (t, c_t)).replace('\n', '\n ') |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 589 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 590 | Q_t = numerical_jacobian_x_x(self.cost.cost, x_hat_prev, u_t) |
| 591 | P_t = numerical_jacobian_x_u(self.cost.cost, x_hat_prev, u_t) |
| 592 | P_T_t = numerical_jacobian_u_x(self.cost.cost, x_hat_prev, u_t) |
| 593 | R_t = numerical_jacobian_u_u(self.cost.cost, x_hat_prev, u_t) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 594 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 595 | q_t = numerical_jacobian_x(self.cost.cost, x_hat_prev, u_t).T \ |
| 596 | - Q_t * x_hat_prev - P_T_t * u_t |
| 597 | r_t = numerical_jacobian_u(self.cost.cost, x_hat_prev, u_t).T \ |
| 598 | - P_t * x_hat_prev - R_t * u_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 599 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 600 | q_scalar_t = self.cost.cost(x_hat_prev, u_t) \ |
| 601 | - 0.5 * (x_hat_prev.T * (Q_t * x_hat_prev + P_t.T * u_t) \ |
| 602 | + u_t.T * (P_t * x_hat_prev + R_t * u_t)) \ |
| 603 | - x_hat_prev.T * q_t - u_t.T * r_t |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 604 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 605 | C_t = P_t + B_t.T * self.S_t[t + 1] * A_t |
| 606 | D_t = Q_t + A_t.T * self.S_t[t + 1] * A_t |
| 607 | E_t = R_t + B_t.T * self.S_t[t + 1] * B_t |
| 608 | d_t = q_t + A_t.T * self.s_t[t + 1] + A_t.T * self.S_t[t + |
| 609 | 1] * c_t |
| 610 | e_t = r_t + B_t.T * self.s_t[t + 1] + B_t.T * self.S_t[t + |
| 611 | 1] * c_t |
| 612 | self.L_t[t] = -numpy.linalg.inv(E_t) * C_t |
| 613 | self.l_t[t] = -numpy.linalg.inv(E_t) * e_t |
| 614 | self.s_t[t] = d_t + C_t.T * self.l_t[t] |
| 615 | self.S_t[t] = D_t + C_t.T * self.L_t[t] |
| 616 | self.s_scalar_t[t] = q_scalar_t \ |
| 617 | - 0.5 * e_t.T * numpy.linalg.inv(E_t) * e_t \ |
| 618 | + 0.5 * c_t.T * self.S_t[t + 1] * c_t \ |
| 619 | + c_t.T * self.s_t[t + 1] \ |
| 620 | + self.s_scalar_t[t + 1] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 621 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 622 | x_hat = -numpy.linalg.solve((self.S_t[t] + self.S_bar_t[t]), |
| 623 | (self.s_t[t] + self.s_bar_t[t])) |
| 624 | if t == 0: |
| 625 | self.last_x_hat_t[t] = x_hat_initial |
| 626 | else: |
| 627 | self.last_x_hat_t[t] = x_hat |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 628 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 629 | x_hat_t = [x_hat_initial] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 630 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 631 | pylab.figure('states %d' % a) |
| 632 | pylab.ion() |
| 633 | for dim in range(num_states): |
| 634 | pylab.plot( |
| 635 | numpy.arange(len(self.last_x_hat_t)), |
| 636 | [x_hat_loop[dim, 0] for x_hat_loop in self.last_x_hat_t], |
| 637 | marker='o', |
| 638 | label='Xhat[%d]' % dim) |
| 639 | pylab.legend() |
| 640 | pylab.draw() |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 641 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 642 | pylab.figure('xy %d' % a) |
| 643 | pylab.ion() |
| 644 | pylab.plot([x_hat_loop[0, 0] for x_hat_loop in self.last_x_hat_t], |
| 645 | [x_hat_loop[1, 0] for x_hat_loop in self.last_x_hat_t], |
| 646 | marker='o', |
| 647 | label='trajectory') |
| 648 | pylab.legend() |
| 649 | pylab.draw() |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 650 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 651 | final_u_t = [ |
| 652 | numpy.matrix(numpy.zeros((num_inputs, 1))) for _ in range(l + 1) |
| 653 | ] |
| 654 | cost_to_go = [] |
| 655 | cost_to_come = [] |
| 656 | cost = [] |
| 657 | for t in range(l): |
| 658 | cost_to_go.append( |
| 659 | (0.5 * self.last_x_hat_t[t].T * self.S_t[t] * self.last_x_hat_t[t] \ |
| 660 | + self.last_x_hat_t[t].T * self.s_t[t] + self.s_scalar_t[t])[0, 0]) |
| 661 | cost_to_come.append( |
| 662 | (0.5 * self.last_x_hat_t[t].T * self.S_bar_t[t] * self.last_x_hat_t[t] \ |
| 663 | + self.last_x_hat_t[t].T * self.s_bar_t[t] + self.s_scalar_bar_t[t])[0, 0]) |
| 664 | cost.append(cost_to_go[-1] + cost_to_come[-1]) |
| 665 | final_u_t[t] = self.L_t[t] * self.last_x_hat_t[t] + self.l_t[t] |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 666 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 667 | for t in range(l): |
| 668 | A_t = numerical_jacobian_x(self.dynamics.discrete_dynamics, |
| 669 | self.last_x_hat_t[t], final_u_t[t]) |
| 670 | B_t = numerical_jacobian_u(self.dynamics.discrete_dynamics, |
| 671 | self.last_x_hat_t[t], final_u_t[t]) |
| 672 | c_t = self.dynamics.discrete_dynamics(self.last_x_hat_t[t], final_u_t[t]) \ |
| 673 | - A_t * self.last_x_hat_t[t] - B_t * final_u_t[t] |
| 674 | print("Infeasability at", t, "is", |
| 675 | ((A_t * self.last_x_hat_t[t] + B_t * final_u_t[t] + c_t) \ |
| 676 | - self.last_x_hat_t[t + 1]).T) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 677 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 678 | pylab.figure('u') |
| 679 | samples = numpy.arange(len(final_u_t)) |
| 680 | for i in range(num_inputs): |
| 681 | pylab.plot(samples, [u[i, 0] for u in final_u_t], |
| 682 | label='u[%d]' % i, |
| 683 | marker='o') |
| 684 | pylab.legend() |
| 685 | |
| 686 | pylab.figure('cost') |
| 687 | cost_samples = numpy.arange(len(cost)) |
| 688 | pylab.plot(cost_samples, cost_to_go, label='cost to go', marker='o') |
| 689 | pylab.plot(cost_samples, |
| 690 | cost_to_come, |
| 691 | label='cost to come', |
| 692 | marker='o') |
| 693 | pylab.plot(cost_samples, cost, label='cost', marker='o') |
| 694 | pylab.legend() |
| 695 | |
| 696 | pylab.ioff() |
| 697 | pylab.show() |
| 698 | |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 699 | |
| 700 | if __name__ == '__main__': |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 701 | dt = 0.05 |
| 702 | #arm_dynamics = ArmDynamics(dt=dt) |
| 703 | #elqr = ELQR(arm_dynamics, ArmCostFunction(dt=dt, dynamics=arm_dynamics)) |
| 704 | #x_hat_initial = numpy.matrix([[0.10], [1.0], [0.0], [0.0]]) |
| 705 | #elqr.Solve(x_hat_initial, 100, 3) |
Austin Schuh | 434c837 | 2018-01-21 16:30:06 -0800 | [diff] [blame] | 706 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 707 | elqr = ELQR(SkidSteerDynamics(dt=dt), CostFunction(dt=dt)) |
| 708 | x_hat_initial = numpy.matrix([[0.10], [1.0], [0.0]]) |
| 709 | elqr.Solve(x_hat_initial, 100, 15) |
| 710 | sys.exit(1) |