Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions epde/operators/common/coeff_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def apply(self, objective : Equation, arguments : dict = None):
if weight_idx in nonzero_features_indexes:
weights[weight_idx] = valueable_weights[nonzero_features_indexes.index(weight_idx)]
weights[-1] = valueable_weights[-1]
nonzero_terms_mask = np.array([False if np.isclose(weight, 0) else True for weight in weights])
weights = np.array([item if keep else 0 for item, keep in zip(weights, nonzero_terms_mask)])
objective.weights_internal = np.array([item if keep else 0 for item, keep in zip(objective.weights_internal, nonzero_terms_mask[:-1])])
# nonzero_terms_mask = np.array([False if np.isclose(weight, 0) else True for weight in weights])
# weights = np.array([item if keep else 0 for item, keep in zip(weights, nonzero_terms_mask)])
# objective.weights_internal = np.array([item if keep else 0 for item, keep in zip(objective.weights_internal, nonzero_terms_mask[:-1])])
objective.weights_final_evald = True
objective.weights_final = weights

Expand Down
64 changes: 18 additions & 46 deletions epde/operators/common/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =

try:
if features is None:
maximum = target.max(axis=0)
minimum = target.min(axis=0)
discr = (target - target.mean(axis=0) - minimum) / (maximum - minimum)
discr = (target - target.mean(axis=0)) / np.linalg.norm(target, 2)
else:
discr_feats = np.dot(features, objective.weights_final[:-1][objective.weights_internal != 0])
discr_feats = discr_feats + np.full(target.shape, objective.weights_final[-1])
maximum = np.max([discr_feats.max(axis=0), target.max(axis=0)])
minimum = np.min([discr_feats.min(axis=0), target.min(axis=0)])
discr = ((discr_feats - minimum) - (target - minimum)) / (maximum - minimum)
discr = (discr_feats - target) / np.linalg.norm(target, 2)
discr = np.multiply(discr, self.g_fun_vals)
rl_error = np.linalg.norm(discr, ord=2)
except ValueError:
Expand All @@ -145,32 +141,14 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
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

# discr = np.mean(discr ** 2)
# ll = np.log(discr)
# aic = 2 * len(objective.weights_final) - 2 * ll
# ssr = np.sum(discr ** 2)
# n = len(target)
# llf = - n / 2 * np.log(2 * np.pi) - n / 2 * np.log(ssr / n) - n / 2
# aic = 2 * len([_ for _ in objective.weights_final if _ != 0]) - 2 * llf
# aic = np.log(n) * len([_ for _ in objective.weights_final if _ != 0]) - 2 * llf
# objective.aic = 1/(1 + np.exp(- 1e-4 * ll))

# if force_out_of_place:
# return 1 / (np.exp(-aic / 3e5))

# objective.aic = 1 / (np.exp(-aic / 3e5))
objective.aic = None
objective.aic_calculated = True
# print(aic)
# print(len([_ for _ in objective.weights_final if _ !=0]))
# print(objective.aic)
assert objective.simplified, 'Trying to evaluate not simplified equation.'

# assert objective.simplified, 'Trying to evaluate not simplified equation.'

