Solve for the set of coordinates that reduces the average distances between request and server in half

0

I generate a DataFrame with coordinates and distances to 3 servers.

import pandas as pd
import numpy as np
import random
from math import sin, cos, sqrt, atan2, radians

def haversine(lat1, lon1, lat2, lon2):
    R = 6373.0
    lat1 = radians(lat1)
    lon1 = radians(lon1)
    lat2 = radians(lat2)
    lon2 = radians(lon2)

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    distance = R * c
    return distance

def nearest_server(distances):
    nearest = np.argmax(distances)
    return distances[nearest]

def main():
    df = [(random.uniform(-90.0000, 90.0000), random.uniform(-180.0000, 80.0000)) for i in range(0, 10000)]
    df = pd.DataFrame(df, columns=['lat', 'lon'])

    # Calculate the haversine distances from request to each server
    df['server_1_km'] = df.apply(lambda x: haversine(x['lat'], x['lon'], 26.91, 75.78), axis=1)
    df['server_2_km'] = df.apply(lambda x: haversine(x['lat'], x['lon'], 43.54, -116.56), axis=1)
    df['server_3_km'] = df.apply(lambda x: haversine(x['lat'], x['lon'], 18.51, 73.84), axis=1)

    #determine the nearest distance in kilometers
    df['nearest_distance_km'] = df.apply(lambda x: nearest_server([x['server_1_km'], x['server_2_km'], x['server_3_km']]), axis=1)

if __name__ == '__main__':
    main()

The resulting dataframe:

df.head()
         lat        lon   server_1_km  server_2_km   server_3_km  nearest_distance_km
0  46.669639  39.533947   3853.364128  9715.302979   4425.403201          9715.302979
1  64.845647 -75.829235   9524.431080  3449.905531  10396.715639         10396.715639
2 -12.997567 -85.863194  17561.869774  7018.701526  17765.524191         17765.524191
3  58.701055 -95.549241  10465.032112  2215.936083  11378.201340         11378.201340
4  26.392046 -61.901963  12560.106384  5229.352101  13109.087621         13109.087621

The average nearest_distance_km from request to server:

df['nearest_distance_km'].mean()
12581.12885335224

I need to solve for any set of server coordinates of a "minimum" cardinality that halves that distance (12581/2 == 6290.5).

Looking for interesting solutions/pointers to solving this problem.

Note: It doesn't matter if the new coordinates are in the middle of the ocean. Although, restricting the solution to land-only coordinates is nice-to-have.

zfact0rial

Posted 2021-02-25T23:47:03.820

Reputation: 31

No answers