forked from ITMO-NSS-team/EPDE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitness.py
More file actions
402 lines (306 loc) · 15.5 KB
/
Copy pathfitness.py
File metadata and controls
402 lines (306 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 4 13:20:59 2021
@author: mike_ubuntu
"""
import numpy as np
from copy import deepcopy
import torch
import matplotlib.pyplot as plt
from matplotlib import cm
from epde.integrate import SolverAdapter
from epde.structure.main_structures import SoEq, Equation
from epde.operators.utils.template import CompoundOperator
import epde.globals as global_var
from sklearn.linear_model import LinearRegression, Ridge
from scipy.optimize import minimize
from epde.supplementary import minmax_normalize
from epde.supplementary import calculate_weights
LOSS_NAN_VAL = 1e7
class L2Fitness(CompoundOperator):
"""
The operator, which calculates fitness function to the individual (equation) as the L2 norm
of the vector of disrepancy between left part of the equation and the right part, evaluated
on the grid nodes.
Notable attributes:
-------------------
params : dict
Inhereted from the ``CompoundOperator`` class.
Parameters of the operator; main parameters:
penalty_coeff - penalty coefficient, to that the fitness function value of equation with no non-zero coefficients, is multiplied;
suboperators : dict
Methods:
-----------
apply(equation)
calculate the fitness function of the equation, that will be stored in the equation.fitness_value.
"""
key = 'DiscrepancyBasedFitness'
def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool = False):
"""
Calculate the fitness function values. The result is not returned, but stored in the equation.fitness_value attribute.
Parameters:
------------
equation : Equation object
the equation object, to that the fitness function is obtained.
Returns:
------------
None
"""
self_args, subop_args = self.parse_suboperator_args(arguments = arguments)
if force_out_of_place:
self.suboperators['sparsity'].apply(objective, subop_args['sparsity'])
self.suboperators['coeff_calc'].apply(objective, subop_args['coeff_calc'])
_, target, features = objective.evaluate(normalize = False, return_val = False)
if features is None:
discr_feats = 0
else:
discr_feats = np.dot(features, objective.weights_final[:-1][objective.weights_internal != 0])
discr = (discr_feats + np.full(target.shape, objective.weights_final[-1]) - target)
self.g_fun_vals = global_var.grid_cache.g_func_flat
discr = np.multiply(discr, self.g_fun_vals)
rl_error = np.linalg.norm(discr, ord = 2)
if not (self.params['penalty_coeff'] > 0. and self.params['penalty_coeff'] < 1.):
raise ValueError('Incorrect penalty coefficient set, value shall be in (0, 1).')
fitness_value = rl_error
if np.sum(objective.weights_final) == 0:
fitness_value /= self.params['penalty_coeff']
if force_out_of_place:
return fitness_value
else:
objective.fitness_calculated = True
objective.fitness_value = fitness_value
def use_default_tags(self):
self._tags = {'fitness evaluation', 'gene level', 'contains suboperators', 'inplace'}
class L2LRFitness(CompoundOperator):
key = 'DiscrepancyBasedFitnessWithCV'
def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool = False):
"""
Calculate the fitness function values. The result is not returned, but stored in the equation.fitness_value attribute.
Parameters:
------------
equation : Equation object
the equation object, to that the fitness function is obtained.
Returns:
------------
None
"""
self_args, subop_args = self.parse_suboperator_args(arguments=arguments)
# self.suboperators['sparsity'].apply(objective, subop_args['sparsity'])
if force_out_of_place:
self.suboperators['sparsity'].apply(objective, subop_args['sparsity'])
if all(objective.weights_internal == 0):
return None
self.suboperators['coeff_calc'].apply(objective, subop_args['coeff_calc'])
if force_out_of_place:
_, target, features = objective.evaluate(normalize=False, return_val=False)
else:
_, target, features = objective.evaluate(normalize=True, return_val=False)
# _, target, features = objective.evaluate(normalize=False, return_val=False)
self.get_g_fun_vals()
if features is None:
discr = target - target.mean()
else:
discr_feats = np.dot(features, objective.weights_final[:-1])
discr_feats = discr_feats + objective.weights_final[-1]
discr = target - discr_feats
rl_error = np.sum(np.abs(discr)) / np.sum(np.abs(target))
if not (self.params['penalty_coeff'] > 0. and self.params['penalty_coeff'] < 1.):
raise ValueError('Incorrect penalty coefficient set, value shall be in (0, 1).')
fitness_value = rl_error
if force_out_of_place:
return fitness_value
objective.aic = None
objective.aic_calculated = True
data_shape = global_var.grid_cache.inner_shape
if hasattr(objective, '_cached_sw_weights') and objective._cached_sw_weights is not None:
weights = objective._cached_sw_weights
else:
weights = calculate_weights(features, target, self.g_fun_vals, data_shape)
weights_arr = np.array(weights)
std = weights_arr.std(axis=0, ddof=1)
mu = weights_arr.mean(axis=0)
# Safe division
with np.errstate(divide='ignore', invalid='ignore'):
cv = (std ** 2) / (mu ** 2)
cv[mu == 0] = 0.0 # Handle zero mean
total_lr = sum(cv[:-1]) / len(data_shape)
objective.fitness_calculated = True
objective.fitness_value = fitness_value
objective.stability_calculated = True
objective.coefficients_stability = total_lr
def get_g_fun_vals(self):
try:
self.g_fun_vals = global_var.grid_cache.g_func[global_var.grid_cache.g_func_mask].reshape(-1)
except AttributeError:
self.g_fun_vals = None
def use_default_tags(self):
self._tags = {'fitness evaluation', 'gene level', 'contains suboperators', 'inplace'}
class SolverBasedFitness(CompoundOperator):
# To be modified to include physics-informed information criterion (PIC)
key = 'SolverBasedFitness'
def __init__(self, param_keys: list):
super().__init__(param_keys)
self.adapter = None
def set_adapter(self, net = None):
if self.adapter is None or net is not None:
compiling_params = {'mode': 'autograd', 'tol':0.01, 'lambda_bound': 100} # 'h': 1e-1
optimizer_params = {}
training_params = {'epochs': 1e3, 'info_string_every' : 1e3}
early_stopping_params = {'patience': 4, 'no_improvement_patience' : 250}
explicit_cpu = False
device = 'cuda' if (torch.cuda.is_available and not explicit_cpu) else 'cpu'
self.adapter = SolverAdapter(net = net, use_cache = False, device=device)
self.adapter.set_compiling_params(**compiling_params)
self.adapter.set_optimizer_params(**optimizer_params)
self.adapter.set_early_stopping_params(**early_stopping_params)
self.adapter.set_training_params(**training_params)
def apply(self, objective : SoEq, arguments : dict, force_out_of_place: bool = False):
self_args, subop_args = self.parse_suboperator_args(arguments = arguments)
try:
net = deepcopy(global_var.solution_guess_nn)
except NameError:
net = None
self.set_adapter(net=net)
if force_out_of_place:
self.suboperators['sparsity'].apply(objective, subop_args['sparsity'])
self.suboperators['coeff_calc'].apply(objective, subop_args['coeff_calc'])
print('solving equation:')
print(objective.text_form)
loss_add, solution_nn = self.adapter.solve_epde_system(system = objective, grids = None,
boundary_conditions = None, use_fourier=True)
_, grids = global_var.grid_cache.get_all(mode = 'torch')
grids = torch.stack([grid.reshape(-1) for grid in grids], dim = 1).float()
solution = solution_nn(grids).detach().cpu().numpy()
self.g_fun_vals = global_var.grid_cache.g_func
if force_out_of_place:
sum_err = 0
for eq_idx, eq in enumerate(objective.vals):
if torch.isnan(loss_add):
fitness_value = 2*LOSS_NAN_VAL
else:
referential_data = global_var.tensor_cache.get((eq.main_var_to_explain, (1.0,)))
discr = (solution[..., eq_idx] - referential_data.reshape(solution[..., eq_idx].shape))
discr = np.multiply(discr, self.g_fun_vals.reshape(discr.shape))
rl_error = np.linalg.norm(discr, ord = 2)
print(f'fitness error is {rl_error}, while loss addition is {float(loss_add)}')
fitness_value = rl_error + self.params['pinn_loss_mult'] * float(loss_add) # TODO: make pinn_loss_mult case dependent
if np.sum(eq.weights_final) == 0:
fitness_value /= self.params['penalty_coeff']
if force_out_of_place:
sum_err += fitness_value
else:
eq.fitness_calculated = True
eq.fitness_value = fitness_value
def use_default_tags(self):
self._tags = {'fitness evaluation', 'chromosome level', 'contains suboperators', 'inplace'}
class PIC(CompoundOperator):
key = 'PIC'
def __init__(self, param_keys: list):
super().__init__(param_keys)
self.adapter = None
def set_adapter(self, net=None):
if self.adapter is None or net is not None:
compiling_params = {'mode': 'autograd', 'tol': 0.01, 'lambda_bound': 100} # 'h': 1e-1
optimizer_params = {}
training_params = {'epochs': 1e3, 'info_string_every': 1e3}
early_stopping_params = {'patience': 4, 'no_improvement_patience': 250}
explicit_cpu = False
device = 'cuda' if (torch.cuda.is_available and not explicit_cpu) else 'cpu'
self.adapter = SolverAdapter(net=net, use_cache=False, device=device)
self.adapter.set_compiling_params(**compiling_params)
self.adapter.set_optimizer_params(**optimizer_params)
self.adapter.set_early_stopping_params(**early_stopping_params)
self.adapter.set_training_params(**training_params)
def apply(self, objective: SoEq, arguments: dict, force_out_of_place: bool = False):
self_args, subop_args = self.parse_suboperator_args(arguments=arguments)
try:
net = deepcopy(global_var.solution_guess_nn)
except NameError:
net = None
self.set_adapter(net=net)
if force_out_of_place:
self.suboperators['sparsity'].apply(objective, subop_args['sparsity'])
self.suboperators['coeff_calc'].apply(objective, subop_args['coeff_calc'])
print('solving equation:')
print(objective.text_form)
loss_add, solution_nn = self.adapter.solve_epde_system(system=objective, grids=None,
boundary_conditions=None, use_fourier=True)
_, grids = global_var.grid_cache.get_all(mode='torch')
g_mask = global_var.grid_cache.g_func_mask
grids = [grid[g_mask] for grid in grids]
grids = torch.stack([grid.reshape(-1) for grid in grids], dim=1).float()
solution = solution_nn(grids).detach().cpu().numpy()
self.g_fun_vals = global_var.grid_cache.g_func[g_mask]
if force_out_of_place:
sum_err = 0
for eq_idx, eq in enumerate(objective.vals):
# Calculate p-loss
if torch.isnan(loss_add):
lp = 2 * LOSS_NAN_VAL
else:
referential_data = global_var.tensor_cache.get((eq.main_var_to_explain, (1.0,)))
discr = solution[..., eq_idx] - referential_data.reshape(solution[..., eq_idx].shape)
discr = np.multiply(discr, self.g_fun_vals.reshape(discr.shape))
# rl_error = np.sqrt(np.mean(discr ** 2))
# rl_error = np.sum(np.abs(discr)) / np.sum(np.abs(referential_data.reshape(solution[..., eq_idx].shape))) * 100
rl_error = np.mean(discr ** 2)
print(f'fitness error is {rl_error}, while loss addition is {float(loss_add)}')
lp = rl_error + self.params['pinn_loss_mult'] * float(
loss_add) # TODO: make pinn_loss_mult case dependent
if force_out_of_place:
sum_err += lp
continue
eq.aic_calculated = True
# Calculate r-loss
data_shape = global_var.grid_cache.inner_shape
_, target, features = eq.evaluate(normalize=True, return_val=False)
if hasattr(eq, '_cached_sw_weights') and eq._cached_sw_weights is not None:
weights = eq._cached_sw_weights
else:
weights = calculate_weights(features, target, self.g_fun_vals, data_shape)
weights_arr = np.array(weights)
std = weights_arr.std(axis=0, ddof=1)
mu = weights_arr.mean(axis=0)
# Safe division
with np.errstate(divide='ignore', invalid='ignore'):
cv = (std ** 2) / (mu ** 2)
cv[mu == 0] = 0.0 # Handle zero mean
total_lr = sum(cv[:-1]) / len(data_shape)
eq.fitness_calculated = True
eq.fitness_value = lp
eq.stability_calculated = True
eq.coefficients_stability = total_lr
def feature_reshape(self, features_vals):
features = features_vals[0]
if len(features_vals) > 1:
for i in range(1, len(features_vals)):
features = np.vstack([features, features_vals[i]])
features = np.vstack([features, np.ones(features_vals[0].shape)]) # Add constant feature
features = np.transpose(features)
if features.ndim == 1:
features = features.reshape(-1, 1)
return features
def get_g_fun_vals(self):
try:
self.g_fun_vals = global_var.grid_cache.g_func_flat
except AttributeError:
self.g_fun_vals = None
def use_default_tags(self):
self._tags = {'fitness evaluation', 'chromosome level', 'contains suboperators', 'inplace'}
def plot_data_vs_solution(grid, data, solution):
if grid.shape[1]==2:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(grid[:,0].reshape(-1), grid[:,1].reshape(-1),
solution.reshape(-1), cmap=cm.jet, linewidth=0.2)
ax.set_xlabel("x1")
ax.set_ylabel("x2")
plt.show()
if grid.shape[1]==1:
fig = plt.figure()
plt.scatter(grid.reshape(-1), solution.reshape(-1), color = 'r')
plt.scatter(grid.reshape(-1), data.reshape(-1), color = 'k')
plt.show()
else:
raise Exception('Infeasible dimensionality of the input dataset.')