# Calculate r-loss
data_shape = global_var.grid_cache.g_func.shape
Expand All @@ -190,7 +168,7 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
if target_vals.ndim == 1:
window_size = len(target_vals) // 2
num_horizons = len(target_vals) - window_size + 1
if window_size < 15:
if num_horizons < 30:
step_size = 1
else:
step_size = num_horizons // 30
Expand All @@ -200,10 +178,10 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
for start_idx in range(0, num_horizons, step_size):
end_idx = start_idx + window_size
target_window = target_vals[start_idx:end_idx]
if np.isclose(np.sqrt(np.mean(np.power(target_window, 2))), 0, atol=1e-10):
if np.isclose(np.linalg.norm(target_window, 2), 0):
window_stability = np.abs(np.std(target_window))
else:
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
window_stability = np.abs(np.std(target_window) / np.linalg.norm(target_window, 2))
eq_window_weights.append(window_stability)
lr = np.mean(eq_window_weights)
else:
Expand All @@ -217,8 +195,8 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
np.abs(np.std(_)) if np.isclose(np.linalg.norm(_, 2), 0)
else np.abs(np.std(_) / np.linalg.norm(_, 2))
for _ in zip(*eq_window_weights)
])
lr = eq_cv.mean()
Expand All @@ -229,7 +207,7 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
eq_window_weights = []
window_size = target_vals.shape[dim] // 2
num_horizons = target_vals.shape[dim] - window_size + 1
if window_size < 15:
if num_horizons < 30:
step_size = 1
else:
step_size = num_horizons // 30
Expand All @@ -241,10 +219,10 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
target_window = target_vals[start_idx:end_idx, :].reshape(-1)
else:
target_window = target_vals[:, start_idx:end_idx].reshape(-1)
if np.isclose(np.sqrt(np.mean(np.power(target_window, 2))), 0, atol=1e-10):
if np.isclose(np.linalg.norm(target_window, 2), 0):
window_stability = np.abs(np.std(target_window))
else:
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
window_stability = np.abs(np.std(target_window) / np.linalg.norm(target_window, 2))
eq_window_weights.append(window_stability)
lr += np.mean(eq_window_weights)
else:
Expand All @@ -263,8 +241,8 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
np.abs(np.std(_)) if np.isclose(np.linalg.norm(_, 2), 0)
else np.abs(np.std(_) / np.linalg.norm(_, 2))
for _ in zip(*eq_window_weights)
])
lr += eq_cv.mean()
Expand All @@ -285,10 +263,10 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
target_window = target_vals[:, start_idx:end_idx, :].reshape(-1)
else:
target_window = target_vals[:, :, start_idx:end_idx].reshape(-1)
if np.isclose(np.sqrt(np.mean(np.power(target_window, 2))), 0, atol=1e-10):
if np.isclose(np.linalg.norm(target_window, 2), 0):
window_stability = np.abs(np.std(target_window))
else:
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
window_stability = np.abs(np.std(target_window) / np.linalg.norm(target_window, 2))
eq_window_weights.append(window_stability)
lr += np.mean(eq_window_weights)
else:
Expand All @@ -311,21 +289,15 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
np.abs(np.std(_)) if np.isclose(np.linalg.norm(_, 2), 0)
else np.abs(np.std(_) / np.linalg.norm(_, 2))
for _ in zip(*eq_window_weights)
])

lr += eq_cv.mean()

fitness_value = round(fitness_value, 8)
lr = round(lr / target_vals.ndim, 8)

# if lr > self.params['max_lr']:
# self.params['max_lr'] = lr
# if fitness_value > self.params['max_lp']:
# self.params['max_lp'] = fitness_value

objective.fitness_calculated = True
objective.fitness_value = fitness_value
objective.stability_calculated = True
Expand Down
11 changes: 6 additions & 5 deletions epde/operators/common/right_part_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def apply(self, objective : Equation, arguments : dict):

objective.reset_state(True)

while not (objective.right_part_selected and objective.simplified):
while not (objective.simplified and objective.is_correct_right_part):
objective.is_correct_right_part = False
min_fitness = np.inf
weights_internal = np.zeros_like(objective.structure)
min_idx = 0
Expand All @@ -75,8 +76,11 @@ def apply(self, objective : Equation, arguments : dict):
# if not np.isclose(objective.fitness_value, max_fitness) and global_var.verbose.show_warnings:
# warnings.warn('Reevaluation of fitness function for equation has obtained different result. Not an error, if ANN DE solver is used.')
self.simplify_equation(objective)
if objective.structure[objective.target_idx].contains_variable(objective.main_var_to_explain) and objective.structure[objective.target_idx].contains_deriv(objective.main_var_to_explain):
objective.is_correct_right_part = True
else:
objective.reset_explaining_term(objective.target_idx)
objective.right_part_selected = True

def simplify_equation(self, objective: Equation):
# Get nonzero terms
Expand Down Expand Up @@ -121,11 +125,8 @@ def simplify_equation(self, objective: Equation):
while objective.structure.count(term) > 1 or term == temp:
term.randomize()
term.reset_saved_state()
objective.simplified = False
objective.right_part_selected = False
return
return
objective.simplified = True
objective.right_part_selected = True

def use_default_tags(self):
self._tags = {'equation right part selection', 'gene level', 'contains suboperators', 'inplace'}
Expand Down
Loading