def find_root(self): L_alpha = [1.0, 10.0] L_lam = [[1.0, 2.0, 3.0], [1.0, 1.0], [0.0], [-1.0, 1.0], [-1.0, 0.0, 1.0], [1.0, -2.0, -3.0], [-3.0, -4.0, -5.0, -6.0]] L_rho = [1e-2, 1e-1, 1e0, 1e1] L_tol = [1e-3, 1e-5, 1e-10, 1e-14] for alpha_r, lam, rho, tol in product(L_alpha, L_lam, L_rho, L_tol): alpha = ones(len(lam)) * alpha_r lam = array(lam) tol = rho * tol if min(lam) > 0: if calculate_distance(alpha, lam, 0.0) < rho: continue print 'alpha', alpha, 'lam', lam, 'rho', rho, 'tol', tol nu = find_step_size(alpha, lam, rho, tol) f_nu = calculate_distance(alpha, lam, nu) print 'nu', nu, 'f(nu)', f_nu abs(rho - calculate_distance(alpha, lam, nu)) < Assert(tol)
def main(): parser = argparse.ArgumentParser( description="Test root-finding method to find step size in trust region optimization methods." ) parser.add_argument("--alpha", type=float) parser.add_argument("--lam", type=float, nargs="+") parser.add_argument("--rho", type=float) args = parser.parse_args() alpha = args.alpha lam = array(args.lam) rho = args.rho print "α:", alpha print "λ:", lam print "ρ:", rho print "f(0)=", calculate_distance(alpha, lam, 0) nu = find_step_size(alpha, lam, rho, 1e-14) print "nu=", nu print "f(nu)-rho=", calculate_distance(alpha, lam, nu) - rho rez = 0.01 xx = mgrid[nu / 100.0 : nu * 100 : rez] yy = array([calculate_distance(alpha, lam, x) for x in xx]) ion() plot(xx, yy) plot([nu / 10.0, nu * 10.0], [rho, rho], "r-") plot(nu, calculate_distance(alpha, lam, nu), "rd") grid() code.interact()