def compare(steps, number_ktrans, number_rays, fineness, inner_of_box): radius = 3 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) discrete_model = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) one_d_model = threed_to_oned(discrete_model, cuboid_coordinates, steps) source_point_d = create_source_point_d(radius, number_ktrans, perc_circle=0.125) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) cont_results = create_con_results(rays_d, inner_of_box, fineness) dis_results = k_trafo_one_dim_all(liam_all, one_d_model) return cont_results, dis_results
def v_step_function(values_1d, *args): """the function we are optimizing""" liam_all, cont_results, steps, cuboid_coordinates, rho, u = args value_results = k_trafo_one_dim_all(liam_all, values_1d) value_difference_norm = np.linalg.norm(value_results - cont_results)**2 uv_dif = np.linalg.norm(u - values_1d) return value_difference_norm + (rho / 2) * uv_dif
def eq_system_for_opt(values, *args): """the function we are optimizing""" liam_all, cont_results, steps, cuboid_coordinates, alpha = args value_results = k_trafo_one_dim_all(liam_all, values) value_difference = value_results - cont_results value_difference_norm = (np.sum(value_difference**2) / len(value_difference))**0.5 return value_difference_norm
def y_function(values_1d, *args): """the function we are optimizing - y = values_1d""" liam_all, cont_results, gamma, x, z = args value_results = k_trafo_one_dim_all(liam_all, values_1d) value_difference_norm = np.linalg.norm(value_results - cont_results)**2 difference = np.linalg.norm(2 * x - z - values_1d)**2 y = 0.5 * difference + 0.5 * gamma * value_difference_norm return y
def test_k_transform_oned_vs_threed(): """ test if the three dimensional K-transform leads to the same result as the one dimensional K transform for symmetric inner_of_box functions """ def inner_of_box(x, y, z): """ Different shells around the origin. """ rad_pos = (x**2 + y**2 + z**2)**0.5 if rad_pos <= 0.2: return 1 if rad_pos <= 0.5: return 0.8 if rad_pos <= 0.8: return 0.5 return 0 steps = 20 cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } number_rays = {'dim_1': 5, 'dim_2': 5} number_ktrans = 10 big_line_matrix_array, ktra_v, touched, values, x_cor, y_cor, z_cor, rays_d =\ generate_everything(number_ktrans, cuboid_coordinates, number_rays, steps, inner_of_box) result_3d = k_transform(values, big_line_matrix_array) fineness = 10**3 radius = 2 source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_1d = k_trafo_one_dim_all(liam_all, values_1d) assert (result_3d == result_1d).all()
def callback_fun(values, *args): """this callback function is used to display the value of the function we are optimizing""" global iteration liam_all, cont_results, steps, cuboid_coordinates, alpha = args iteration += 1 a = k_trafo_one_dim_all(liam_all, values) - cont_results print( str(iteration) + " : Value of the function: " + str(np.sum(a**2)**0.5))
def test_create_con_results(): """ testing of the cont results of the k_transform are the same as the one for the 1d k_transform, if the object is discrete """ def inner_of_box(x, y, z): if (0.5 > x > -0.5) and (0.5 > y > -0.5) and (0.5 > z > -0.5): return 1 return 0 number_rays = {'dim_1': 1, 'dim_2': 1} cuboid_coordinates = { 'x1': -1, 'x2': 1, 'y1': -1, 'y2': 1, 'z1': -1, 'z2': 1 } steps = 4 radius = 2 fineness = 10**4 number_ktrans = 5 source_point_d = create_source_point_d(radius, number_ktrans) rays_d = generate_rays_d(source_point_d, cuboid_coordinates, number_rays) cont_results = create_con_results(rays_d, inner_of_box, fineness) liam_all = generate_all_line_integral_array_matrix(rays_d, cuboid_coordinates, steps, fineness) x_cor, y_cor, z_cor = coordinate_discretization(cuboid_coordinates, steps) values = discretization_of_model(x_cor, y_cor, z_cor, inner_of_box, steps) values_1d = threed_to_oned(values, cuboid_coordinates, steps) result_1d = k_trafo_one_dim_all(liam_all, values_1d) print(cont_results) print(result_1d) np.testing.assert_array_almost_equal(cont_results, result_1d, decimal=2)
options={ 'disp': False, 'maxcor': 10, 'ftol': 10**-20, 'gtol': 10**-20, 'eps': 10**-8, # if to high stops fast 'maxiter': 10**7, 'maxls': 100, 'maxfun': 10**7 }) if i > 0: change_in_solution(old_solution, v.x) print( np.linalg.norm(cont_results_noise - k_trafo_one_dim_all(liam_all, v.x))) #np.linalg.norm(cont_results_noise - dis_results), old_solution = v.x # + np.average(v.x) * 0.05 * (np.random.random(len(v.x)) - 0.5) v1d = threed_to_oned(values, cuboid_coordinates, steps) plt.plot(v1d, "-o") plt.plot(v.x, "-o") plt.savefig(str(i) + ".png") plt.close() """ #while dist > 3 * 10**-13: while dist > 0.0118: u = threed_to_oned(values, cuboid_coordinates, steps) * 0 v = scipy.optimize.minimize(