Skip to content

Commit 84547c9

Browse files
authored
Merge pull request #41 from Gromwud/main
OffsprinUpdater and Lr fitness update
2 parents 25c9e35 + 51b950b commit 84547c9

3 files changed

Lines changed: 55 additions & 35 deletions

File tree

epde/interface/equation_translator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _(text_form : str, pool, all_vars: List[str], use_pic: bool = False):
5050
metaparameters={'terms_number': {'optimizable': False, 'value': len(term_list)},
5151
'max_factors_in_term': {'optimizable': False, 'value': max_factors}}
5252
for var_key in all_vars:
53-
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 1.}
53+
metaparameters[('sparsity', var_key)] = {'optimizable': True, 'value': 0.}
5454

5555

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

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

190190
equation = Equation(pool=pool, basic_structure=terms_aggregated,
191191
metaparameters=metaparameters)

epde/operators/common/fitness.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
200200
for start_idx in range(0, num_horizons, step_size):
201201
end_idx = start_idx + window_size
202202
target_window = target_vals[start_idx:end_idx]
203-
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
203+
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
204+
window_stability = np.abs(np.std(target_window))
205+
else:
206+
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
207+
eq_window_weights.append(window_stability)
204208
lr = np.mean(eq_window_weights)
205209
else:
206210
features = self.feature_reshape(features_vals)
@@ -212,7 +216,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
212216
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals[start_idx:end_idx])
213217
valuable_weights = estimator.coef_[:-1]
214218
eq_window_weights.append(valuable_weights)
215-
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_weights)])
219+
eq_cv = np.array([
220+
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
221+
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
222+
for _ in zip(*eq_window_weights)
223+
])
216224
lr = eq_cv.mean()
217225

218226
elif target_vals.ndim == 2:
@@ -233,7 +241,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
233241
target_window = target_vals[start_idx:end_idx, :].reshape(-1)
234242
else:
235243
target_window = target_vals[:, start_idx:end_idx].reshape(-1)
236-
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
244+
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
245+
window_stability = np.abs(np.std(target_window))
246+
else:
247+
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
248+
eq_window_weights.append(window_stability)
237249
lr += np.mean(eq_window_weights)
238250
else:
239251
features = self.feature_reshape(features_vals)
@@ -250,7 +262,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
250262
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals.reshape(*data_shape, -1)[:, start_idx:end_idx].reshape(-1))
251263
valuable_weights = estimator.coef_[:-1]
252264
eq_window_weights.append(valuable_weights)
253-
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_weights)])
265+
eq_cv = np.array([
266+
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
267+
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
268+
for _ in zip(*eq_window_weights)
269+
])
254270
lr += eq_cv.mean()
255271

256272
elif target_vals.ndim == 3:
@@ -269,7 +285,11 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
269285
target_window = target_vals[:, start_idx:end_idx, :].reshape(-1)
270286
else:
271287
target_window = target_vals[:, :, start_idx:end_idx].reshape(-1)
272-
eq_window_weights.append(np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2)))))
288+
if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10):
289+
window_stability = np.abs(np.std(target_window))
290+
else:
291+
window_stability = np.abs(np.std(target_window) / np.sqrt(np.mean(np.power(target_window, 2))))
292+
eq_window_weights.append(window_stability)
273293
lr += np.mean(eq_window_weights)
274294
else:
275295
features = self.feature_reshape(features_vals)
@@ -290,7 +310,12 @@ def apply(self, objective: Equation, arguments: dict, force_out_of_place: bool =
290310
estimator.fit(feature_window, target_window, sample_weight=self.g_fun_vals.reshape(*data_shape, -1)[:, :, start_idx:end_idx].reshape(-1))
291311
valuable_weights = estimator.coef_[:-1]
292312
eq_window_weights.append(valuable_weights)
293-
eq_cv = np.array([np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2)))) for _ in zip(*eq_window_weights)])
313+
eq_cv = np.array([
314+
np.abs(np.std(_)) if np.isclose(np.sqrt(np.mean(np.power(_, 2))), 0, atol=1e-10)
315+
else np.abs(np.std(_) / np.sqrt(np.mean(np.power(_, 2))))
316+
for _ in zip(*eq_window_weights)
317+
])
318+
294319
lr += eq_cv.mean()
295320

296321
objective.fitness_calculated = True

epde/operators/multiobjective/moeadd_specific.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -343,38 +343,33 @@ def best_obj_values(levels : ParetoLevels):
343343

344344
class OffspringUpdater(CompoundOperator):
345345
key = 'ParetoLevelUpdater'
346-
347-
def apply(self, objective : ParetoLevels, arguments : dict):
348-
self_args, subop_args = self.parse_suboperator_args(arguments = arguments)
346+
347+
def apply(self, objective: ParetoLevels, arguments: dict):
348+
self_args, subop_args = self.parse_suboperator_args(arguments=arguments)
349349

350350
while objective.unplaced_candidates:
351351
offspring = objective.unplaced_candidates.pop()
352-
attempt = 1; attempt_limit = self.params['attempt_limit']
352+
attempt = 1;
353+
attempt_limit = self.params['attempt_limit']
354+
temp_offspring = self.suboperators['chromosome_mutation'].apply(objective=offspring,
355+
arguments=subop_args['chromosome_mutation'])
353356
while True:
354-
temp_offspring = self.suboperators['chromosome_mutation'].apply(objective = offspring,
355-
arguments = subop_args['chromosome_mutation'])
356-
self.suboperators['right_part_selector'].apply(objective = temp_offspring,
357-
arguments = subop_args['right_part_selector'])
358-
self.suboperators['chromosome_fitness'].apply(objective = temp_offspring,
359-
arguments = subop_args['chromosome_fitness'])
360-
361-
if all([temp_offspring != solution for solution in objective.population]):
362-
self.suboperators['pareto_level_updater'].apply(objective = (temp_offspring, objective),
363-
arguments = subop_args['pareto_level_updater'])
357+
self.suboperators['right_part_selector'].apply(objective=temp_offspring,
358+
arguments=subop_args['right_part_selector'])
359+
self.suboperators['chromosome_fitness'].apply(objective=temp_offspring,
360+
arguments=subop_args['chromosome_fitness'])
361+
362+
if all([not np.allclose(temp_offspring.obj_fun, solution.obj_fun) for solution in objective.population]):
363+
self.suboperators['pareto_level_updater'].apply(objective=(temp_offspring, objective),
364+
arguments=subop_args['pareto_level_updater'])
364365
break
365366
elif attempt >= attempt_limit:
366-
# print(temp_offspring.text_form)
367-
# print('-----------------------')
368-
# for idx, individual in enumerate(objective.population):
369-
# print(f'Individual {idx}')
370-
# print(individual.text_form)
371-
# print('-----------------------')
372-
# raise Exception('Can not place individual into the population. Try decreasing population size or increasing token variety. ')
373-
print('The algorithm had issues with generating unique offsprings, allowed replication.')
374-
self.suboperators['pareto_level_updater'].apply(objective = (temp_offspring, objective),
375-
arguments = subop_args['pareto_level_updater'])
376-
377-
break
367+
# print('The algorithm had issues with generating unique offsprings.')
368+
temp_offspring.create()
369+
# temp_offspring.reset_state()
370+
attempt = 1
371+
self.suboperators['chromosome_mutation'].apply(objective=temp_offspring,
372+
arguments=subop_args['chromosome_mutation'])
378373
attempt += 1
379374
return objective
380375

0 commit comments

Comments
 (0)