1+ import numpy as np
2+ import copy
3+ from scipy .integrate import solve_ivp
4+ from epde .interface .interface import EpdeSearch
5+ from epde .interface .equation_translator import translate_equation
6+ from epde .structure .main_structures import SoEq , Chromosome
7+ from epde .interface .token_family import TFPool
8+ from epde .operators .utils .default_parameter_loader import EvolutionaryParams
9+ from epde .solver .factory import SolverFactory
10+
11+ def create_equation_from_str (eq_str , target_var , base_pool , all_vars ):
12+ families_copy = [copy .deepcopy (fam ) for fam in base_pool .families ]
13+ for fam in families_copy :
14+ if hasattr (fam , 'variable' ) and fam .variable is not None and fam .variable != target_var :
15+ fam .status ['demands_equation' ] = False
16+ temp_pool = TFPool (families_copy )
17+ soeq = translate_equation (eq_str , temp_pool , all_vars = [target_var ])
18+ return soeq .vals [target_var ]
19+
20+ def lv_rhs (t , y ):
21+ u , v = y
22+ alpha , beta , gamma , delta = 2 / 3 , 4 / 3 , 1.0 , 1.0
23+ du = alpha * u - beta * u * v
24+ dv = delta * u * v - gamma * v
25+ return [du , dv ]
26+
27+ t = np .linspace (0 , 20 , 200 )
28+ sol_ref = solve_ivp (lv_rhs , (0 ,20 ), [1.0 ,1.0 ], t_eval = t , method = 'RK45' , rtol = 1e-6 , atol = 1e-9 )
29+ exact_u , exact_v = sol_ref .y
30+
31+ data_u = exact_u + 0.01 * np .random .normal (size = exact_u .shape )
32+ data_v = exact_v + 0.01 * np .random .normal (size = exact_v .shape )
33+
34+ search = EpdeSearch (
35+ use_solver = False ,
36+ multiobjective_mode = True ,
37+ coordinate_tensors = [t ],
38+ verbose_params = {'show_iter_idx' : False },
39+ device = 'cpu'
40+ )
41+ search .set_preprocessor (default_preprocessor_type = 'FD' , preprocessor_kwargs = {})
42+ search .create_pool (data = [data_u , data_v ], variable_names = ['u' , 'v' ], max_deriv_order = 1 , additional_tokens = [])
43+
44+ correct_eqs = [
45+ '0.6666666666666666 * u{power: 1.0} + -1.3333333333333333 * u{power: 1.0} * v{power: 1.0} = du/dx0{power: 1.0}' ,
46+ '1.0 * u{power: 1.0} * v{power: 1.0} + -1.0 * v{power: 1.0} = dv/dx0{power: 1.0}'
47+ ]
48+
49+ eq_u = create_equation_from_str (correct_eqs [0 ], 'u' , search .pool , ['u' , 'v' ])
50+ eq_u .main_var_to_explain = 'u'
51+ eq_u .weights_internal = np .ones (len (eq_u .structure ) - 1 )
52+ eq_u .weights_internal_evald = True
53+ eq_u .weights_final_evald = True
54+
55+ eq_v = create_equation_from_str (correct_eqs [1 ], 'v' , search .pool , ['u' , 'v' ])
56+ eq_v .main_var_to_explain = 'v'
57+ eq_v .weights_internal = np .ones (len (eq_v .structure ) - 1 )
58+ eq_v .weights_internal_evald = True
59+ eq_v .weights_final_evald = True
60+
61+ system = SoEq (search .pool , {})
62+ system .vals = Chromosome ({'u' : eq_u , 'v' : eq_v }, {})
63+ system .moeadd_set = True
64+
65+ def solve_with_solver (solver_type , solver_config , system , t , data ):
66+ adapter = SolverFactory .create (solver_type , ** solver_config )
67+ solutions , loss = adapter .solve (system , [t ], data )
68+ return solutions , loss
69+
70+
71+ print ("=" * 50 )
72+ print ("Classical ODE solver (RK45)" )
73+ print ("=" * 50 )
74+
75+ solver_config_classical = {
76+ "method" : "RK45" ,
77+ "rtol" : 1e-6 ,
78+ "atol" : 1e-9 ,
79+ "rhs" : lv_rhs ,
80+ "y0" : [1.0 , 1.0 ]
81+ }
82+ solutions_cl , loss_cl = solve_with_solver ("classical_ode" , solver_config_classical , system , t , [data_u , data_v ])
83+ print (f"Loss (RMSE): { loss_cl :.6f} " )
84+ print (f"Max error u: { np .max (np .abs (solutions_cl [0 ] - exact_u )):.6f} " )
85+ print (f"Max error v: { np .max (np .abs (solutions_cl [1 ] - exact_v )):.6f} " )
86+
87+ print ("\n " + "=" * 50 )
88+ print ("DeepXDE (PINN) solver" )
89+ print ("=" * 50 )
90+
91+ solver_config_deepxde = EvolutionaryParams ().get_default_params_for_operator ('DeepXDEBasedFitness' )
92+ solutions_dx , loss_dx = solve_with_solver ("deepxde" , solver_config_deepxde , system , t , [data_u , data_v ])
93+ print (f"Loss (RMSE): { loss_dx :.6f} " )
94+ print (f"Max error u: { np .max (np .abs (solutions_dx [0 ] - exact_u )):.6f} " )
95+ print (f"Max error v: { np .max (np .abs (solutions_dx [1 ] - exact_v )):.6f} " )
0 commit comments