Skip to content

Commit

Permalink
Add metrics to measure algorithm efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
LisIva committed Oct 7, 2023
1 parent 4c9bc7d commit 00fcc7e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
43 changes: 29 additions & 14 deletions experiment_kdv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from epde.interface.prepared_tokens import CustomTokens
from kdv_init_distrib import coefficients1, coefficients2

import traceback
import logging


def find_coeff_diff(res):
differences = []
Expand Down Expand Up @@ -121,14 +124,15 @@ def hash_term(term):
''' Parameters of the experiment '''
write_csv = False
print_results = True
max_iter_number = 1
max_iter_number = 10
title = 'df0'


time_ls = []
differences_ls = []
num_found_eq = []
for i in range(max_iter_number):
mean_diff_ls = []
i = 0
while i < max_iter_number:
epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary,
dimensionality=dimensionality, coordinate_tensors=grids)

Expand All @@ -150,11 +154,15 @@ def hash_term(term):

epde_search_obj.set_moeadd_params(population_size=8, training_epochs=90)
start = time.time()

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, ])
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())
i -= 1
continue
end = time.time()
epde_search_obj.equation_search_results(only_print=True, num=2)
time1 = end-start
Expand All @@ -164,11 +172,13 @@ def hash_term(term):
difference_ls = find_coeff_diff(res)
if len(difference_ls) != 0:
differences_ls.append(min(difference_ls))
else:
differences_ls.append(None)
mean_diff_ls += difference_ls
# else:
# differences_ls.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)

if write_csv:
Expand All @@ -178,10 +188,15 @@ def hash_term(term):
df.to_csv(f'data_kdv/{title}.csv')

if print_results:
print('\nAverage time, s:', sum(time_ls) / len(time_ls))
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('\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):.4f}')
print(f'Average minimum MAE per run: {sum(differences_ls) / len(differences_ls):.4f}')
print(f'Average # of found eq per run: {sum(num_found_eq) / len(num_found_eq):.2f}')
48 changes: 16 additions & 32 deletions experiment_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,6 @@ def hash_term(term):


if __name__ == '__main__':
distrib1 = {('u',): 17,
('du/dx1',): 34,
('d^2u/dx1^2',): 38,
('du/dx2',): 41,
('d^2u/dx2^2',): 30}
distrib2 = {('u',): 17,
('du/dx1',): 34,
('d^2u/dx1^2',): 48,
('du/dx2',): 41,
('d^2u/dx2^2',): 42}

distrib3 = {('u',): 17,
('du/dx1',): 34,
('d^2u/dx1^2',): 93,
('du/dx2',): 41,
('d^2u/dx2^2',): 94}

distrib4 = {('u',): 1,
('du/dx1',): 1,
('d^2u/dx1^2',): 1,
('du/dx2',): 1,
('d^2u/dx2^2',): 1}
# draw_all_distributions(distrib1, distrib2, distrib3, "t")

path = "data_wave/"
df = pd.read_csv(f'{path}wave_sln_100.csv', header=None)
Expand All @@ -175,8 +152,7 @@ def hash_term(term):
''' Parameters of the experiment '''
write_csv = False
print_results = True
max_iter_number = 1
distrib = {} # {} or distrib1 or distrib2 or distrib3 or distrib4
max_iter_number = 50
title = 'df0'
''''''

Expand All @@ -187,6 +163,7 @@ def hash_term(term):

time_ls = []
differences_ls = []
mean_diff_ls = []
num_found_eq = []
for i in range(max_iter_number):
epde_search_obj = epde_alg.EpdeSearch(use_solver=False, boundary=boundary,
Expand All @@ -199,7 +176,7 @@ def hash_term(term):
# equation_factors_max_number = 1!!!!!!!!!!!
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), custom_prob_terms=distrib)
eq_sparsity_interval=(1e-08, 5))
end = time.time()
epde_search_obj.equation_search_results(only_print=True, num=2)
time1 = end-start
Expand All @@ -209,10 +186,12 @@ def hash_term(term):

if len(difference_ls) != 0:
differences_ls.append(min(difference_ls))
else:
differences_ls.append(None)
mean_diff_ls += difference_ls
# else:
# differences_ls.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)

if write_csv:
Expand All @@ -222,10 +201,15 @@ def hash_term(term):
df.to_csv(f'data_wave/{title}.csv')

if print_results:
print('\nAverage time, s:', sum(time_ls) / len(time_ls))
print('\nTime for every run, s:')
for item in time_ls:
print(f'{item: .4f}')
print('\nMAE and # of found equations in every run:')
for item1, item2 in zip(differences_ls, num_found_eq):
print(f"diff: {item1:.5f}", "num eq:", item2)
# print('\nMAE and # of found equations in every run:')
# for item1, item2 in zip(differences_ls, num_found_eq):
# print(f"diff: {item1:.5f}", "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):.4f}')
print(f'Average minimum MAE per run: {sum(differences_ls) / len(differences_ls):.4f}')
print(f'Average # of found eq: {sum(num_found_eq) / len(num_found_eq):.2f}')
4 changes: 2 additions & 2 deletions symnet/initcoefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def get_csym_tsym(u, derivs, shape, input_names, pool_names, sparsity=0.001, add
# TODO: SymNet имеет 3 todo (+, pool_terms, preproc_input)

# TODO: что делать с left_side_name? (случ. генер.?)
left_side_name = ('du/dx1', )
# left_side_name = ('du/dx1', )
# TODO: если в левой части e.g. d^2u/dx2^2, то как получить в правой слагаемое d^2u/dx2^2 * u?
# left_side_name = ('d^2u/dx2^2',)
left_side_name = ('d^2u/dx2^2',)

input_names, idx = clean_names(left_side_name, input_names)
x_train, y_train = prepare_batches(u, derivs, shape, idx, additional_tokens=additional_tokens)
Expand Down

0 comments on commit 00fcc7e

Please sign in to comment.