from dataclasses import dataclass from scipy.optimize import basinhopping import numpy as np import math from random import randrange @dataclass class Vertexer: nodes: np.ndarray c: float def reconstruct(self, times): def ssr_error(point): return np.sum(((np.linalg.norm(self.nodes - point, axis=1) / self.c) - times)**2) result = basinhopping(ssr_error, np.array([0,0]), niter=10000) print(result) # Simulate sources to test code x_1 = randrange(10); y_1 = randrange(10) x_2 = randrange(10); y_2 = randrange(10) x_3 = randrange(10); y_3 = randrange(10) # Pick source to be at random location x = randrange(10); y = randrange(10); # Set velocity c = 299792 # km/ns # Generate simulated source t_1 = math.sqrt( (x - x_1)**2 + (y - y_1)**2 ) / c t_2 = math.sqrt( (x - x_2)**2 + (y - y_2)**2 ) / c t_3 = math.sqrt( (x - x_3)**2 + (y - y_3)**2 ) / c t_1 = np.random.normal(t_1, 0.001) t_2 = np.random.normal(t_2, 0.001) t_3 = np.random.normal(t_3, 0.001) print(x,y) myVertexer = Vertexer(np.array([[x_1, y_1],[x_2, y_2],[x_3, y_3]]), c) print(myVertexer.reconstruct(np.array([t_1, t_2, t_3])))