Skip to content

Commit 902fc2e

Browse files
Merge pull request #7 from Yaroslav-Muravev/DeepXDEBasedFitness
Current version of Unified DeepXDE
2 parents 48d8aa2 + ddd66d7 commit 902fc2e

3 files changed

Lines changed: 100 additions & 14 deletions

File tree

epde/integrate/deepxde_integration.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ def func(x):
117117

118118
if len(left_idx) > 0:
119119
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(left_idx),
120-
lambda _, on_boundary: on_boundary and np.isclose(_.x[0], x.min(),
120+
lambda _, on_boundary: on_boundary and np.isclose(_[0], x.min(),
121121
rtol=1e-5,
122122
atol=eps_x),
123-
component=var_idx))
123+
component=var_idx)) # Заменил _.x[0] на _[0]
124124
if len(right_idx) > 0:
125125
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(right_idx),
126-
lambda _, on_boundary: on_boundary and np.isclose(_.x[0], x.max(),
126+
lambda _, on_boundary: on_boundary and np.isclose(_[0], x.max(),
127127
rtol=1e-5,
128128
atol=eps_x),
129-
component=var_idx))
129+
component=var_idx)) # Заменил _.x[0] на _[0]
130130
if len(initial_idx) > 0:
131131
bcs.append(dde.icbc.IC(geomtime, make_bc_func(initial_idx),
132132
lambda _, on_initial: on_initial,
@@ -197,28 +197,28 @@ def func(x):
197197

198198
if len(x_min_idx) > 0:
199199
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(x_min_idx),
200-
lambda _, on_boundary: on_boundary and np.isclose(_.x[0], x.min(),
200+
lambda _, on_boundary: on_boundary and np.isclose(_[0], x.min(),
201201
rtol=1e-5,
202202
atol=eps_x),
203-
component=var_idx))
203+
component=var_idx)) # Заменил _.x[0] на _[0]
204204
if len(x_max_idx) > 0:
205205
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(x_max_idx),
206-
lambda _, on_boundary: on_boundary and np.isclose(_.x[0], x.max(),
206+
lambda _, on_boundary: on_boundary and np.isclose(_[0], x.max(),
207207
rtol=1e-5,
208208
atol=eps_x),
209-
component=var_idx))
209+
component=var_idx)) # Заменил _.x[0] на _[0]
210210
if len(y_min_idx) > 0:
211211
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(y_min_idx),
212-
lambda _, on_boundary: on_boundary and np.isclose(_.x[1], y.min(),
212+
lambda _, on_boundary: on_boundary and np.isclose(_[1], y.min(),
213213
rtol=1e-5,
214214
atol=eps_y),
215-
component=var_idx))
215+
component=var_idx)) # Заменил _.x[1] на _[1]
216216
if len(y_max_idx) > 0:
217217
bcs.append(dde.icbc.DirichletBC(geomtime, make_bc_func(y_max_idx),
218-
lambda _, on_boundary: on_boundary and np.isclose(_.x[1], y.max(),
218+
lambda _, on_boundary: on_boundary and np.isclose(_[1], y.max(),
219219
rtol=1e-5,
220220
atol=eps_y),
221-
component=var_idx))
221+
component=var_idx)) # Заменил _.x[1] на _[1]
222222
if len(initial_idx) > 0:
223223
bcs.append(dde.icbc.IC(geomtime, make_bc_func(initial_idx),
224224
lambda _, on_initial: on_initial,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import numpy as np
2+
import copy
3+
from epde.interface.interface import EpdeSearch
4+
from epde.interface.equation_translator import translate_equation
5+
from epde.structure.main_structures import SoEq, Chromosome
6+
from epde.interface.token_family import TFPool
7+
from epde.operators.utils.default_parameter_loader import EvolutionaryParams
8+
from epde.solver.factory import SolverFactory
9+
10+
def create_equation_from_str(eq_str, target_var, base_pool, all_vars):
11+
families_copy = [copy.deepcopy(fam) for fam in base_pool.families]
12+
for fam in families_copy:
13+
if hasattr(fam, 'variable') and fam.variable is not None and fam.variable != target_var:
14+
fam.status['demands_equation'] = False
15+
temp_pool = TFPool(families_copy)
16+
soeq = translate_equation(eq_str, temp_pool, all_vars=[target_var])
17+
return soeq.vals[target_var]
18+
19+
def solve_with_solver(solver_type, solver_config, system, grids, data):
20+
adapter = SolverFactory.create(solver_type, **solver_config)
21+
solutions, loss = adapter.solve(system, grids, data)
22+
return solutions, loss
23+
24+
# ----------------------------------------------------------------------
25+
# Волновое уравнение (PDE) – только DeepXDE
26+
# ----------------------------------------------------------------------
27+
print("=" * 60)
28+
print("Wave equation (PDE) – DeepXDE PINN solver")
29+
print("=" * 60)
30+
31+
# Генерация данных (аналитическое решение)
32+
nx, nt = 50, 50
33+
x = np.linspace(0, 1, nx)
34+
t = np.linspace(0, 2, nt)
35+
X_grid, T_grid = np.meshgrid(t, x, indexing='ij')
36+
exact = np.sin(np.pi * X_grid) * np.cos(np.pi * T_grid)
37+
data_wave = exact + 0.01 * np.random.normal(size=exact.shape)
38+
39+
# Создание пула EPDE
40+
search_wave = EpdeSearch(
41+
use_solver=False,
42+
coordinate_tensors=(T_grid, X_grid),
43+
verbose_params={'show_iter_idx': False},
44+
device='cpu'
45+
)
46+
search_wave.set_preprocessor(default_preprocessor_type='FD', preprocessor_kwargs={})
47+
search_wave.create_pool(
48+
data=data_wave,
49+
variable_names=['u'],
50+
max_deriv_order=(2, 2),
51+
additional_tokens=[]
52+
)
53+
54+
# Уравнение волновое
55+
eq_str = '1.0 * d^2u/dx1^2{power: 1.0} = d^2u/dx0^2{power: 1.0}'
56+
soeq_wave = translate_equation(eq_str, search_wave.pool, all_vars=['u'])
57+
eq_wave = soeq_wave.vals['u']
58+
eq_wave.main_var_to_explain = 'u'
59+
eq_wave.weights_internal = np.ones(len(eq_wave.structure) - 1)
60+
eq_wave.weights_internal_evald = True
61+
eq_wave.weights_final_evald = True
62+
63+
system_wave = SoEq(search_wave.pool, {})
64+
system_wave.vals = Chromosome({'u': eq_wave}, {})
65+
system_wave.moeadd_set = True
66+
67+
# Конфигурация DeepXDE (увеличиваем параметры для PDE)
68+
solver_config_deepxde = EvolutionaryParams().get_default_params_for_operator('DeepXDEBasedFitness')
69+
solver_config_deepxde['num_domain'] = 2000
70+
solver_config_deepxde['num_boundary'] = 500
71+
solver_config_deepxde['num_initial'] = 500
72+
solver_config_deepxde['epochs'] = 3000
73+
74+
solutions_wave, loss_wave = solve_with_solver(
75+
"deepxde",
76+
solver_config_deepxde,
77+
system_wave,
78+
[T_grid, X_grid],
79+
[data_wave.flatten()]
80+
)
81+
82+
soln_wave = solutions_wave[0].reshape(data_wave.shape)
83+
print(f"Loss (RMSE): {loss_wave:.6f}")
84+
print(f"Max error: {np.max(np.abs(soln_wave - exact)):.6f}")

tests/functional/operator_factory.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ def create(name: str, params: dict) -> CompoundOperator:
4040
operator = SolverBasedFitness(list(params.keys()), objectives=[primary],
4141
primary=primary, stability=Instability(),
4242
backend='deepxde')
43-
sparsity = LASSOSparsity()
44-
coeff_calc = LinRegBasedCoeffsEquation()
43+
#sparsity = LASSOSparsity()
44+
#coeff_calc = LinRegBasedCoeffsEquation()
45+
sparsity = map_operator_between_levels(LASSOSparsity(), 'gene level', 'chromosome level')
46+
coeff_calc = map_operator_between_levels(LinRegBasedCoeffsEquation(), 'gene level', 'chromosome level')
4547
else:
4648
raise ValueError(f"Unknown operator: {name}")
4749

0 commit comments

Comments
 (0)