@@ -6,12 +6,12 @@ class NonlinearSampleSystemConfigModule():
66 ENV_NAME = "NonlinearSampleSystem-v0"
77 PLANNER_TYPE = "Const"
88 TYPE = "Nonlinear"
9- TASK_HORIZON = 2500
9+ TASK_HORIZON = 2000
1010 PRED_LEN = 10
1111 STATE_SIZE = 2
1212 INPUT_SIZE = 1
1313 DT = 0.01
14- R = np .diag ([0.01 ])
14+ R = np .diag ([1. ])
1515 Q = None
1616 Sf = None
1717 # bounds
@@ -46,24 +46,33 @@ def __init__(self):
4646 "noise_sigma" : 0.9 ,
4747 },
4848 "iLQR" : {
49- "max_iter " : 500 ,
49+ "max_iters " : 500 ,
5050 "init_mu" : 1. ,
5151 "mu_min" : 1e-6 ,
5252 "mu_max" : 1e10 ,
5353 "init_delta" : 2. ,
5454 "threshold" : 1e-6 ,
5555 },
5656 "DDP" : {
57- "max_iter " : 500 ,
57+ "max_iters " : 500 ,
5858 "init_mu" : 1. ,
5959 "mu_min" : 1e-6 ,
6060 "mu_max" : 1e10 ,
6161 "init_delta" : 2. ,
6262 "threshold" : 1e-6 ,
6363 },
64+ "NMPC" : {
65+ "threshold" : 1e-5 ,
66+ "max_iters" : 1000 ,
67+ "learning_rate" : 0.1
68+ },
6469 "NMPC-CGMRES" : {
70+ "threshold" : 1e-3
6571 },
6672 "NMPC-Newton" : {
73+ "threshold" : 1e-3 ,
74+ "max_iteration" : 500 ,
75+ "learning_rate" : 1e-3
6776 },
6877 }
6978
@@ -103,7 +112,7 @@ def state_cost_fn(x, g_x):
103112
104113 return 0.5 * (x [0 ]** 2 ) + 0.5 * (x [1 ]** 2 )
105114
106- @ staticmethod
115+ @staticmethod
107116 def terminal_state_cost_fn (terminal_x , terminal_g_x ):
108117 """
109118
@@ -123,7 +132,7 @@ def terminal_state_cost_fn(terminal_x, terminal_g_x):
123132
124133 return 0.5 * (terminal_x [0 ]** 2 ) + 0.5 * (terminal_x [1 ]** 2 )
125134
126- @ staticmethod
135+ @staticmethod
127136 def gradient_cost_fn_with_state (x , g_x , terminal = False ):
128137 """ gradient of costs with respect to the state
129138
@@ -147,7 +156,7 @@ def gradient_cost_fn_with_state(x, g_x, terminal=False):
147156
148157 return cost_dx
149158
150- @ staticmethod
159+ @staticmethod
151160 def gradient_cost_fn_with_input (x , u ):
152161 """ gradient of costs with respect to the input
153162
@@ -159,7 +168,7 @@ def gradient_cost_fn_with_input(x, u):
159168 """
160169 return 2. * u * np .diag (NonlinearSampleSystemConfigModule .R )
161170
162- @ staticmethod
171+ @staticmethod
163172 def hessian_cost_fn_with_state (x , g_x , terminal = False ):
164173 """ hessian costs with respect to the state
165174
@@ -187,7 +196,7 @@ def hessian_cost_fn_with_state(x, g_x, terminal=False):
187196
188197 return hessian [np .newaxis , :, :]
189198
190- @ staticmethod
199+ @staticmethod
191200 def hessian_cost_fn_with_input (x , u ):
192201 """ hessian costs with respect to the input
193202
@@ -202,7 +211,7 @@ def hessian_cost_fn_with_input(x, u):
202211
203212 return np .tile (NonlinearSampleSystemConfigModule .R , (pred_len , 1 , 1 ))
204213
205- @ staticmethod
214+ @staticmethod
206215 def hessian_cost_fn_with_input_state (x , u ):
207216 """ hessian costs with respect to the state and input
208217
@@ -217,3 +226,71 @@ def hessian_cost_fn_with_input_state(x, u):
217226 (pred_len , input_size ) = u .shape
218227
219228 return np .zeros ((pred_len , input_size , state_size ))
229+
230+ @staticmethod
231+ def gradient_hamiltonian_input (x , lam , u , g_x ):
232+ """
233+
234+ Args:
235+ x (numpy.ndarray): shape(pred_len+1, state_size)
236+ lam (numpy.ndarray): shape(pred_len, state_size)
237+ u (numpy.ndarray): shape(pred_len, input_size)
238+ g_xs (numpy.ndarray): shape(pred_len, state_size)
239+
240+ Returns:
241+ F (numpy.ndarray), shape(pred_len, input_size)
242+ """
243+ if len (x .shape ) == 1 :
244+ input_size = u .shape [0 ]
245+ F = np .zeros (input_size )
246+ F [0 ] = u [0 ] + lam [1 ]
247+
248+ return F
249+
250+ elif len (x .shape ) == 2 :
251+ pred_len , input_size = u .shape
252+ F = np .zeros ((pred_len , input_size ))
253+
254+ for i in range (pred_len ):
255+ F [i , 0 ] = u [i , 0 ] + lam [i , 1 ]
256+
257+ return F
258+
259+ else :
260+ raise NotImplementedError
261+
262+ @staticmethod
263+ def gradient_hamiltonian_state (x , lam , u , g_x ):
264+ """
265+ Args:
266+ x (numpy.ndarray): shape(pred_len+1, state_size)
267+ lam (numpy.ndarray): shape(pred_len, state_size)
268+ u (numpy.ndarray): shape(pred_len, input_size)
269+ g_xs (numpy.ndarray): shape(pred_len, state_size)
270+
271+ Returns:
272+ lam_dot (numpy.ndarray), shape(state_size, )
273+ """
274+ if len (lam .shape ) == 1 :
275+ state_size = lam .shape [0 ]
276+ lam_dot = np .zeros (state_size )
277+ lam_dot [0 ] = x [0 ] - (2. * x [0 ] * x [1 ] + 1. ) * lam [1 ]
278+ lam_dot [1 ] = x [1 ] + lam [0 ] + \
279+ (- 3. * (x [1 ]** 2 ) - x [0 ]** 2 + 1. ) * lam [1 ]
280+
281+ return lam_dot
282+
283+ elif len (lam .shape ) == 2 :
284+ pred_len , state_size = lam .shape
285+ lam_dot = np .zeros ((pred_len , state_size ))
286+
287+ for i in range (pred_len ):
288+ lam_dot [i , 0 ] = x [i , 0 ] - \
289+ (2. * x [i , 0 ] * x [i , 1 ] + 1. ) * lam [i , 1 ]
290+ lam_dot [i , 1 ] = x [i , 1 ] + lam [i , 0 ] + \
291+ (- 3. * (x [i , 1 ]** 2 ) - x [i , 0 ]** 2 + 1. ) * lam [i , 1 ]
292+
293+ return lam_dot
294+
295+ else :
296+ raise NotImplementedError
0 commit comments