Spaces:
Runtime error
Runtime error
import numpy as np | |
def get_simple_data(num_points, max_domain): | |
full_data = [] | |
for i in range(num_points): | |
new_datum = [np.random.uniform(0, max_domain), | |
np.random.uniform(0, max_domain), | |
np.random.uniform(0, max_domain)] | |
full_data.append(new_datum) | |
return full_data | |
def get_simple_var_names(): | |
return ["a", "b", "c"] | |
def get_xy_var_names(): | |
return ["x", "y"] | |
def get_angle_data(num_data): | |
full_data = [] | |
for i in range(num_data): | |
theta = np.random.uniform(0, 2 * np.pi) | |
while np.abs(theta - np.pi / 2) < 1e-3 or np.abs(theta - 3 * np.pi / 2) < 1e-3: | |
theta = np.random.uniform(0, 2 * np.pi) | |
new_datum = [np.sin(theta), np.cos(theta), np.tan(theta)] | |
full_data.append(new_datum) | |
return full_data | |
def get_fake_angles(num_data): | |
full_data = [] | |
for i in range(num_data): | |
[s, c] = np.random.uniform(-1, 1, 2) | |
t = np.random.uniform(-5, 5) | |
new_datum = [s, c, t] | |
full_data.append(new_datum) | |
return full_data | |
def get_xy_data(num_data, max_domain): | |
full_data = [] | |
for i in range(num_data): | |
# t = np.random.uniform(0, 2 * np.pi) | |
# r = 2 + 3 * np.cos(t) | |
# # r = 5 * np.cos(2*t)/np.cos(t) | |
# | |
# x = r * np.cos(t) | |
# y = r * np.sin(t) | |
y = np.random.uniform(-3, 2) | |
sign = np.random.choice([-1, 1]) | |
x = sign * np.sqrt(np.abs(2 * np.sin(y) + 5 - y ** 3)) | |
full_data.append([x, y]) | |
return full_data | |
def get_fake_xy_data(num_data, max_domain): | |
full_data = [] | |
for i in range(num_data): | |
[x, y] = np.random.uniform(0, max_domain, 2) | |
full_data.append([x, y]) | |
return full_data | |
def get_triangle_data(num_triangles, max_domain): | |
full_data = [] | |
for i in range(num_triangles): | |
[x1, y1, x2, y2, x3, y3] = np.random.uniform(0, max_domain, 6) | |
a = np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) | |
b = np.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2) | |
c = np.sqrt((x1 - x3) ** 2 + (y1 - y3) ** 2) | |
va = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) | |
vb = np.arccos((c ** 2 + a ** 2 - b ** 2) / (2 * a * c)) | |
vc = np.arccos((a ** 2 + b ** 2 - c ** 2) / (2 * b * a)) | |
sa, sb, sc = np.sin([va, vb, vc]) | |
ca, cb, cc = np.cos([va, vb, vc]) | |
ta, tb, tc = np.tan([va, vb, vc]) | |
new_datum = [] | |
# new_datum.extend([x1, x2, x3]) | |
# new_datum.extend([y1, y2, y3]) | |
new_datum.extend([a, b, c]) | |
# new_datum.extend([va, vb, vc]) | |
# new_datum.extend([sa, sb, sc]) | |
# new_datum.extend([ca, cb, cc]) | |
# new_datum.extend([ta, tb, tc]) | |
full_data.append(new_datum) | |
return full_data | |
def get_right_triangle_data(num_triangles, max_domain): | |
full_data = [] | |
for i in range(num_triangles): | |
[s1, s2] = np.random.uniform(0, max_domain, 2) | |
c = max(s1, s2) | |
a = min(s1, s2) | |
b = np.sqrt(c * c - a * a) | |
va = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) | |
vb = np.arccos((c ** 2 + a ** 2 - b ** 2) / (2 * a * c)) | |
vc = np.arccos((a ** 2 + b ** 2 - c ** 2) / (2 * b * a)) | |
sa, sb, sc = np.sin([va, vb, vc]) | |
ca, cb, cc = np.cos([va, vb, vc]) | |
ta, tb, tc = np.tan([va, vb, vc]) | |
new_datum = [] | |
new_datum.extend([a, b, c]) | |
# new_datum.extend([sa, sb, sc]) | |
# new_datum.extend([ca, cb, cc]) | |
# new_datum.extend([ta, tb, tc]) | |
full_data.append(new_datum) | |
return full_data | |
def get_fake_triangle_data(num_triangles, max_domain): | |
full_data = [] | |
for i in range(num_triangles): | |
[a, b, c] = np.random.uniform(0, max_domain, 3) | |
[sa, sb, sc] = np.random.uniform(-1, 1, 3) | |
[ca, cb, cc] = np.random.uniform(-1, 1, 3) | |
[ta, tb, tc] = np.random.uniform(-5, 5, 3) | |
new_datum = [] | |
new_datum.extend([a, b, c]) | |
# new_datum.extend([va, vb, vc]) | |
new_datum.extend([sa, sb, sc]) | |
new_datum.extend([ca, cb, cc]) | |
new_datum.extend([ta, tb, tc]) | |
# new_datum.extend([va, vb, vc]) | |
# new_datum.extend([a, 2*a]) | |
# new_datum.extend([ta, tb, tc]) | |
full_data.append(new_datum) | |
return full_data | |
def get_non_triangle_data(num_triangles, max_domain): | |
full_data = [] | |
for i in range(num_triangles): | |
[x1, y1, x2, y2, x3, y3, x4, y4] = np.random.uniform(0, max_domain, 8) | |
a = np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) | |
b = np.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2) | |
c = np.sqrt((x4 - x3) ** 2 + (y4 - y3) ** 2) | |
d = np.sqrt((x1 - x4) ** 2 + (y1 - y4) ** 2) | |
e24 = np.sqrt((x4 - x2) ** 2 + (y4 - y2) ** 2) | |
e13 = np.sqrt((x3 - x1) ** 2 + (y3 - y1) ** 2) | |
va = np.arccos((b ** 2 + c ** 2 - e24 ** 2) / (2 * b * c)) | |
vb = np.arccos((d ** 2 + c ** 2 - e13 ** 2) / (2 * d * c)) | |
vc = np.arccos((d ** 2 + a ** 2 - e24 ** 2) / (2 * d * a)) | |
sa, sb, sc = np.sin([va, vb, vc]) | |
ca, cb, cc = np.cos([va, vb, vc]) | |
ta, tb, tc = np.tan([va, vb, vc]) | |
new_datum = [] | |
new_datum.extend([a, b, c]) | |
# new_datum.extend([va, vb, vc]) | |
new_datum.extend([sa, sb, sc]) | |
new_datum.extend([ca, cb, cc]) | |
# new_datum.extend([ta, tb, tc]) | |
full_data.append(new_datum) | |
return full_data | |
def get_var_names(): | |
# var_names = ["a", "b", "c"] | |
var_names = [] | |
var_names.extend(["sin(x)", "cos(x)", "tan(x)"]) | |
# var_names.extend(["A", "B", "C"]) | |
var_names.extend(["sin(A)", "sin(B)", "sin(C)"]) | |
var_names.extend(["cos(A)", "cos(B)", "cos(C)"]) | |
var_names.extend(["tan(A)", "tan(B)", "tan(C)"]) | |
return var_names | |
# | |
# our_results = [] | |
# | |
# settings.true_eqn = "0*x1" | |
# settings.num_features = num_smp_features | |
# settings.show_output = False | |
# settings.keep_logs = False | |
# | |
# model = SFL() | |
# | |
# for trial_round in range(num_trials): | |
# sampled_features = np.random.choice(range(len(full_data[0])), num_smp_features, replace=True) | |
# | |
# # sampled_features = [3,6] | |
# data = [[row[smp_i] for smp_i in sampled_features] for row in full_data] | |
# smp_var_names = [var_names[smp_i] for smp_i in sampled_features] | |
# print("Trial round {} of {}.".format(trial_round + 1, num_trials)) | |
# print(" Using variables {}.".format(smp_var_names)) | |
# | |
# settings.fixed_x = [] | |
# settings.fixed_y = [] | |
# for line in data: | |
# settings.fixed_x.append(line) | |
# settings.fixed_y.append(0) | |
# | |
# model.reset(var_names=smp_var_names) | |
# | |
# # train_X = DataUtils.generate_data(settings.train_N, n_vars=model.n_input_variables, | |
# # avoid_zero=settings.avoid_zero) | |
# # valid_X = DataUtils.generate_data(settings.train_N, n_vars=model.n_input_variables, | |
# # avoid_zero=settings.avoid_zero) | |
# # test_X = DataUtils.generate_data(settings.test_N, n_vars=model.n_input_variables, | |
# # min_x=settings.test_scope[0], | |
# # max_x=settings.test_scope[1]) | |
# train_X = np.array(data) | |
# valid_X = np.array(data) | |
# test_X = np.array(data) | |
# | |
# train_X = train_X.reshape([-1, settings.num_dims_per_feature, settings.num_features]) | |
# valid_X = valid_X.reshape([-1, settings.num_dims_per_feature, settings.num_features]) | |
# test_X = test_X.reshape([-1, settings.num_dims_per_feature, settings.num_features]) | |
# | |
# start_time = time.time() | |
# best_model, best_iter, best_err = model.repeat_train(train_X, | |
# settings.num_train_repeat_processes, | |
# test_x=test_X) | |
# running_time = time.time() - start_time | |
# | |
# print("best_model: {}".format(best_model)) | |
# print("----------------------") | |
# print("Finished this experiment. Took {:.2f} minutes.\n".format(running_time / 60)) | |
# | |
# our_results.append([best_err, best_model, smp_var_names]) | |
# our_results = sorted(our_results, key=lambda entry: entry[0]) | |
# | |
# output_file = open("images/triangle_output.txt", "w") | |
# for entry in our_results: | |
# output_file.write("{}\n{}\n{}\n\n".format(entry[0], entry[2], entry[1])) | |
# output_file.close() | |
# | |
# print("Final solution found at attempt {}:".format(best_iter)) | |
# print("y = {}".format(best_model)) | |
# print("Test error: {}".format(best_err)) | |
# if best_err < 0.02: | |
# print("Attained error less than 0.02 - great!") | |
# print() | |