@@ -62,18 +62,11 @@ def __init__(self):
6262 "threshold" : 1e-6 ,
6363 },
6464 "NMPC" : {
65- "threshold" : 1e-5 ,
66- "max_iters" : 1000 ,
67- "learning_rate" : 0.1
68- },
69- "NMPC-CGMRES" : {
70- "threshold" : 1e-3
71- },
72- "NMPC-Newton" : {
73- "threshold" : 1e-3 ,
74- "max_iteration" : 500 ,
75- "learning_rate" : 1e-3
76- },
65+ "threshold" : 0.01 ,
66+ "max_iters" : 5000 ,
67+ "learning_rate" : 0.01 ,
68+ "optimizer_mode" : "conjugate"
69+ }
7770 }
7871
7972 @staticmethod
@@ -133,7 +126,7 @@ def terminal_state_cost_fn(terminal_x, terminal_g_x):
133126 return 0.5 * (terminal_x [0 ]** 2 ) + 0.5 * (terminal_x [1 ]** 2 )
134127
135128 @staticmethod
136- def gradient_cost_fn_with_state (x , g_x , terminal = False ):
129+ def gradient_cost_fn_state (x , g_x , terminal = False ):
137130 """ gradient of costs with respect to the state
138131
139132 Args:
@@ -157,7 +150,7 @@ def gradient_cost_fn_with_state(x, g_x, terminal=False):
157150 return cost_dx
158151
159152 @staticmethod
160- def gradient_cost_fn_with_input (x , u ):
153+ def gradient_cost_fn_input (x , u ):
161154 """ gradient of costs with respect to the input
162155
163156 Args:
@@ -169,7 +162,7 @@ def gradient_cost_fn_with_input(x, u):
169162 return 2. * u * np .diag (NonlinearSampleSystemConfigModule .R )
170163
171164 @staticmethod
172- def hessian_cost_fn_with_state (x , g_x , terminal = False ):
165+ def hessian_cost_fn_state (x , g_x , terminal = False ):
173166 """ hessian costs with respect to the state
174167
175168 Args:
@@ -197,7 +190,7 @@ def hessian_cost_fn_with_state(x, g_x, terminal=False):
197190 return hessian [np .newaxis , :, :]
198191
199192 @staticmethod
200- def hessian_cost_fn_with_input (x , u ):
193+ def hessian_cost_fn_input (x , u ):
201194 """ hessian costs with respect to the input
202195
203196 Args:
@@ -212,7 +205,7 @@ def hessian_cost_fn_with_input(x, u):
212205 return np .tile (NonlinearSampleSystemConfigModule .R , (pred_len , 1 , 1 ))
213206
214207 @staticmethod
215- def hessian_cost_fn_with_input_state (x , u ):
208+ def hessian_cost_fn_input_state (x , u ):
216209 """ hessian costs with respect to the state and input
217210
218211 Args:
@@ -294,3 +287,67 @@ def gradient_hamiltonian_state(x, lam, u, g_x):
294287
295288 else :
296289 raise NotImplementedError
290+
291+
292+ class NonlinearSampleSystemExtendConfigModule (NonlinearSampleSystemConfigModule ):
293+ def __init__ (self ):
294+ super ().__init__ ()
295+ self .opt_config = {
296+ "NMPCCGMRES" : {
297+ "threshold" : 1e-3 ,
298+ "zeta" : 100. ,
299+ "delta" : 0.01 ,
300+ "alpha" : 0.5 ,
301+ "tf" : 1. ,
302+ "constraint" : True
303+ },
304+ "NMPCNewton" : {
305+ "threshold" : 1e-3 ,
306+ "max_iteration" : 500 ,
307+ "learning_rate" : 1e-3
308+ }
309+ }
310+
311+ @staticmethod
312+ def gradient_hamiltonian_input_with_constraint (x , lam , u , g_x , dummy_u , raw ):
313+ """
314+
315+ Args:
316+ x (numpy.ndarray): shape(pred_len+1, state_size)
317+ lam (numpy.ndarray): shape(pred_len, state_size)
318+ u (numpy.ndarray): shape(pred_len, input_size)
319+ g_xs (numpy.ndarray): shape(pred_len, state_size)
320+ dummy_u (numpy.ndarray): shape(pred_len, input_size)
321+ raw (numpy.ndarray): shape(pred_len, input_size), Lagrangian for constraints
322+
323+ Returns:
324+ F (numpy.ndarray), shape(pred_len, 3)
325+ """
326+ if len (x .shape ) == 1 :
327+ vanilla_F = np .zeros (1 )
328+ extend_F = np .zeros (1 ) # 1 is the same as input size
329+ extend_C = np .zeros (1 )
330+
331+ vanilla_F [0 ] = u [0 ] + lam [1 ] + 2. * raw [0 ] * u [0 ]
332+ extend_F [0 ] = - 0.01 + 2. * raw [0 ] * dummy_u [0 ]
333+ extend_C [0 ] = u [0 ]** 2 + dummy_u [0 ]** 2 - \
334+ NonlinearSampleSystemConfigModule .INPUT_LOWER_BOUND ** 2
335+
336+ F = np .concatenate ([vanilla_F , extend_F , extend_C ])
337+
338+ elif len (x .shape ) == 2 :
339+ pred_len , _ = u .shape
340+ vanilla_F = np .zeros ((pred_len , 1 ))
341+ extend_F = np .zeros ((pred_len , 1 )) # 1 is the same as input size
342+ extend_C = np .zeros ((pred_len , 1 ))
343+
344+ for i in range (pred_len ):
345+ vanilla_F [i , 0 ] = \
346+ u [i , 0 ] + lam [i , 1 ] + 2. * raw [i , 0 ] * u [i , 0 ]
347+ extend_F [i , 0 ] = - 0.01 + 2. * raw [i , 0 ] * dummy_u [i , 0 ]
348+ extend_C [i , 0 ] = u [i , 0 ]** 2 + dummy_u [i , 0 ]** 2 - \
349+ NonlinearSampleSystemConfigModule .INPUT_LOWER_BOUND ** 2
350+
351+ F = np .concatenate ([vanilla_F , extend_F , extend_C ], axis = 1 )
352+
353+ return F
0 commit comments