-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_distance_calc.py
58 lines (46 loc) · 1.56 KB
/
test_distance_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from math import pow, log
from geopy.distance import distance as distance_func
from scipy.optimize import minimize
# Startup Sauna
points = [
("d6d73e4d84c92f8c5fff4340a5dce12f", 60.18498637, 24.83608603, 29395),
("d6d73e4d84c92f8c5fff4340a5dce12f", 60.18466897, 24.83625233, 24566),
("d6d73e4d84c92f8c5fff4340a5dce12f", 60.18432798, 24.83515263, 19105),
("d6d73e4d84c92f8c5fff4340a5dce12f", 60.18508505, 24.83578026, 27669),
]
def score_to_distance(score: int | float) -> float:
"""Distance from the real answer.
Derived by finding best fit using different models.
"""
if score < 0 or score > 30000:
raise ValueError("score has to be between 0 and 30000")
a = 30000
b = -0.005
return log(score / a) / b
def mse(x, locations, distances) -> float:
mse = 0.0
for location, distance in zip(locations, distances):
distance_calculated = distance_func(x, location).meters
mse += pow(distance_calculated - distance, 2.0)
return mse / len(distances)
locations = []
distances = []
for point in points[1:]:
coords = (point[1], point[2])
score = point[3]
locations.append(coords)
d = score_to_distance(score)
distances.append(d)
print(f"{locations = }")
print(f"{distances = }")
initial_location = min(zip(distances, locations), key=lambda x: x[0])[1]
print(f"{initial_location = }")
result = minimize(
mse,
initial_location,
args=(locations, distances),
method="L-BFGS-B",
options={"ftol": 1e-5, "maxiter": 1e7},
)
estimated_location = result.x
print(f"{estimated_location = }")