From 7ea3090ff2c126d0e62041669f4966da0e5ae8a1 Mon Sep 17 00:00:00 2001 From: LisIva Date: Wed, 22 May 2024 12:31:08 +0300 Subject: [PATCH] Make the scripts more uniform and correct the name of output files --- .gitignore | 2 + .../experiment_burgers.py | 167 -------------- .../experiment_burgers_sindy.py | 176 --------------- .../experiment_kdv.py | 213 ------------------ .../experiment_kdv_sindy.py | 170 -------------- .../experiment_wave.py | 171 -------------- Experiments on noised data/noised_burgers.py | 26 ++- .../noised_burgers_pysindy.py | 19 +- .../noised_burgers_sindy.py | 21 +- Experiments on noised data/noised_kdv.py | 51 +++-- .../noised_kdv_pysindy.py | 20 +- .../noised_kdv_sindy.py | 52 +++-- Experiments on noised data/noised_wave.py | 46 ++-- 13 files changed, 148 insertions(+), 986 deletions(-) delete mode 100644 Experiments on no noise data/experiment_burgers.py delete mode 100644 Experiments on no noise data/experiment_burgers_sindy.py delete mode 100644 Experiments on no noise data/experiment_kdv.py delete mode 100644 Experiments on no noise data/experiment_kdv_sindy.py delete mode 100644 Experiments on no noise data/experiment_wave.py diff --git a/.gitignore b/.gitignore index 739385d..176c2c0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ classic_alg_exp/ *.png *.csv *.docx +*.txt +*.pickle # Distribution / packaging .Python pyvenv.cfg diff --git a/Experiments on no noise data/experiment_burgers.py b/Experiments on no noise data/experiment_burgers.py deleted file mode 100644 index 84870ca..0000000 --- a/Experiments on no noise data/experiment_burgers.py +++ /dev/null @@ -1,167 +0,0 @@ -import time -import numpy as np -import pandas as pd -import epde.interface.interface as epde_alg -from matplotlib import rcParams -rcParams.update({'figure.autolayout': True}) -import traceback -import logging -import os -from pathlib import Path - - -def find_coeff_diff(res, coefficients: dict): - differences = [] - - for pareto_front in res: - for soeq in pareto_front: - if soeq.obj_fun[0] < 10.: - eq_text = soeq.vals.chromosome['u'].value.text_form - terms_dict = out_formatting(eq_text) - diff = coefficients_difference(terms_dict, coefficients) - if diff != -1: - differences.append(diff) - - return differences - - -def coefficients_difference(terms_dict, coefficients): - mae = 0. - eq_found = 0 - for term_hash in terms_dict.keys(): - mae += abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) - if coefficients.get(term_hash) == -1.0 and abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) < 0.2: - eq_found += 1 - - mae /= len(terms_dict) - if eq_found == 2: - return mae - else: - return -1 - - -def out_formatting(string): - string = string.replace("u{power: 1.0}", "u") - string = string.replace("d^2u/dx2^2{power: 1.0}", "d^2u/dx2^2") - string = string.replace("d^2u/dx1^2{power: 1.0}", "d^2u/dx1^2") - string = string.replace("du/dx1{power: 1.0}", "du/dx1") - string = string.replace("du/dx2{power: 1.0}", "du/dx2") - string = string.replace(" ", "") - - ls_equal = string.split('=') - ls_left = ls_equal[0].split('+') - ls_terms = [] - for term in ls_left: - ls_term = term.split('*') - ls_terms.append(ls_term) - ls_right = ls_equal[1].split('*') - - terms_dict = {} - for term in ls_terms: - if len(term) == 1: - terms_dict[1] = float(term[0]) - else: - coeff = float(term.pop(0)) - terms_dict[hash_term(term)] = coeff - - terms_dict[hash_term(ls_right)] = -1. - return terms_dict - - -def hash_term(term): - total_term = 0 - for token in term: - total_token = 1 - if type(token) == tuple: - token = token[0] - for char in token: - total_token += ord(char) - total_term += total_token * total_token - return total_term - - -if __name__ == '__main__': - - path = "data_burg" - path_full = os.path.join(Path().absolute().parent, path, "burgers_sln_100.csv") - df = pd.read_csv(path_full, header=None) - - u = df.values - u = np.transpose(u) - x = np.linspace(-1000, 0, 101) - t = np.linspace(0, 1, 101) - - boundary = 10 - dimensionality = u.ndim - grids = np.meshgrid(t, x, indexing='ij') - - ''' Parameters of the experiment ''' - write_csv = True - print_results = True - max_iter_number = 50 - title = 'dfs0' - - terms = [('du/dx1', ), ('du/dx2', 'u'), ('u',), ('du/dx2',), ('u', 'du/dx1'), ('du/dx1', 'du/dx2'),] - hashed_ls = [hash_term(term) for term in terms] - coefficients = dict(zip(hashed_ls, [-1., -1., 0., 0., 0., 0.])) - coefficients[1] = 0. - - time_ls = [] - differences_ls = [] - mean_diff_ls = [] - num_found_eq = [] - differences_ls_none = [] - i = 0 - population_error = 0 - while i < max_iter_number: - epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, - dimensionality=dimensionality, coordinate_tensors=grids) - - epde_search_obj.set_moeadd_params(population_size=5, training_epochs=5) - start = time.time() - try: - epde_search_obj.fit(data=u, max_deriv_order=(1, 1), - equation_terms_max_number=3, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-4)) - except Exception as e: - logging.error(traceback.format_exc()) - population_error += 1 - continue - end = time.time() - epde_search_obj.equation_search_results(only_print=True, num=2) - time1 = end-start - - res = epde_search_obj.equation_search_results(only_print=False, num=2) - difference_ls = find_coeff_diff(res, coefficients) - - if len(difference_ls) != 0: - differences_ls.append(min(difference_ls)) - differences_ls_none.append(min(difference_ls)) - mean_diff_ls += difference_ls - else: - differences_ls_none.append(None) - - num_found_eq.append(len(difference_ls)) - print('Overall time is:', time1) - print(f'Iteration processed: {i+1}/{max_iter_number}\n') - i += 1 - time_ls.append(time1) - - if write_csv: - arr = np.array([differences_ls_none, time_ls, num_found_eq]) - arr = arr.T - df = pd.DataFrame(data=arr, columns=['MAE', 'time', 'number_found_eq']) - df.to_csv(os.path.join(Path().absolute().parent, "data_burg", f"{title}.csv")) - - if print_results: - print('\nTime for every run:') - for item in time_ls: - print(item) - - print() - print(f'\nAverage time, s: {sum(time_ls) / len(time_ls):.2f}') - print(f'Average MAE per eq: {sum(mean_diff_ls) / len(mean_diff_ls):.6f}') - print(f'Average minimum MAE per run: {sum(differences_ls) / max_iter_number:.6f}') - print(f'Average # of found eq per run: {sum(num_found_eq) / max_iter_number:.2f}') - print(f"Runs where eq was not found: {num_found_eq.count(0)}") - print(f"Num of population error occurrence: {population_error}") diff --git a/Experiments on no noise data/experiment_burgers_sindy.py b/Experiments on no noise data/experiment_burgers_sindy.py deleted file mode 100644 index 396ee6d..0000000 --- a/Experiments on no noise data/experiment_burgers_sindy.py +++ /dev/null @@ -1,176 +0,0 @@ -import time -import numpy as np -import pandas as pd -import epde.interface.interface as epde_alg -import seaborn as sns -import matplotlib.pyplot as plt -from scipy.io import loadmat -from matplotlib import rcParams -rcParams.update({'figure.autolayout': True}) -import traceback -import logging -import os -from pathlib import Path - - -def find_coeff_diff(res, coefficients: dict): - differences = [] - - for pareto_front in res: - for soeq in pareto_front: - if soeq.obj_fun[0] < 10: - eq_text = soeq.vals.chromosome['u'].value.text_form - terms_dict = out_formatting(eq_text) - diff = coefficients_difference(terms_dict, coefficients) - if diff != -1: - differences.append(diff) - - return differences - - -def coefficients_difference(terms_dict, coefficients): - mae = 0. - eq_found = 0 - for term_hash in terms_dict.keys(): - mae += abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) - if coefficients.get(term_hash) != 0.0 and abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) < 0.1: - eq_found += 1 - - mae /= len(terms_dict) - if eq_found == 3: - return mae - else: - return -1 - - -def out_formatting(string): - string = string.replace("u{power: 1.0}", "u") - string = string.replace("d^2u/dx2^2{power: 1.0}", "d^2u/dx2^2") - string = string.replace("d^2u/dx1^2{power: 1.0}", "d^2u/dx1^2") - string = string.replace("du/dx1{power: 1.0}", "du/dx1") - string = string.replace("du/dx2{power: 1.0}", "du/dx2") - string = string.replace(" ", "") - - ls_equal = string.split('=') - ls_left = ls_equal[0].split('+') - ls_terms = [] - for term in ls_left: - ls_term = term.split('*') - ls_terms.append(ls_term) - ls_right = ls_equal[1].split('*') - - terms_dict = {} - for term in ls_terms: - if len(term) == 1: - terms_dict[1] = float(term[0]) - else: - coeff = float(term.pop(0)) - terms_dict[hash_term(term)] = coeff - - terms_dict[hash_term(ls_right)] = -1. - return terms_dict - - -def hash_term(term): - total_term = 0 - for token in term: - total_token = 1 - if type(token) == tuple: - token = token[0] - for char in token: - total_token += ord(char) - total_term += total_token * total_token - return total_term - - -if __name__ == '__main__': - path_full = os.path.join(Path().absolute().parent, "data_burg", "burgers.mat") - burg = loadmat(path_full) - t = np.ravel(burg['t']) - x = np.ravel(burg['x']) - u = np.real(burg['usol']) - u = np.transpose(u) - - boundary = 10 - dimensionality = u.ndim - grids = np.meshgrid(t, x, indexing='ij') - - ''' Parameters of the experiment ''' - write_csv = False - print_results = True - max_iter_number = 1 - title = 'dfs0' - - terms = [('u',), ('du/dx1',), ('du/dx2',), ('d^2u/dx2^2',), ('u', 'du/dx1'), ('u', 'du/dx2'), ('u', 'd^2u/dx2^2'), - ('du/dx1', 'du/dx2'), ('du/dx1', 'd^2u/dx2^2'), ('du/dx2', 'd^2u/dx2^2')] - hashed_ls = [hash_term(term) for term in terms] - coefficients = dict(zip(hashed_ls, [0., -1., 0., 0.1, 0., -1., 0., 0., 0., 0.])) - coefficients[1] = 0. - - time_ls = [] - differences_ls = [] - mean_diff_ls = [] - num_found_eq = [] - differences_ls_none = [] - i = 0 - population_error = 0 - while i < max_iter_number: - epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, - dimensionality=dimensionality, coordinate_tensors=grids) - - epde_search_obj.set_moeadd_params(population_size=8, training_epochs=7) - start = time.time() - try: - epde_search_obj.fit(data=u, max_deriv_order=(1, 2), - equation_terms_max_number=3, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-1)) - except Exception as e: - logging.error(traceback.format_exc()) - population_error += 1 - continue - end = time.time() - epde_search_obj.equation_search_results(only_print=True, num=4) - time1 = end-start - - # keys = list(epde_search_obj.cache[1].memory_default.keys()) - # for i in range(4): - # val = epde_search_obj.cache[1].memory_default.get(keys[i]) - # name = keys[i][0].replace("/", '_') - # path = os.path.join(Path().absolute().parent, "data_burg", name) - # np.save(path, val) - # print() - - res = epde_search_obj.equation_search_results(only_print=False, num=4) - difference_ls = find_coeff_diff(res, coefficients) - - if len(difference_ls) != 0: - differences_ls.append(min(difference_ls)) - differences_ls_none.append(min(difference_ls)) - mean_diff_ls += difference_ls - else: - differences_ls_none.append(None) - - num_found_eq.append(len(difference_ls)) - print('Overall time is:', time1) - print(f'Iteration processed: {i+1}/{max_iter_number}\n') - i += 1 - time_ls.append(time1) - - if write_csv: - arr = np.array([differences_ls_none, time_ls, num_found_eq]) - arr = arr.T - df = pd.DataFrame(data=arr, columns=['MAE', 'time', 'number_found_eq']) - df.to_csv(os.path.join(Path().absolute().parent, "data_pysindy_burg", f"{title}.csv")) - - if print_results: - print('\nTime for every run, s:') - for item in time_ls: - print(f'{item: .4f}') - - print() - print(f'\nAverage time, s: {sum(time_ls) / len(time_ls):.2f}') - print(f'Average MAE per eq: {sum(mean_diff_ls) / len(mean_diff_ls):.6f}') - print(f'Average minimum MAE per run: {sum(differences_ls) / max_iter_number:.6f}') - print(f'Average # of found eq per run: {sum(num_found_eq) / max_iter_number:.2f}') - print(f"Runs where eq was not found: {num_found_eq.count(0)}") - print(f"Num of population error occurrence: {population_error}") diff --git a/Experiments on no noise data/experiment_kdv.py b/Experiments on no noise data/experiment_kdv.py deleted file mode 100644 index 575c7b0..0000000 --- a/Experiments on no noise data/experiment_kdv.py +++ /dev/null @@ -1,213 +0,0 @@ -import time -import numpy as np -import pandas as pd -import epde.interface.interface as epde_alg -from epde.evaluators import CustomEvaluator -from epde.interface.prepared_tokens import CustomTokens -from kdv_init_distrib import coefficients1, coefficients2 - -import traceback -import logging -import os -from pathlib import Path - - -def find_coeff_diff(res): - differences = [] - - for pareto_front in res: - for soeq in pareto_front: - if soeq.obj_fun[0] < 0.02: - eq_text = soeq.vals.chromosome['u'].value.text_form - terms_dict = out_formatting(eq_text) - diff = coefficients_difference(terms_dict) - if diff != -1: - differences.append(diff) - - return differences - - -def coefficients_difference(terms_dict): - mae1 = 0. - mae2 = 0. - eq_found = 0 - for term_hash in terms_dict.keys(): - mae1 += abs(terms_dict.get(term_hash) - coefficients1.get(term_hash)) - mae2 += abs(terms_dict.get(term_hash) - coefficients2.get(term_hash)) - if coefficients1.get(term_hash) != 0.0 and (abs(terms_dict.get(term_hash) - coefficients1.get(term_hash)) < 0.1\ - or abs(terms_dict.get(term_hash) - coefficients2.get(term_hash)) < 0.1): - eq_found += 1 - - mae1 /= len(terms_dict) - mae2 /= len(terms_dict) - mae = min(mae1, mae2) - - if eq_found == 4: - return mae - else: - return -1 - - -def out_formatting(string): - string = string.replace("u{power: 1.0}", "u") - string = string.replace("d^2u/dx2^2{power: 1.0}", "d^2u/dx2^2") - string = string.replace("d^2u/dx1^2{power: 1.0}", "d^2u/dx1^2") - string = string.replace("du/dx1{power: 1.0}", "du/dx1") - string = string.replace("du/dx2{power: 1.0}", "du/dx2") - string = string.replace("cos(t)sin(x){power: 1.0}", "cos(t)sin(x)") - string = string.replace("d^3u/dx2^3{power: 1.0}", "d^3u/dx2^3") - string = string.replace(" ", "") - - ls_equal = string.split('=') - ls_left = ls_equal[0].split('+') - ls_terms = [] - for term in ls_left: - ls_term = term.split('*') - ls_terms.append(ls_term) - ls_right = ls_equal[1].split('*') - - terms_dict = {} - for term in ls_terms: - if len(term) == 1: - terms_dict[1] = float(term[0]) - else: - coeff = float(term.pop(0)) - terms_dict[hash_term(term)] = coeff - - terms_dict[hash_term(ls_right)] = -1. - return terms_dict - - -def hash_term(term): - total_term = 0 - for token in term: - total_token = 1 - if type(token) == tuple: - token = token[0] - for char in token: - total_token += ord(char) - total_term += total_token * total_token - return total_term - - -if __name__ == '__main__': - path_full = os.path.join(Path().absolute().parent, "data_kdv", "KdV_sln_100.csv") - df = pd.read_csv(path_full, header=None) - - os.path.join(Path().absolute().parent, "data_kdv", "d_x_100.csv") - dddx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "ddd_x_100.csv"), header=None) - ddx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "dd_x_100.csv"), header=None) - dx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "d_x_100.csv"), header=None) - dt = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "d_t_100.csv"), header=None) - - u = df.values - u = np.transpose(u) - - ddd_x = dddx.values - ddd_x = np.transpose(ddd_x) - dd_x = ddx.values - dd_x = np.transpose(dd_x) - d_x = dx.values - d_x = np.transpose(d_x) - d_t = dt.values - d_t = np.transpose(d_t) - - derivs = np.zeros(shape=(u.shape[0],u.shape[1],4)) - derivs[:, :, 0] = d_t - derivs[:, :, 1] = d_x - derivs[:, :, 2] = dd_x - derivs[:, :, 3] = ddd_x - - t = np.linspace(0, 1, u.shape[0]) - x = np.linspace(0, 1, u.shape[1]) - - boundary = 0 - dimensionality = u.ndim - grids = np.meshgrid(t, x, indexing='ij') - - ''' Parameters of the experiment ''' - write_csv = True - print_results = True - max_iter_number = 50 - title = 'dfs0' - - time_ls = [] - differences_ls = [] - differences_ls_none = [] - num_found_eq = [] - mean_diff_ls = [] - i = 0 - population_error = 0 - while i < max_iter_number: - epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, - dimensionality=dimensionality, coordinate_tensors=grids) - - custom_trigonometric_eval_fun = { - 'cos(t)sin(x)': lambda *grids, **kwargs: (np.cos(grids[0]) * np.sin(grids[1])) ** kwargs['power']} - custom_trig_evaluator = CustomEvaluator(custom_trigonometric_eval_fun, - eval_fun_params_labels=['power']) - trig_params_ranges = {'power': (1, 1)} - # something = custom_trigonometric_eval_fun.get('cos(t)sin(x)')(*grids, - # **{'power': trig_params_ranges.get('power')[0]}) - trig_params_equal_ranges = {} - - custom_trig_tokens = CustomTokens(token_type='trigonometric', - token_labels=['cos(t)sin(x)'], - evaluator=custom_trig_evaluator, - params_ranges=trig_params_ranges, - params_equality_ranges=trig_params_equal_ranges, - meaningful=True, unique_token_type=False) - - epde_search_obj.set_moeadd_params(population_size=8, training_epochs=90) - start = time.time() - try: - epde_search_obj.fit(data=u, max_deriv_order=(1, 3), - equation_terms_max_number=4, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-06), derivs=[derivs], - additional_tokens=[custom_trig_tokens, ]) - except Exception as e: - logging.error(traceback.format_exc()) - population_error += 1 - continue - end = time.time() - epde_search_obj.equation_search_results(only_print=True, num=2) - time1 = end-start - - res = epde_search_obj.equation_search_results(only_print=False, num=2) - - difference_ls = find_coeff_diff(res) - if len(difference_ls) != 0: - differences_ls.append(min(difference_ls)) - differences_ls_none.append(min(difference_ls)) - mean_diff_ls += difference_ls - else: - differences_ls_none.append(None) - - num_found_eq.append(len(difference_ls)) - - print('Overall time is:', time1) - print(f'Iteration processed: {i+1}/{max_iter_number}\n') - time_ls.append(time1) - i += 1 - - if write_csv: - arr = np.array([differences_ls_none, time_ls, num_found_eq]) - arr = arr.T - df = pd.DataFrame(data=arr, columns=['MAE', 'time', 'number_found_eq']) - df.to_csv(os.path.join(Path().absolute().parent, "data_kdv", f"{title}.csv")) - - if print_results: - print('\nTime for every run:') - for item in time_ls: - print(item) - # print('\nMAE and # of found equations in every run:') - # for item1, item2 in zip(differences_ls, num_found_eq): - # print("diff:", item1, "num eq:", item2) - - print() - print(f'\nAverage time, s: {sum(time_ls) / len(time_ls):.2f}') - print(f'Average MAE per eq: {sum(mean_diff_ls) / len(mean_diff_ls):.6f}') - print(f'Average minimum MAE per run: {sum(differences_ls) / max_iter_number:.6f}') - print(f'Average # of found eq per run: {sum(num_found_eq) / max_iter_number:.2f}') - print(f"Runs where eq was not found: {num_found_eq.count(0)}") - print(f"Num of population error occurrence: {population_error}") diff --git a/Experiments on no noise data/experiment_kdv_sindy.py b/Experiments on no noise data/experiment_kdv_sindy.py deleted file mode 100644 index 7762cfb..0000000 --- a/Experiments on no noise data/experiment_kdv_sindy.py +++ /dev/null @@ -1,170 +0,0 @@ -import time -import numpy as np -import pandas as pd -import epde.interface.interface as epde_alg -from kdv_init_distrib_sindy import coefficients1, coefficients2 -from scipy.io import loadmat -import traceback -import logging -import os -from pathlib import Path - - -def find_coeff_diff(res): - differences = [] - - for pareto_front in res: - for soeq in pareto_front: - if soeq.obj_fun[0] < 10: - eq_text = soeq.vals.chromosome['u'].value.text_form - terms_dict = out_formatting(eq_text) - diff = coefficients_difference(terms_dict) - if diff != -1: - differences.append(diff) - - return differences - - -def coefficients_difference(terms_dict): - mae1 = 0. - mae2 = 0. - eq_found = 0 - for term_hash in terms_dict.keys(): - mae1 += abs(terms_dict.get(term_hash) - coefficients1.get(term_hash)) - mae2 += abs(terms_dict.get(term_hash) - coefficients2.get(term_hash)) - if coefficients1.get(term_hash) != 0.0 and (abs(terms_dict.get(term_hash) - coefficients1.get(term_hash)) < 0.3\ - or abs(terms_dict.get(term_hash) - coefficients2.get(term_hash)) < 0.3): - eq_found += 1 - - values = list(terms_dict.values()) - not_zero_ls = [value for value in values if value != 0.0] - mae1 /= len(not_zero_ls) - mae2 /= len(not_zero_ls) - mae = min(mae1, mae2) - - if eq_found == 3: - return mae - else: - return -1 - - -def out_formatting(string): - string = string.replace("u{power: 1.0}", "u") - string = string.replace("d^2u/dx2^2{power: 1.0}", "d^2u/dx2^2") - string = string.replace("d^2u/dx1^2{power: 1.0}", "d^2u/dx1^2") - string = string.replace("du/dx1{power: 1.0}", "du/dx1") - string = string.replace("du/dx2{power: 1.0}", "du/dx2") - string = string.replace("cos(t)sin(x){power: 1.0}", "cos(t)sin(x)") - string = string.replace("d^3u/dx2^3{power: 1.0}", "d^3u/dx2^3") - string = string.replace(" ", "") - - ls_equal = string.split('=') - ls_left = ls_equal[0].split('+') - ls_terms = [] - for term in ls_left: - ls_term = term.split('*') - ls_terms.append(ls_term) - ls_right = ls_equal[1].split('*') - - terms_dict = {} - for term in ls_terms: - if len(term) == 1: - terms_dict[1] = float(term[0]) - else: - coeff = float(term.pop(0)) - terms_dict[hash_term(term)] = coeff - - terms_dict[hash_term(ls_right)] = -1. - return terms_dict - - -def hash_term(term): - total_term = 0 - for token in term: - total_token = 1 - if type(token) == tuple: - token = token[0] - for char in token: - total_token += ord(char) - total_term += total_token * total_token - return total_term - - -if __name__ == '__main__': - path_full = os.path.join(Path().absolute().parent, "data_kdv", "kdv.mat") - kdV = loadmat(path_full) - t = np.ravel(kdV['t']) - x = np.ravel(kdV['x']) - u = np.real(kdV['usol']) - u = np.transpose(u) - - boundary = 0 - dimensionality = u.ndim - grids = np.meshgrid(t, x, indexing='ij') - - ''' Parameters of the experiment ''' - write_csv = True - print_results = True - max_iter_number = 50 - title = 'dfs0' - - time_ls = [] - differences_ls = [] - mean_diff_ls = [] - num_found_eq = [] - differences_ls_none =[] - i = 0 - population_error = 0 - while i < max_iter_number: - epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, - dimensionality=dimensionality, coordinate_tensors=grids) - - epde_search_obj.set_moeadd_params(population_size=8, training_epochs=90) - start = time.time() - - try: - epde_search_obj.fit(data=u, max_deriv_order=(1, 3), - equation_terms_max_number=4, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-06)) - except Exception as e: - logging.error(traceback.format_exc()) - population_error += 1 - continue - end = time.time() - epde_search_obj.equation_search_results(only_print=True, num=4) - time1 = end-start - - res = epde_search_obj.equation_search_results(only_print=False, num=4) - - difference_ls = find_coeff_diff(res) - if len(difference_ls) != 0: - differences_ls.append(min(difference_ls)) - differences_ls_none.append(min(difference_ls)) - mean_diff_ls += difference_ls - else: - differences_ls_none.append(None) - - num_found_eq.append(len(difference_ls)) - print('Overall time is:', time1) - print(f'Iteration processed: {i+1}/{max_iter_number}\n') - i += 1 - time_ls.append(time1) - - if write_csv: - arr = np.array([differences_ls_none, time_ls, num_found_eq]) - arr = arr.T - df = pd.DataFrame(data=arr, columns=['MAE', 'time', 'number_found_eq']) - df.to_csv(os.path.join(Path().absolute().parent, "data_pysindy_kdv", f"{title}.csv")) - - if print_results: - print('\nTime for every run, s:') - for item in time_ls: - print(f'{item: .4f}') - - print() - print(f'\nAverage time, s: {sum(time_ls) / len(time_ls):.2f}') - print(f'Average MAE per eq: {sum(mean_diff_ls) / len(mean_diff_ls):.6f}') - print(f'Average minimum MAE per run: {sum(differences_ls) / max_iter_number:.6f}') - print(f'Average # of found eq per run: {sum(num_found_eq) / max_iter_number:.2f}') - print(f"Runs where eq was not found: {num_found_eq.count(0)}") - print(f"Num of population error occurrence: {population_error}") diff --git a/Experiments on no noise data/experiment_wave.py b/Experiments on no noise data/experiment_wave.py deleted file mode 100644 index cc08801..0000000 --- a/Experiments on no noise data/experiment_wave.py +++ /dev/null @@ -1,171 +0,0 @@ -import time -import numpy as np -import pandas as pd -import epde.interface.interface as epde_alg -import seaborn as sns -import matplotlib.pyplot as plt -from matplotlib import rcParams -rcParams.update({'figure.autolayout': True}) -import traceback -import logging -import os -from pathlib import Path - - -def find_coeff_diff(res, coefficients: dict): - differences = [] - - for pareto_front in res: - for soeq in pareto_front: - if soeq.obj_fun[0] < 10.: - eq_text = soeq.vals.chromosome['u'].value.text_form - terms_dict = out_formatting(eq_text) - diff = coefficients_difference(terms_dict, coefficients) - if diff != -1: - differences.append(diff) - - return differences - - -def coefficients_difference(terms_dict, coefficients): - mae = 0. - eq_found = 0 - for term_hash in terms_dict.keys(): - mae += abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) - if coefficients.get(term_hash) != 0.0 and abs(terms_dict.get(term_hash) - coefficients.get(term_hash)) < 0.2: - eq_found += 1 - - mae /= len(terms_dict) - if eq_found == 2: - return mae - else: - return -1 - - -def out_formatting(string): - string = string.replace("u{power: 1.0}", "u") - string = string.replace("d^2u/dx2^2{power: 1.0}", "d^2u/dx2^2") - string = string.replace("d^2u/dx1^2{power: 1.0}", "d^2u/dx1^2") - string = string.replace("du/dx1{power: 1.0}", "du/dx1") - string = string.replace("du/dx2{power: 1.0}", "du/dx2") - string = string.replace(" ", "") - - ls_equal = string.split('=') - ls_left = ls_equal[0].split('+') - ls_terms = [] - for term in ls_left: - ls_term = term.split('*') - ls_terms.append(ls_term) - ls_right = ls_equal[1].split('*') - - terms_dict = {} - for term in ls_terms: - if len(term) == 1: - terms_dict[1] = float(term[0]) - else: - coeff = float(term.pop(0)) - terms_dict[hash_term(term)] = coeff - - terms_dict[hash_term(ls_right)] = -1. - return terms_dict - - -def hash_term(term): - total_term = 0 - for token in term: - total_token = 1 - if type(token) == tuple: - token = token[0] - for char in token: - total_token += ord(char) - total_term += total_token * total_token - return total_term - - -if __name__ == '__main__': - - path_full = os.path.join(Path().absolute().parent, "data_wave", "wave_sln_100.csv") - df = pd.read_csv(path_full, header=None) - u = df.values - u = np.transpose(u) - x = np.linspace(0, 1, 101) - t = np.linspace(0, 1, 101) - - boundary = 10 - dimensionality = u.ndim - grids = np.meshgrid(t, x, indexing='ij') - - ''' Parameters of the experiment ''' - write_csv = True - print_results = True - max_iter_number = 50 - title = 'dfs0' - '''''' - - terms = [('u',), ('du/dx1',), ('d^2u/dx1^2',), ('du/dx2',), ('d^2u/dx2^2',)] - hashed_ls = [hash_term(term) for term in terms] - coefficients = dict(zip(hashed_ls, [0., 0., -1., 0., 0.04])) - coefficients[1] = 0. - - time_ls = [] - differences_ls = [] - mean_diff_ls = [] - num_found_eq = [] - differences_ls_none = [] - i = 0 - population_error = 0 - while i < max_iter_number: - epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, - dimensionality=dimensionality, coordinate_tensors=grids, - prune_domain=False) - - epde_search_obj.set_moeadd_params(population_size=5, training_epochs=5) - start = time.time() - - # equation_factors_max_number = 1!!!!!!!!!!! - try: - epde_search_obj.fit(data=u, max_deriv_order=(2, 2), - equation_terms_max_number=3, equation_factors_max_number=1, - eq_sparsity_interval=(1e-08, 5)) - except Exception as e: - logging.error(traceback.format_exc()) - population_error += 1 - continue - end = time.time() - epde_search_obj.equation_search_results(only_print=True, num=2) - time1 = end-start - - res = epde_search_obj.equation_search_results(only_print=False, num=2) - difference_ls = find_coeff_diff(res, coefficients) - - if len(difference_ls) != 0: - differences_ls.append(min(difference_ls)) - differences_ls_none.append(min(difference_ls)) - mean_diff_ls += difference_ls - else: - differences_ls_none.append(None) - - num_found_eq.append(len(difference_ls)) - print('Overall time is:', time1) - print(f'Iteration processed: {i+1}/{max_iter_number}\n') - i += 1 - time_ls.append(time1) - - if write_csv: - arr = np.array([differences_ls_none, time_ls, num_found_eq]) - arr = arr.T - df = pd.DataFrame(data=arr, columns=['MAE', 'time', 'number_found_eq']) - df.to_csv(os.path.join(Path().absolute().parent, "data_wave", f"{title}.csv")) - - if print_results: - print('\nTime for every run, s:') - for item in time_ls: - print(f'{item: .4f}') - - print() - print(f'\nAverage time, s: {sum(time_ls) / len(time_ls):.2f}') - print(f'Average MAE per eq: {sum(mean_diff_ls) / len(mean_diff_ls):.6f}') - print(f'Average minimum MAE per run: {sum(differences_ls) / max_iter_number:.6f}') - print(f'Average # of found eq per run: {sum(num_found_eq) / max_iter_number:.2f}') - print(f"Runs where eq was not found: {num_found_eq.count(0)}") - print(f"Num of population error occurrence: {population_error}") diff --git a/Experiments on noised data/noised_burgers.py b/Experiments on noised data/noised_burgers.py index 3a33a34..a9800cf 100644 --- a/Experiments on noised data/noised_burgers.py +++ b/Experiments on noised data/noised_burgers.py @@ -8,6 +8,7 @@ import traceback import logging import os +import pickle rcParams.update({'figure.autolayout': True}) @@ -99,9 +100,12 @@ def hash_term(term): ''' Parameters of the experiment ''' write_csv = True print_results = True - max_iter_number = 5 - # magnitudes = [1. * 1e-5, 1.5 * 1e-5, 2 * 1e-5, 2.5 * 1e-5, 3. * 1e-5, 3.67 * 1e-5] # - magnitudes = [3. * 1e-5] + max_iter_number = 50 + eq_type = "data_burg" + # magnitudes = [1. * 1e-5, 1.5 * 1e-5, 2 * 1e-5, 2.5 * 1e-5, 3. * 1e-5, 3.67 * 1e-5] + magnitudes = [0, 9.175e-6, 1.835e-5, 2.7525e-5, 3.67 * 1e-5] + magnames = ["0", "9.175e-6", "1.835e-5", "2.7525e-5", "3.67e-5"] + mmfs = [3.5, 3.4, 3.4, 3.5, 3.5] terms = [('du/dx1',), ('du/dx2', 'u'), ('u',), ('du/dx2',), ('u', 'du/dx1'), ('du/dx1', 'du/dx2'), ] hashed_ls = [hash_term(term) for term in terms] @@ -111,8 +115,8 @@ def hash_term(term): draw_not_found = [] draw_time = [] draw_avgmae = [] - for magnitude in magnitudes: - title = f'dfs{magnitude}' + for magnitude, magname, mmf in zip(magnitudes, magnames, mmfs): + title = f'dfs{magname}_tuned2' time_ls = [] differences_ls = [] mean_diff_ls = [] @@ -121,7 +125,10 @@ def hash_term(term): i = 0 population_error = 0 while i < max_iter_number: - u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + if magnitude != 0: + u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + else: + u = u_init epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, dimensionality=dimensionality, coordinate_tensors=grids,) # epde_search_obj.set_preprocessor(default_preprocessor_type='ANN', preprocessor_kwargs={'epochs_max': 800}) @@ -130,7 +137,7 @@ def hash_term(term): try: epde_search_obj.fit(data=u, max_deriv_order=(1, 1), equation_terms_max_number=3, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-4)) + eq_sparsity_interval=(1e-08, 1e-4), mmf=mmf) except Exception as e: logging.error(traceback.format_exc()) population_error += 1 @@ -140,6 +147,11 @@ def hash_term(term): time1 = end-start res = epde_search_obj.equation_search_results(only_print=False, num=2) + + path_exp = os.path.join(Path().absolute().parent, eq_type, "equations", f"{title}_{i}.pickle") + with open(path_exp, "wb") as f: + pickle.dump(res, f) + difference_ls = find_coeff_diff(res, coefficients) if len(difference_ls) != 0: diff --git a/Experiments on noised data/noised_burgers_pysindy.py b/Experiments on noised data/noised_burgers_pysindy.py index aaadfc9..86a8623 100644 --- a/Experiments on noised data/noised_burgers_pysindy.py +++ b/Experiments on noised data/noised_burgers_pysindy.py @@ -8,6 +8,7 @@ from pathlib import Path import matplotlib.pyplot as plt from pysindy_calc_mae import calc_difference +import pickle warnings.filterwarnings("ignore", category=UserWarning) @@ -31,15 +32,18 @@ ''' Parameters of the experiment ''' iter_number = 50 -write_csv = False +write_csv = True print_results = True +eq_type = "data_pysindy_burg" draw_not_found = [] draw_time = [] draw_mae = [] -magnitudes = [0., 1. * 1e-3, 5. * 1e-3, 1. * 1e-2, 2 * 1e-2, 3 * 1e-2] -for magnitude in magnitudes: - title = f"dfp{magnitude}" +magnitudes = [0, 0.0075, 0.015, 0.0225, 0.03] +magnames = ["0", "0.0075", "0.015", "0.0225", "0.03"] + +for magnitude, magname in zip(magnitudes, magnames): + title = f"dfp{magname}" time_ls = [] mae_ls = [] found_ls = [] @@ -64,6 +68,11 @@ model.print() eq = model.equations(17) + + # path_exp = os.path.join(Path().absolute().parent, eq_type, "equations", f"{title}_{i}.pickle") + # with open(path_exp, "wb") as f: + # pickle.dump(eq, f) + mae, found_flag = calc_difference(eq[0], true_coef, true_names) mae_ls.append(mae) @@ -80,7 +89,7 @@ arr = np.array([mae_ls, time_ls, found_ls]) arr = arr.T df = pd.DataFrame(data=arr, columns=["MAE", 'time', "found"]) - df.to_csv(os.path.join(Path().absolute().parent, "data_burg_sindy", f"{title}.csv")) + df.to_csv(os.path.join(Path().absolute().parent, "data_pysindy_burg", f"{title}.csv")) if print_results: print(f"Average time, s: {sum(time_ls) / iter_number:.2f}") print(f"Average min MAE: {df.MAE.mean():.2f}") diff --git a/Experiments on noised data/noised_burgers_sindy.py b/Experiments on noised data/noised_burgers_sindy.py index 5b2908a..00571d2 100644 --- a/Experiments on noised data/noised_burgers_sindy.py +++ b/Experiments on noised data/noised_burgers_sindy.py @@ -11,6 +11,7 @@ import logging import os from pathlib import Path +import pickle def find_coeff_diff(res, coefficients: dict): @@ -110,10 +111,15 @@ def hash_term(term): draw_time = [] draw_avgmae = [] start_gl = time.time() - magnitudes = [1. * 1e-3, 5. * 1e-3, 1. * 1e-2, 2 * 1e-2, 3 * 1e-2] - # magnitudes = [3. * 1e-2, ] - for magnitude in magnitudes: - title = f'dfs{magnitude}' + # magnitudes = [0.0075, 0.015, 0.0225, 0.03] + # magnames = ["0.0075", "0.015", "0.0225", "0.03"] + + magnitudes = [0, 0.0075, 0.015, 0.0225, 0.03] + magnames = ["0", "0.0075", "0.015", "0.0225", "0.03"] + mmfs = [3.2, 3.2, 3.1, 2.8, 2.8] + + for magnitude, magname, mmf in zip(magnitudes, magnames, mmfs): + title = f'dfs{magnitude}_tuned' time_ls = [] differences_ls = [] @@ -123,7 +129,10 @@ def hash_term(term): i = 0 population_error = 0 while i < max_iter_number: - u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + if magnitude != 0: + u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + else: + u = u_init epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, dimensionality=dimensionality, coordinate_tensors=grids) @@ -132,7 +141,7 @@ def hash_term(term): try: epde_search_obj.fit(data=u, max_deriv_order=(1, 2), equation_terms_max_number=3, equation_factors_max_number=2, - eq_sparsity_interval=(1e-08, 1e-1)) + eq_sparsity_interval=(1e-08, 1e-1), mmf=mmf) except Exception as e: logging.error(traceback.format_exc()) population_error += 1 diff --git a/Experiments on noised data/noised_kdv.py b/Experiments on noised data/noised_kdv.py index 9240b2c..09ca485 100644 --- a/Experiments on noised data/noised_kdv.py +++ b/Experiments on noised data/noised_kdv.py @@ -10,6 +10,7 @@ import logging import os from pathlib import Path +import pickle def find_coeff_diff(res): @@ -94,7 +95,7 @@ def hash_term(term): path_full = os.path.join(Path().absolute().parent, "data_kdv", "KdV_sln_100.csv") df = pd.read_csv(path_full, header=None) - os.path.join(Path().absolute().parent, "data_kdv", "d_x_100.csv") + # os.path.join(Path().absolute().parent, "data_kdv", "d_x_100.csv") dddx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "ddd_x_100.csv"), header=None) ddx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "dd_x_100.csv"), header=None) dx = pd.read_csv(os.path.join(Path().absolute().parent, "data_kdv", "d_x_100.csv"), header=None) @@ -134,9 +135,17 @@ def hash_term(term): draw_time = [] draw_avgmae = [] start_gl = time.time() - magnitudes = [1. * 1e-3, 1. * 1e-2, 7. * 1e-2, 8. * 1e-2, 9. * 1e-2, 9.2 * 1e-2] - for magnitude in magnitudes: - title = f'dfs{magnitude}' + magnitudes = [0, 0.023, 0.046, 0.069, 0.092] + magnames = ["0", "0.023", "0.046", "0.069", "0.092"] + mmfs = [4.7, 4.6, 4.5, 4.5, 4.1] + # names_ls = [["0", "2e-5", "2.5e-5", "3e-5", "3.2e-5", "3.47e-5"], # [8.675e-6, 1.735e-5, 2.6025e-5, 3.47e-5] + # ["0", "1e-5", "1.5e-5", "2e-5", "2.5e-5", "3e-5", "3.67e-5"],# [9.175e-6, 1.835e-5, 2.7525e-5, 3.67e-5] + # ["0", "0.001", "0.005", "0.01", "0.02", "0.03"], # [0.0075, 0.015, 0.0225, 0.03] + # ["0", "1e-5", "3.5e-5", "5.5e-5", "8e-5", "2.26e-4"], # [2e-5, 4e-5, 6e-5, 8e-5] + # ["0", "0.001", "0.01", "0.07", "0.08", "0.09", "0.092"]] # [0.023, 0.046, 0.069, 0.092] + + for magnitude, magname, mmf in zip(magnitudes, magnames, mmfs): + title = f'dfs{magname}_tuned' time_ls = [] differences_ls = [] @@ -146,7 +155,10 @@ def hash_term(term): i = 0 population_error = 0 while i < max_iter_number: - u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + if magnitude!= 0: + u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + else: + u = u_init epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, dimensionality=dimensionality, coordinate_tensors=grids) @@ -170,7 +182,7 @@ def hash_term(term): epde_search_obj.fit(data=u, max_deriv_order=(1, 3), equation_terms_max_number=4, equation_factors_max_number=2, eq_sparsity_interval=(1e-08, 1e-06), derivs=[derivs], - additional_tokens=[custom_trig_tokens, ]) + additional_tokens=[custom_trig_tokens, ], mmf=mmf) except Exception as e: logging.error(traceback.format_exc()) population_error += 1 @@ -180,6 +192,7 @@ def hash_term(term): time1 = end-start res = epde_search_obj.equation_search_results(only_print=False, num=2) + difference_ls = find_coeff_diff(res) if len(difference_ls) != 0: @@ -230,16 +243,16 @@ def hash_term(term): plt.grid() plt.show() - plt.plot(magnitudes, draw_time, linewidth=2, markersize=9, marker='o') - plt.title("SymNet") - plt.ylabel("Time, s.") - plt.xlabel("Magnitude value") - plt.grid() - plt.show() - - plt.plot(magnitudes, draw_avgmae, linewidth=2, markersize=9, marker='o') - plt.title("SymNet") - plt.ylabel("Average MAE") - plt.xlabel("Magnitude value") - plt.grid() - plt.show() + # plt.plot(magnitudes, draw_time, linewidth=2, markersize=9, marker='o') + # plt.title("SymNet") + # plt.ylabel("Time, s.") + # plt.xlabel("Magnitude value") + # plt.grid() + # plt.show() + # + # plt.plot(magnitudes, draw_avgmae, linewidth=2, markersize=9, marker='o') + # plt.title("SymNet") + # plt.ylabel("Average MAE") + # plt.xlabel("Magnitude value") + # plt.grid() + # plt.show() diff --git a/Experiments on noised data/noised_kdv_pysindy.py b/Experiments on noised data/noised_kdv_pysindy.py index e1f7b7c..d0e9de5 100644 --- a/Experiments on noised data/noised_kdv_pysindy.py +++ b/Experiments on noised data/noised_kdv_pysindy.py @@ -8,6 +8,7 @@ import os from pathlib import Path from pysindy_calc_mae import calc_difference +import pickle warnings.filterwarnings("ignore", category=UserWarning) @@ -32,14 +33,18 @@ ''' Parameters of the experiment ''' iter_number = 50 print_results = True -write_csv = False +write_csv = True +eq_type = "data_pysindy_kdv" draw_time = [] draw_mae = [] draw_not_found = [] -magnitudes = [0., 1. * 1e-5, 3.5 * 1e-5, 5.5 * 1e-5, 8. * 1e-5, 2.26 * 1e-4] -for magnitude in magnitudes: - title = f"dfp{magnitude}" + +magnitudes = [0, 2e-5, 4e-5, 6e-5, 8. * 1e-5] +magnames = ["0", "2e-5", "4e-5", "6e-5", "8e-5"] + +for magnitude, magname in zip(magnitudes, magnames): + title = f"dfp{magname}" time_ls = [] mae_ls = [] found_ls = [] @@ -63,6 +68,11 @@ time_ls.append(time1) eq = model.equations(17) + + # path_exp = os.path.join(Path().absolute().parent, eq_type, "equations", f"{title}_{i}.pickle") + # with open(path_exp, "wb") as f: + # pickle.dump(eq, f) + mae, found_flag = calc_difference(eq[0], true_coef, true_names) mae_ls.append(mae) @@ -80,7 +90,7 @@ arr = np.array([mae_ls, time_ls, found_ls]) arr = arr.T df = pd.DataFrame(data=arr, columns=["MAE", 'time', "found"]) - df.to_csv(os.path.join(Path().absolute().parent, "data_kdv_sindy", f"{title}.csv")) + df.to_csv(os.path.join(Path().absolute().parent, "data_pysindy_kdv", f"{title}.csv")) if print_results: print(f"Average time, s: {sum(time_ls) / iter_number:.2f}") print(f"Average min MAE: {df.MAE.mean():.2f}") diff --git a/Experiments on noised data/noised_kdv_sindy.py b/Experiments on noised data/noised_kdv_sindy.py index 60144c6..0333922 100644 --- a/Experiments on noised data/noised_kdv_sindy.py +++ b/Experiments on noised data/noised_kdv_sindy.py @@ -9,6 +9,7 @@ import os from pathlib import Path import matplotlib.pyplot as plt +import pickle def find_coeff_diff(res): @@ -107,15 +108,22 @@ def hash_term(term): write_csv = True print_results = True max_iter_number = 50 + eq_type = "data_pysindy_kdv" draw_not_found = [] draw_time = [] draw_avgmae = [] start_gl = time.time() - # magnitudes = [1. * 1e-5, 1. * 1e-4, 3.5 * 1e-5, 5.5 * 1e-5, 8. * 1e-5, 2.26 * 1e-4] - magnitudes = [1. * 1e-4] - for magnitude in magnitudes: - title = f'dfs{magnitude}' + # magnitudes = [1. * 1e-5, 3.5 * 1e-5, 5.5 * 1e-5, 8. * 1e-5, 1. * 1e-4, 2.26 * 1e-4] + # magnitudes = [2.26 * 1e-4] + magnitudes = [0, 2e-5, 4e-5, 6e-5, 8. * 1e-5] # + magnames = ["0", "2e-5", "4e-5", "6e-5", "8e-5"] # + + magnitudes = [4e-5] # + magnames = ["4e-5"] # + + for magnitude, magname in zip(magnitudes, magnames): + title = f'dfs{magname}_142' time_ls = [] differences_ls = [] @@ -125,7 +133,10 @@ def hash_term(term): i = 0 population_error = 0 while i < max_iter_number: - u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + if magnitude != 0: + u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + else: + u = u_init epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, dimensionality=dimensionality, coordinate_tensors=grids) @@ -145,6 +156,11 @@ def hash_term(term): time1 = end-start res = epde_search_obj.equation_search_results(only_print=False, num=4) + + path_exp = os.path.join(Path().absolute().parent, eq_type, "equations", f"{title}_{i}.pickle") + with open(path_exp, "wb") as f: + pickle.dump(res, f) + difference_ls = find_coeff_diff(res) if len(difference_ls) != 0: differences_ls.append(min(difference_ls)) @@ -194,16 +210,16 @@ def hash_term(term): plt.grid() plt.show() - plt.plot(magnitudes, draw_time, linewidth=2, markersize=9, marker='o') - plt.title("SymNet") - plt.ylabel("Time, s.") - plt.xlabel("Magnitude value") - plt.grid() - plt.show() - - plt.plot(magnitudes, draw_avgmae, linewidth=2, markersize=9, marker='o') - plt.title("SymNet") - plt.ylabel("Average MAE") - plt.xlabel("Magnitude value") - plt.grid() - plt.show() + # plt.plot(magnitudes, draw_time, linewidth=2, markersize=9, marker='o') + # plt.title("SymNet") + # plt.ylabel("Time, s.") + # plt.xlabel("Magnitude value") + # plt.grid() + # plt.show() + # + # plt.plot(magnitudes, draw_avgmae, linewidth=2, markersize=9, marker='o') + # plt.title("SymNet") + # plt.ylabel("Average MAE") + # plt.xlabel("Magnitude value") + # plt.grid() + # plt.show() diff --git a/Experiments on noised data/noised_wave.py b/Experiments on noised data/noised_wave.py index a24f849..36bf0d5 100644 --- a/Experiments on noised data/noised_wave.py +++ b/Experiments on noised data/noised_wave.py @@ -8,6 +8,7 @@ import logging import os from pathlib import Path +import pickle rcParams.update({'figure.autolayout': True}) @@ -103,19 +104,19 @@ def hash_term(term): write_csv = True print_results = True max_iter_number = 50 - magnitudes = [2. * 1e-5, 2.5 * 1e-5, 3. * 1e-5, 3.2 * 1e-5, 3.47 * 1e-5] - # magnitudes = [3.47 * 1e-5] + eq_type = "data_wave" + magnitudes = [0, 8.675e-6, 1.735e-5, 2.6025e-5, 3.47e-5] + magnames = ["0", "8.675e-6", "1.735e-5", "2.6025e-5", "3.47e-5"] + mmfs = [1.1, 1.0, 1.0, 1.0, 1.0] draw_not_found = [] draw_time = [] draw_avgmae = [] start_gl = time.time() not_found_ls = [] - # string = "0.04073797307153838 * d^2u/dx2^2{power: 1.0} + 0.0014821321520873156 * du/dx1{power: 1.0} + 0.034797130718506576 = d^2u/dx1^2{power: 1.0}" - # difference_ls = find_diff_str(string, coefficients) - for magnitude in magnitudes: - title = f'dfs{magnitude}' + for magnitude, magname, mmf in zip(magnitudes, magnames, mmfs): + title = f'dfs{magname}' time_ls = [] differences_ls = [] @@ -125,7 +126,10 @@ def hash_term(term): i = 0 population_error = 0 while i < max_iter_number: - u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + if magnitude != 0: + u = u_init + np.random.normal(scale=magnitude * np.abs(u_init), size=u_init.shape) + else: + u = u_init epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary, dimensionality=dimensionality, coordinate_tensors=grids, prune_domain=False) @@ -135,7 +139,7 @@ def hash_term(term): try: epde_search_obj.fit(data=u, max_deriv_order=(2, 2), equation_terms_max_number=3, equation_factors_max_number=1, - eq_sparsity_interval=(1e-08, 5)) + eq_sparsity_interval=(1e-08, 5), mmf=mmf) except Exception as e: logging.error(traceback.format_exc()) population_error += 1 @@ -145,6 +149,11 @@ def hash_term(term): time1 = end-start res = epde_search_obj.equation_search_results(only_print=False, num=2) + + path_exp = os.path.join(Path().absolute().parent, eq_type, "equations", f"{title}_{i}.pickle") + with open(path_exp, "wb") as f: + pickle.dump(res, f) + difference_ls = find_coeff_diff(res, coefficients) if len(difference_ls) != 0: @@ -192,24 +201,3 @@ def hash_term(term): end_gl = time.time() print(f"Overall time: {(end_gl - start_gl) / 3600:.2f}, h.") print(f"Runs where eq was not found for each magn: {not_found_ls}") - # print(f"Overall time: {end_gl - start_gl:.2f}, s.") - # plt.title("Original") - # plt.plot(magnitudes, draw_not_found, linewidth=2, markersize=9, marker='o') - # plt.ylabel("No. runs with not found eq.") - # plt.xlabel("Magnitude value") - # plt.grid() - # plt.show() - # - # plt.plot(magnitudes, draw_time, linewidth=2, markersize=9, marker='o') - # plt.title("Original") - # plt.ylabel("Time, s.") - # plt.xlabel("Magnitude value") - # plt.grid() - # plt.show() - # - # plt.plot(magnitudes, draw_avgmae, linewidth=2, markersize=9, marker='o') - # plt.title("Original") - # plt.ylabel("Average MAE") - # plt.xlabel("Magnitude value") - # plt.grid() - # plt.show() \ No newline at end of file