def gen_trace(kwargs): duplicates = lambda U, v: (np.fabs(U - v) < 1e-6).all(axis=0) soln = df_sv.fiber_solver(**kwargs) fps = df_fx.sanitize_points(soln['Fixed points'], kwargs['f'], kwargs['ef'], kwargs['Df'], duplicates) return fps, soln['Fiber trace'].points, soln['Fiber trace'].step_amounts
def sanity3D(): # Set up fiber arguments # v = np.ones((3,1)) + np.random.randn(3,1)*0.1 v = np.zeros((3, 1)) c = f(v) # v = np.ones((3,1)) # c = np.random.randn(*v.shape) fiber_kwargs = { "f": f, "ef": ef, "Df": Df, "compute_step_amount": compute_step_amount, "v": v, "c": c, "max_step_size": 1, "max_traverse_steps": 120, "max_solve_iterations": 2**5, } # Run in one direction solution = sv.fiber_solver(**fiber_kwargs) trace = solution["Fiber trace"] # V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1,::10] V1 = np.concatenate(trace.points, axis=1)[:-1, ::10] z = trace.z_initial # # Run in other direction (negate initial tangent) # fiber_kwargs["z"] = -z # solution = sv.fiber_solver(**fiber_kwargs) # V2 = np.concatenate(trace.points, axis=1)[:-1,::10] # # Join fiber segments # V = np.concatenate((np.fliplr(V1), V2), axis=1) # C = f(V) # print(V) V = V1 C = f(V) * 0.0001 print(np.sqrt((C * C).sum(axis=0))) # print(V) # print(trace.residuals) # print(trace.step_amounts) ax = pt.gca(projection='3d') ax.plot(*V, marker='o', color='k') ax.quiver(*np.concatenate((V, C), axis=0), color='k', arrow_length_ratio=0.00001) pt.show()
def run_fiber_solver(W, **kwargs): """ rnn-specific convenience wrapper for generic fiber solver and post-processing W is the (N,N) weight matrix; kwargs are as in dfibers.solvers.fiber_solver """ # Setup random c for termination criteria N = W.shape[0] if "c" in kwargs: c = kwargs["c"] else: c = np.random.randn(N,1) c = c/np.linalg.norm(c) kwargs["c"] = c if "compute_step_amount" not in kwargs: kwargs["compute_step_amount"] = compute_step_amount_factory(W) # Run solver from origin solution = sv.fiber_solver( f = f_factory(W), ef = ef_factory(W), Df = Df_factory(W), v = np.zeros((N,1)), terminate = terminate_factory(W, c), max_solve_iterations = 2**5, **kwargs ) # Post-process fixed points fxpts = solution["Fixed points"] fxpts = np.concatenate((-fxpts, np.zeros((N,1)), fxpts), axis=1) fxpts = fx.sanitize_points( fxpts, f = f_factory(W), ef = ef_factory(W), Df = Df_factory(W), duplicates = duplicates_factory(W), ) # Return post-processed fixed points and full solver result return fxpts, solution
ax = pt.gca(projection="3d") ax.plot(*A, color='gray', linestyle='-', alpha=0.5) br1 = np.sqrt(b * (r - 1)) U = np.array([[0, 0, 0], [br1, br1, r - 1], [-br1, -br1, r - 1]]).T ax.scatter(*U, color='black') # Run and visualize fiber components, for each fxpt xlims, ylims, zlims = [-20, 20], [-30, 30], [-20, 60] for fc in [0, 2]: # start from current fxpt fiber_kwargs["v"] = U[:, [fc]] # ax.text(U[0,fc],U[1,fc],U[2,fc], str(fc)) # Run in one direction solution = sv.fiber_solver(**fiber_kwargs) V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:N, :] z = solution["Fiber trace"].z_initial # Run in other direction (negate initial tangent) fiber_kwargs["z"] = -z solution = sv.fiber_solver(**fiber_kwargs) V2 = np.concatenate(solution["Fiber trace"].points, axis=1)[:N, :] # Join fiber segments, restrict to figure limits V = np.concatenate((np.fliplr(V1), V2), axis=1) V = V[:, ::50] for i, (lo, hi) in enumerate([xlims, ylims, zlims]): V = V[:, (lo < V[i, :]) & (V[i, :] < hi)] C = f(V)
def sanity2D(): # Set up fiber arguments v = np.array([[2], [.5]]) fiber_kwargs = { "f": f, "ef": ef, "Df": Df, "compute_step_amount": compute_step_amount, "v": v, "c": f(v), "max_step_size": 1, "max_traverse_steps": 2000, "max_solve_iterations": 2**5, } # Run in one direction solution = sv.fiber_solver(**fiber_kwargs) V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1, ::10] z = solution["Fiber trace"].z_initial # Run in other direction (negate initial tangent) fiber_kwargs["z"] = -z solution = sv.fiber_solver(**fiber_kwargs) V2 = np.concatenate(solution["Fiber trace"].points, axis=1)[:-1, ::10] # Join fiber segments V = np.concatenate((np.fliplr(V1), V2), axis=1) # Grids for fiber and surface X_fiber, Y_fiber = np.mgrid[-2.5:3.5:40j, -1:4:40j] X_surface, Y_surface = np.mgrid[-2.5:3.5:100j, -1:4:100j] # Compute rosenbrock R_surface = R(np.array([X_surface.flatten(), Y_surface.flatten()])) R_surface = R_surface.reshape(X_surface.shape) # Visualize fiber and surface ax_fiber = pt.subplot(2, 1, 2) tv.plot_fiber(X_fiber, Y_fiber, V, f, ax=ax_fiber, scale_XY=500, scale_V=25) ax_fiber.plot([1], [1], 'ko') # global optimum ax_surface = pt.gcf().add_subplot(2, 1, 1, projection="3d") ax_surface.plot_surface(X_surface, Y_surface, R_surface, linewidth=0, antialiased=False, color='gray') ax_surface.view_init(azim=-98, elev=21) for ax in [ax_fiber, ax_surface]: ax.set_xlabel("x") ax.set_ylabel("y") if ax == ax_surface: ax.set_zlabel("R(x,y)", rotation=90) ax.set_zlim([0, 10000]) ax.view_init(elev=40, azim=-106) ax.set_xlim([-2.5, 3.5]) pt.show()
def run_trial(args): basename, sample, timeout = args stop_time = time.clock() + timeout logfile = open("%s_s%d.log" % (basename, sample), "w") # Set up fiber arguments np.random.seed() v = 20 * np.random.rand(2, 1) - 10 # random point in domain c = lv.f(v) # direction at that point c = c + 0.1 * np.random.randn(2, 1) # perturb for more variability fiber_kwargs = { "f": lv.f, "ef": lv.ef, "Df": lv.Df, "compute_step_amount": lambda trace: (0.0001, 0), "v": v, "c": c, "stop_time": stop_time, "terminate": lambda trace: (np.fabs(trace.x[:-1]) > 10).any(), "max_solve_iterations": 2**5, } solve_start = time.clock() # Run in one direction solution = sv.fiber_solver(logger=lu.Logger(logfile).plus_prefix("+: "), **fiber_kwargs) X1 = np.concatenate(solution["Fiber trace"].points, axis=1) V1 = solution["Fixed points"] z = solution["Fiber trace"].z_initial # print("Status: %s\n"%solution["Fiber trace"].status) # Run in other direction (negate initial tangent) solution = sv.fiber_solver(z=-z, logger=lu.Logger(logfile).plus_prefix("-: "), **fiber_kwargs) X2 = np.concatenate(solution["Fiber trace"].points, axis=1) V2 = solution["Fixed points"] # print("Status: %s\n"%solution["Fiber trace"].status) # Join fiber segments fiber = np.concatenate((np.fliplr(X1), X2), axis=1) # Union solutions fxpts = fx.sanitize_points( np.concatenate((V1, V2), axis=1), f=lv.f, ef=lv.ef, Df=lv.Df, duplicates=lambda V, v: (np.fabs(V - v) < 10**-6).all(axis=0), ) # Save results with open("%s_s%d.npz" % (basename, sample), 'w') as rf: np.savez( rf, **{ "fxpts": fxpts, "fiber": fiber, "runtime": time.clock() - solve_start }) logfile.close()
compute_step_amount = rnn.compute_step_amount_factory(W) x = 0.01 * np.random.randn(N + 1, 1) c = np.random.randn(N, 1) c = c / np.linalg.norm(c) terminate = rnn.terminate_factory(W, c) max_solve_iterations = 2**5 solve_tolerance = 10**-18 max_step_size = 1 solution = sv.fiber_solver( f, ef, Df, compute_step_amount, v=x[:N, :], c=c, terminate=terminate, max_step_size=0.01, max_traverse_steps=1000, max_solve_iterations=max_solve_iterations, solve_tolerance=solve_tolerance, ) fiber = solution["Fiber trace"] X = np.concatenate(fiber.points, axis=1) z = fiber.z_initial print("%d steps" % X.shape[1]) # X = np.concatenate((-np.fliplr(X), X), axis=1) V = X[:-1, :] C = f(V) lm = 1.25
import dfibers.traversal as tv # help(tv.FiberTrace) terminate = lambda trace: (np.fabs(trace.x) > 10**6).any() compute_step_amount = lambda trace: (10**-3, None, False) import dfibers.solvers as sv # help(tv.traverse_fiber) # help(sv.fiber_solver) solution = sv.fiber_solver( f, ef, Df, compute_step_amount, v = np.zeros((N,1)), terminate=terminate, max_traverse_steps=10**3, max_solve_iterations=2**5, ) duplicates = lambda U, v: (np.fabs(U - v) < 2**-21).all(axis=0) import dfibers.fixed_points as fx V = solution["Fixed points"] V = fx.sanitize_points(V, f, ef, Df, duplicates) print("Fixed points:") print(V) print("Fixed point residuals:") print(f(V))