#NEWLINE# def select_step( x , A_h , g_h , c_h , p , p_h , d , lb , ub , theta ) : #NEWLINE# #INDENT# 'Select the best step according to Trust Region Reflective algorithm.' #NEWLINE# if in_bounds ( ( x + p ) , lb , ub ) : #NEWLINE# #INDENT# return p #NEWLINE# #UNINDENT# ( p_stride , hits ) = step_size_to_bound ( x , p , lb , ub ) #NEWLINE# r_h = np . copy ( p_h ) #NEWLINE# r_h [ hits . astype ( bool ) ] *= ( - 1 ) #NEWLINE# r = ( d * r_h ) #NEWLINE# p *= p_stride #NEWLINE# p_h *= p_stride #NEWLINE# x_on_bound = ( x + p ) #NEWLINE# ( r_stride_u , _ ) = step_size_to_bound ( x_on_bound , r , lb , ub ) #NEWLINE# r_stride_l = ( ( 1 - theta ) * r_stride_u ) #NEWLINE# r_stride_u *= theta #NEWLINE# if ( r_stride_u > 0 ) : #NEWLINE# #INDENT# ( a , b , c ) = build_quadratic_1d ( A_h , g_h , r_h , s0 = p_h , diag = c_h ) #NEWLINE# ( r_stride , r_value ) = minimize_quadratic_1d ( a , b , r_stride_l , r_stride_u , c = c ) #NEWLINE# r_h = ( p_h + ( r_h * r_stride ) ) #NEWLINE# r = ( d * r_h ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# r_value = np . inf #NEWLINE# #UNINDENT# p_h *= theta #NEWLINE# p *= theta #NEWLINE# p_value = evaluate_quadratic ( A_h , g_h , p_h , diag = c_h ) #NEWLINE# ag_h = ( - g_h ) #NEWLINE# ag = ( d * ag_h ) #NEWLINE# ( ag_stride_u , _ ) = step_size_to_bound ( x , ag , lb , ub ) #NEWLINE# ag_stride_u *= theta #NEWLINE# ( a , b ) = build_quadratic_1d ( A_h , g_h , ag_h , diag = c_h ) #NEWLINE# ( ag_stride , ag_value ) = minimize_quadratic_1d ( a , b , 0 , ag_stride_u ) #NEWLINE# ag *= ag_stride #NEWLINE# if ( ( p_value < r_value ) and ( p_value < ag_value ) ) : #NEWLINE# #INDENT# return p #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( r_value < p_value ) and ( r_value < ag_value ) ) : #NEWLINE# #INDENT# return r #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ag