|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +class NonlinearSampleSystemConfigModule(): |
| 5 | + # parameters |
| 6 | + ENV_NAME = "NonlinearSampleSystem-v0" |
| 7 | + PLANNER_TYPE = "Const" |
| 8 | + TYPE = "Nonlinear" |
| 9 | + TASK_HORIZON = 2500 |
| 10 | + PRED_LEN = 10 |
| 11 | + STATE_SIZE = 2 |
| 12 | + INPUT_SIZE = 1 |
| 13 | + DT = 0.01 |
| 14 | + R = np.diag([0.01]) |
| 15 | + Q = None |
| 16 | + Sf = None |
| 17 | + # bounds |
| 18 | + INPUT_LOWER_BOUND = np.array([-0.5]) |
| 19 | + INPUT_UPPER_BOUND = np.array([0.5]) |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + """ |
| 23 | + """ |
| 24 | + # opt configs |
| 25 | + self.opt_config = { |
| 26 | + "Random": { |
| 27 | + "popsize": 5000 |
| 28 | + }, |
| 29 | + "CEM": { |
| 30 | + "popsize": 500, |
| 31 | + "num_elites": 50, |
| 32 | + "max_iters": 15, |
| 33 | + "alpha": 0.3, |
| 34 | + "init_var": 9., |
| 35 | + "threshold": 0.001 |
| 36 | + }, |
| 37 | + "MPPI": { |
| 38 | + "beta": 0.6, |
| 39 | + "popsize": 5000, |
| 40 | + "kappa": 0.9, |
| 41 | + "noise_sigma": 0.5, |
| 42 | + }, |
| 43 | + "MPPIWilliams": { |
| 44 | + "popsize": 5000, |
| 45 | + "lambda": 1., |
| 46 | + "noise_sigma": 0.9, |
| 47 | + }, |
| 48 | + "iLQR": { |
| 49 | + "max_iter": 500, |
| 50 | + "init_mu": 1., |
| 51 | + "mu_min": 1e-6, |
| 52 | + "mu_max": 1e10, |
| 53 | + "init_delta": 2., |
| 54 | + "threshold": 1e-6, |
| 55 | + }, |
| 56 | + "DDP": { |
| 57 | + "max_iter": 500, |
| 58 | + "init_mu": 1., |
| 59 | + "mu_min": 1e-6, |
| 60 | + "mu_max": 1e10, |
| 61 | + "init_delta": 2., |
| 62 | + "threshold": 1e-6, |
| 63 | + }, |
| 64 | + "NMPC-CGMRES": { |
| 65 | + }, |
| 66 | + "NMPC-Newton": { |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def input_cost_fn(u): |
| 72 | + """ input cost functions |
| 73 | +
|
| 74 | + Args: |
| 75 | + u (numpy.ndarray): input, shape(pred_len, input_size) |
| 76 | + or shape(pop_size, pred_len, input_size) |
| 77 | + Returns: |
| 78 | + cost (numpy.ndarray): cost of input, shape(pred_len, input_size) or |
| 79 | + shape(pop_size, pred_len, input_size) |
| 80 | + """ |
| 81 | + return (u**2) * np.diag(NonlinearSampleSystemConfigModule.R) |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def state_cost_fn(x, g_x): |
| 85 | + """ state cost function |
| 86 | +
|
| 87 | + Args: |
| 88 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 89 | + or shape(pop_size, pred_len, state_size) |
| 90 | + g_x (numpy.ndarray): goal state, shape(pred_len, state_size) |
| 91 | + or shape(pop_size, pred_len, state_size) |
| 92 | + Returns: |
| 93 | + cost (numpy.ndarray): cost of state, shape(pred_len, 1) or |
| 94 | + shape(pop_size, pred_len, 1) |
| 95 | + """ |
| 96 | + |
| 97 | + if len(x.shape) > 2: |
| 98 | + return (0.5 * (x[:, :, 0]**2) + |
| 99 | + 0.5 * (x[:, :, 1]**2))[:, :, np.newaxis] |
| 100 | + |
| 101 | + elif len(x.shape) > 1: |
| 102 | + return (0.5 * (x[:, 0]**2) + 0.5 * (x[:, 1]**2))[:, np.newaxis] |
| 103 | + |
| 104 | + return 0.5 * (x[0]**2) + 0.5 * (x[1]**2) |
| 105 | + |
| 106 | + @ staticmethod |
| 107 | + def terminal_state_cost_fn(terminal_x, terminal_g_x): |
| 108 | + """ |
| 109 | +
|
| 110 | + Args: |
| 111 | + terminal_x (numpy.ndarray): terminal state, |
| 112 | + shape(state_size, ) or shape(pop_size, state_size) |
| 113 | + terminal_g_x (numpy.ndarray): terminal goal state, |
| 114 | + shape(state_size, ) or shape(pop_size, state_size) |
| 115 | + Returns: |
| 116 | + cost (numpy.ndarray): cost of state, shape(pred_len, ) or |
| 117 | + shape(pop_size, pred_len) |
| 118 | + """ |
| 119 | + |
| 120 | + if len(terminal_x.shape) > 1: |
| 121 | + return (0.5 * (terminal_x[:, 0]**2) + |
| 122 | + 0.5 * (terminal_x[:, 1]**2))[:, np.newaxis] |
| 123 | + |
| 124 | + return 0.5 * (terminal_x[0]**2) + 0.5 * (terminal_x[1]**2) |
| 125 | + |
| 126 | + @ staticmethod |
| 127 | + def gradient_cost_fn_with_state(x, g_x, terminal=False): |
| 128 | + """ gradient of costs with respect to the state |
| 129 | +
|
| 130 | + Args: |
| 131 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 132 | + g_x (numpy.ndarray): goal state, shape(pred_len, state_size) |
| 133 | +
|
| 134 | + Returns: |
| 135 | + l_x (numpy.ndarray): gradient of cost, shape(pred_len, state_size) |
| 136 | + or shape(1, state_size) |
| 137 | + """ |
| 138 | + if not terminal: |
| 139 | + cost_dx0 = x[:, 0] |
| 140 | + cost_dx1 = x[:, 1] |
| 141 | + cost_dx = np.stack((cost_dx0, cost_dx1), axis=1) |
| 142 | + return cost_dx |
| 143 | + |
| 144 | + cost_dx0 = x[0] |
| 145 | + cost_dx1 = x[1] |
| 146 | + cost_dx = np.array([[cost_dx0, cost_dx1]]) |
| 147 | + |
| 148 | + return cost_dx |
| 149 | + |
| 150 | + @ staticmethod |
| 151 | + def gradient_cost_fn_with_input(x, u): |
| 152 | + """ gradient of costs with respect to the input |
| 153 | +
|
| 154 | + Args: |
| 155 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 156 | + u (numpy.ndarray): goal state, shape(pred_len, input_size) |
| 157 | + Returns: |
| 158 | + l_u (numpy.ndarray): gradient of cost, shape(pred_len, input_size) |
| 159 | + """ |
| 160 | + return 2. * u * np.diag(NonlinearSampleSystemConfigModule.R) |
| 161 | + |
| 162 | + @ staticmethod |
| 163 | + def hessian_cost_fn_with_state(x, g_x, terminal=False): |
| 164 | + """ hessian costs with respect to the state |
| 165 | +
|
| 166 | + Args: |
| 167 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 168 | + g_x (numpy.ndarray): goal state, shape(pred_len, state_size) |
| 169 | + Returns: |
| 170 | + l_xx (numpy.ndarray): gradient of cost, |
| 171 | + shape(pred_len, state_size, state_size) or |
| 172 | + shape(1, state_size, state_size) or |
| 173 | + """ |
| 174 | + if not terminal: |
| 175 | + (pred_len, state_size) = x.shape |
| 176 | + hessian = np.eye(state_size) |
| 177 | + hessian = np.tile(hessian, (pred_len, 1, 1)) |
| 178 | + hessian[:, 0, 0] = 1. |
| 179 | + hessian[:, 1, 1] = 1. |
| 180 | + |
| 181 | + return hessian |
| 182 | + |
| 183 | + state_size = len(x) |
| 184 | + hessian = np.eye(state_size) |
| 185 | + hessian[0, 0] = 1. |
| 186 | + hessian[1, 1] = 1. |
| 187 | + |
| 188 | + return hessian[np.newaxis, :, :] |
| 189 | + |
| 190 | + @ staticmethod |
| 191 | + def hessian_cost_fn_with_input(x, u): |
| 192 | + """ hessian costs with respect to the input |
| 193 | +
|
| 194 | + Args: |
| 195 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 196 | + u (numpy.ndarray): goal state, shape(pred_len, input_size) |
| 197 | + Returns: |
| 198 | + l_uu (numpy.ndarray): gradient of cost, |
| 199 | + shape(pred_len, input_size, input_size) |
| 200 | + """ |
| 201 | + (pred_len, _) = u.shape |
| 202 | + |
| 203 | + return np.tile(NonlinearSampleSystemConfigModule.R, (pred_len, 1, 1)) |
| 204 | + |
| 205 | + @ staticmethod |
| 206 | + def hessian_cost_fn_with_input_state(x, u): |
| 207 | + """ hessian costs with respect to the state and input |
| 208 | +
|
| 209 | + Args: |
| 210 | + x (numpy.ndarray): state, shape(pred_len, state_size) |
| 211 | + u (numpy.ndarray): goal state, shape(pred_len, input_size) |
| 212 | + Returns: |
| 213 | + l_ux (numpy.ndarray): gradient of cost , |
| 214 | + shape(pred_len, input_size, state_size) |
| 215 | + """ |
| 216 | + (_, state_size) = x.shape |
| 217 | + (pred_len, input_size) = u.shape |
| 218 | + |
| 219 | + return np.zeros((pred_len, input_size, state_size)) |
0 commit comments