From 5284d234eba2ca032bd64d85314eca820361ef8c Mon Sep 17 00:00:00 2001 From: LisIva Date: Sat, 18 May 2024 17:56:14 +0300 Subject: [PATCH] Make mmf a parameter (proportion of mixing of two distributions) --- epde/globals.py | 7 +++++-- epde/interface/interface.py | 9 +++++---- epde/operators/multiobjective/variation.py | 3 +-- epde/structure/main_structures.py | 8 +++----- symnet/initcoefficients.py | 2 +- symnet/pool_terms.py | 3 ++- symnet/preproc_output.py | 3 +-- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/epde/globals.py b/epde/globals.py index dc94298..c392f78 100644 --- a/epde/globals.py +++ b/epde/globals.py @@ -98,7 +98,10 @@ def init_verbose(plot_DE_solutions : bool = False, show_iter_idx : bool = True, show_iter_stats, show_warnings) -def init_symnet_pool(max_factors_in_term, families, u, derivs, shape, names, grids, max_deriv_order): +def init_symnet_pool(max_factors_in_term, families, u, derivs, shape, names, grids, max_deriv_order, mmf): global sympool - sympool = pool_terms.PoolTerms(max_factors_in_term, families) + sympool = pool_terms.PoolTerms(max_factors_in_term, families, mmf=mmf) sympool.set_initial_distr(u, derivs, shape, names, families=families, grids=grids, max_deriv_order=max_deriv_order) + # temporary_cf = [0.3, 1.5, 0.3, 0.3, 1.5, 0.3, 1.5, 0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,] + # for i, key in enumerate(sympool.pool_dict.keys()): + # sympool.pool_dict[key] = temporary_cf[i] diff --git a/epde/interface/interface.py b/epde/interface/interface.py index 31e83bd..b03e2c5 100644 --- a/epde/interface/interface.py +++ b/epde/interface/interface.py @@ -503,7 +503,7 @@ def set_preprocessor(self, preprocessor_pipeline: PreprocessingPipe = None, def create_pool(self, data: Union[np.ndarray, list, tuple], variable_names=['u',], derivs=None, max_deriv_order=1, additional_tokens=[], - data_fun_pow: int = 1, max_factors_in_term=1): + data_fun_pow: int = 1, max_factors_in_term=1, mmf=2.4): self.pool_params = {'variable_names' : variable_names, 'max_deriv_order' : max_deriv_order, 'additional_tokens' : [family.token_family.ftype for family in additional_tokens]} assert (isinstance(derivs, list) and isinstance(derivs[0], np.ndarray)) or derivs is None @@ -556,7 +556,7 @@ def create_pool(self, data: Union[np.ndarray, list, tuple], variable_names=['u', grids = [self.cache[0].memory_default.get('0'), self.cache[0].memory_default.get('1')] global_var.init_symnet_pool(max_factors_in_term, self.pool.families, entry.data_tensor, entry.derivatives, entry.data_tensor.shape, entry.names, - grids=grids, max_deriv_order=max_deriv_order) + grids=grids, max_deriv_order=max_deriv_order, mmf=mmf) print(f'The cardinality of defined token pool is {self.pool.families_cardinality()}') print(f'Among them, the pool contains {self.pool.families_cardinality(meaningful_only=True)}') @@ -577,7 +577,7 @@ def saved_derivaties(self): def fit(self, data: Union[np.ndarray, list, tuple], equation_terms_max_number=6, equation_factors_max_number=1, variable_names=['u',], eq_sparsity_interval=(1e-4, 2.5), - derivs=None, max_deriv_order=1, additional_tokens=[], data_fun_pow: int = 1): + derivs=None, max_deriv_order=1, additional_tokens=[], data_fun_pow: int = 1, mmf: int = 2.4): """ Fit epde search algorithm to obtain differential equations, describing passed data. @@ -634,7 +634,8 @@ def fit(self, data: Union[np.ndarray, list, tuple], equation_terms_max_number=6, derivs=derivs, max_deriv_order=max_deriv_order, additional_tokens=additional_tokens, data_fun_pow=data_fun_pow, - max_factors_in_term=equation_factors_max_number) + max_factors_in_term=equation_factors_max_number, + mmf=mmf) self.optimizer_init_params['population_instruct'] = {"pool": self.pool, "terms_number": equation_terms_max_number, "max_factors_in_term": equation_factors_max_number, diff --git a/epde/operators/multiobjective/variation.py b/epde/operators/multiobjective/variation.py index 02220c8..8d91d71 100644 --- a/epde/operators/multiobjective/variation.py +++ b/epde/operators/multiobjective/variation.py @@ -202,11 +202,10 @@ def get_equation_cross_distr(self, equation, start_idx): for i in range(start_idx, len(equation.structure)): sym_term = to_symbolic(equation.structure[i]) importance_coeffs[sym_term] = global_var.sympool.pool_dict.get(sym_term) - cross_distr = get_cross_distr(importance_coeffs, start_idx, len(equation.structure)) + cross_distr = get_cross_distr(importance_coeffs, start_idx, len(equation.structure), global_var.sympool.mmf) return cross_distr - class EquationExchangeCrossover(CompoundOperator): key = 'EquationExchangeCrossover' diff --git a/epde/structure/main_structures.py b/epde/structure/main_structures.py index 7e90e4c..7cdefd5 100644 --- a/epde/structure/main_structures.py +++ b/epde/structure/main_structures.py @@ -335,11 +335,11 @@ def set_mutation_probability(self): for tsym in allow_tsym: allow_csym.append(global_var.sympool.pool_dict.get(tsym)) - eq_prob_ls = self._set_probabilities(allow_csym, draw_prob=False) + eq_prob_ls = self._set_probabilities(allow_csym, draw_prob=False, mmf=global_var.sympool.mmf) return allow_tsym, eq_prob_ls @staticmethod - def _set_probabilities(csym_pool_ls, mmf=2.4, draw_prob=False): + def _set_probabilities(csym_pool_ls, mmf=1.4, draw_prob=False): def draw_probabilities(final_probabilities, mmf, smoothing_factor, title): final_probabilities = final_probabilities.copy() final_probabilities.sort() @@ -348,15 +348,13 @@ def draw_probabilities(final_probabilities, mmf, smoothing_factor, title): fig, ax = plt.subplots(figsize=(16, 8)) ax.set_ylim(0, max(final_probabilities) + 0.01) sns.barplot(x=np.arange(len(final_probabilities)), y=final_probabilities, orient="v", ax=ax) - # plt.title(f"Smoothing factor: {smoothing_factor:.3f}, mmf: {mmf:.1f}") plt.grid() # plt.show() plt.yticks(fontsize=50) plt.savefig(f'individual_distr_{title}.png', transparent=True) csym_arr = np.fabs(np.array(csym_pool_ls)) - # draw_probabilities(csym_arr, mmf, smoothing_factor, "before") - if np.max(csym_arr) / np.min(csym_arr) > 2.6: + if np.max(csym_arr) / np.min(csym_arr) > mmf: min_max_coeff = mmf * np.min(csym_arr) - np.max(csym_arr) smoothing_factor = min_max_coeff / (min_max_coeff - (mmf - 1) * np.average(csym_arr)) uniform_csym = np.array([np.sum(csym_arr) / len(csym_arr)] * len(csym_arr)) diff --git a/symnet/initcoefficients.py b/symnet/initcoefficients.py index 1b49790..9ce6434 100644 --- a/symnet/initcoefficients.py +++ b/symnet/initcoefficients.py @@ -136,7 +136,7 @@ def get_csym_tsym(u, derivs, shape, input_names, pool_names, additional_tokens=N """ # TODO: SymNet имеет 4 todo (+, pool_terms, preproc_input) - # TODO: если в левой части e.g. d^2u/dx2^2, то как получить в правой слагаемое d^2u/dx2^2 * u? + # TODO: если в левой части e.g. d^2u/dx1^2, то как получить в правой слагаемое d^2u/dx1^2 * u? left_pool = get_left_pool(max_deriv_order) model, left_side_name = select_model(input_names, left_pool, u, derivs, shape, additional_tokens) diff --git a/symnet/pool_terms.py b/symnet/pool_terms.py index a9ab03d..2e6f33a 100644 --- a/symnet/pool_terms.py +++ b/symnet/pool_terms.py @@ -6,11 +6,12 @@ class PoolTerms: - def __init__(self, max_factors_in_term, families): + def __init__(self, max_factors_in_term, families, mmf): self.pool_sym_dict = None self.pool_sym_ls = None self.pool_dict = None + self.mmf = mmf token_ls = [] for family in families: diff --git a/symnet/preproc_output.py b/symnet/preproc_output.py index 8bf66cb..83afcdd 100644 --- a/symnet/preproc_output.py +++ b/symnet/preproc_output.py @@ -57,8 +57,7 @@ def to_symbolic(term): return Symbol(str(term.cache_label[0])) -def get_cross_distr(custom_cross_prob, start_idx, end_idx_exclude): - mmf = 2.4 +def get_cross_distr(custom_cross_prob, start_idx, end_idx_exclude, mmf): values = list(custom_cross_prob.values()) csym_arr = np.fabs(np.array(values))