Skip to content
Closed
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/interface/equation_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _(text_form : str, pool, all_vars: List[str], use_pic: bool = False):
metaparameters={'terms_number': {'optimizable': False, 'value': len(term_list)},
'max_factors_in_term': {'optimizable': False, 'value': max_factors}}
for var_key in all_vars:
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 1.}
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 0.}


equation = Equation(pool=pool, basic_structure=term_list, var_to_explain = all_vars[0],
Expand Down Expand Up @@ -95,7 +95,7 @@ def _(text_form : dict, pool, all_vars: List[str], use_pic: bool = False):
metaparameters={'terms_number': {'optimizable': False, 'value': len(term_list)},
'max_factors_in_term': {'optimizable': False, 'value': max_factors}}
for var_key in all_vars:
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 1.}
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 0.}

equation = Equation(pool = pool, basic_structure = term_list, var_to_explain = var_key,
metaparameters = metaparameters)
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(self, lp_terms : Union[list, tuple, dict], rp_term : Union[list, tu
metaparameters={'terms_number': {'optimizable': False, 'value': len(term_list)},
'max_factors_in_term': {'optimizable': False, 'value': max_factors}}
for var_key in all_vars:
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 1.}
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 0.}

equation = Equation(pool=pool, basic_structure=terms_aggregated,
metaparameters=metaparameters)
Expand Down
37 changes: 31 additions & 6 deletions epde/operators/common/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ 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]
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
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))))
eq_window_weights.append(window_stability)
lr = np.mean(eq_window_weights)
else:
features = self.feature_reshape(features_vals)
Expand All @@ -212,7 +216,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals[start_idx:end_idx])
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_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))))
for _ in zip(*eq_window_weights)
])
lr = eq_cv.mean()

elif target_vals.ndim == 2:
Expand All @@ -233,7 +241,11 @@ 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)
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
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))))
eq_window_weights.append(window_stability)
lr += np.mean(eq_window_weights)
else:
features = self.feature_reshape(features_vals)
Expand All @@ -250,7 +262,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals.reshape(*data_shape, -1)[:, start_idx:end_idx].reshape(-1))
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_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))))
for _ in zip(*eq_window_weights)
])
lr += eq_cv.mean()

elif target_vals.ndim == 3:
Expand All @@ -269,7 +285,11 @@ 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)
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
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))))
eq_window_weights.append(window_stability)
lr += np.mean(eq_window_weights)
else:
features = self.feature_reshape(features_vals)
Expand All @@ -290,7 +310,12 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals.reshape(*data_shape, -1)[:, :, start_idx:end_idx].reshape(-1))
valuable_weights = estimator.coef_[:-1]
eq_window_weights.append(valuable_weights)
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_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))))
for _ in zip(*eq_window_weights)
])

lr += eq_cv.mean()

objective.fitness_calculated = True
Expand Down
47 changes: 21 additions & 26 deletions epde/operators/multiobjective/moeadd_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,38 +343,33 @@ def best_obj_values(levels : ParetoLevels):

class OffspringUpdater(CompoundOperator):
key = 'ParetoLevelUpdater'
def apply(self, objective : ParetoLevels, arguments : dict):
self_args, subop_args = self.parse_suboperator_args(arguments = arguments)

def apply(self, objective: ParetoLevels, arguments: dict):
self_args, subop_args = self.parse_suboperator_args(arguments=arguments)

while objective.unplaced_candidates:
offspring = objective.unplaced_candidates.pop()
attempt = 1; attempt_limit = self.params['attempt_limit']
attempt = 1;
attempt_limit = self.params['attempt_limit']
temp_offspring = self.suboperators['chromosome_mutation'].apply(objective=offspring,
arguments=subop_args['chromosome_mutation'])
while True:
temp_offspring = self.suboperators['chromosome_mutation'].apply(objective = offspring,
arguments = subop_args['chromosome_mutation'])
self.suboperators['right_part_selector'].apply(objective = temp_offspring,
arguments = subop_args['right_part_selector'])
self.suboperators['chromosome_fitness'].apply(objective = temp_offspring,
arguments = subop_args['chromosome_fitness'])

if all([temp_offspring != solution for solution in objective.population]):
self.suboperators['pareto_level_updater'].apply(objective = (temp_offspring, objective),
arguments = subop_args['pareto_level_updater'])
self.suboperators['right_part_selector'].apply(objective=temp_offspring,
arguments=subop_args['right_part_selector'])
self.suboperators['chromosome_fitness'].apply(objective=temp_offspring,
arguments=subop_args['chromosome_fitness'])

if all([not np.allclose(temp_offspring.obj_fun, solution.obj_fun) for solution in objective.population]):
self.suboperators['pareto_level_updater'].apply(objective=(temp_offspring, objective),
arguments=subop_args['pareto_level_updater'])
break
elif attempt >= attempt_limit:
# print(temp_offspring.text_form)
# print('-----------------------')
# for idx, individual in enumerate(objective.population):
# print(f'Individual {idx}')
# print(individual.text_form)
# print('-----------------------')
# raise Exception('Can not place individual into the population. Try decreasing population size or increasing token variety. ')
print('The algorithm had issues with generating unique offsprings, allowed replication.')
self.suboperators['pareto_level_updater'].apply(objective = (temp_offspring, objective),
arguments = subop_args['pareto_level_updater'])

break
# print('The algorithm had issues with generating unique offsprings.')
temp_offspring.create()
# temp_offspring.reset_state()
attempt = 1
self.suboperators['chromosome_mutation'].apply(objective=temp_offspring,
arguments=subop_args['chromosome_mutation'])
attempt += 1
return objective

Expand Down