def test_lotka_volterra(): alpha, beta, delta, gamma = 1, 1, 1, 1 lotka_volterra = lambda x, y, t : [diff(x, t) - (alpha*x - beta*x*y), diff(y, t) - (delta*x*y - gamma*y)] init_vals_lv = [ IVP(t_0=0.0, x_0=1.5), IVP(t_0=0.0, x_0=1.0) ] nets_lv = [ FCNN(hidden_units=(32, 32), actv=SinActv), FCNN(hidden_units=(32, 32), actv=SinActv), ] solution_lv, _ = solve_system(ode_system=lotka_volterra, conditions=init_vals_lv, t_min=0.0, t_max=12, nets=nets_lv, max_epochs=12000, monitor=Monitor(t_min=0.0, t_max=12, check_every=100)) ts = np.linspace(0, 12, 100) prey_net, pred_net = solution_lv(ts, as_type='np') def dPdt(P, t): return [P[0]*alpha - beta*P[0]*P[1], delta*P[0]*P[1] - gamma*P[1]] P0 = [1.5, 1.0] Ps = odeint(dPdt, P0, ts) prey_num = Ps[:,0] pred_num = Ps[:,1] assert isclose(prey_net, prey_num, atol=0.1).all() assert isclose(pred_net, pred_num, atol=0.1).all() print('Lotka Volterra test passed.')
def test_monitor(): exponential = lambda x, t: diff(x, t) - x init_val_ex = IVP(t_0=0.0, x_0=1.0) solution_ex, _ = solve(ode=exponential, condition=init_val_ex, t_min=0.0, t_max=2.0, max_epochs=3, monitor=Monitor(t_min=0.0, t_max=2.0, check_every=1)) print('Monitor test passed.')
def test_monitor(): exponential = lambda u, t: diff(u, t) - u init_val_ex = IVP(t_0=0.0, u_0=1.0) solution_ex, _ = solve(ode=exponential, condition=init_val_ex, t_min=0.0, t_max=2.0, max_epochs=3, monitor=Monitor(t_min=0.0, t_max=2.0, check_every=1))