I am a new to programming and particularly python but I am trying to learn it and I found it to be very fascinating so far.
I have a list of 30 fixed coordinates x and y.
x = np.array([[13,10,12,13,11,12,11,13,12,13,14,15,15,16,18,2,3,4,6,9,1,3,6,7,8,10,12,11,10,30]])
y = np.array([[12,11,10,9,8,7,6,6,7,8,11,12,13,15,14,18,12,11,10,13,15,16,18,17,16,15,14,13,12,3]])
I want to find an optimized (centralized) location that can connect up to a maximum of the 10 fixed coordinates by finding the minimum distance.
I tired to use an optimization algorithm but i can only get one optimum location for a list of coordinates (i.e. i cannot constraint the location to 10 coordinates).
Any help would be appreciated.
def objective(x):
px = x[0]
py = x[1]
wdistance = sum(((px - wx)**2 + (py - wy)**2)**0.5)
tdistance = (((px - tx)**2 + (py - ty)**2)**0.5)
cost = wdistance * 2.5 + tdistance * 5
return cost
# initial estimate of the centralized location
x0 = [10,10]
# boundary values for the centralized locations
bnds = ((0,15), (0,15))
sol = minimize(objective, x0, bounds=bnds, method='SLSQP')
print(sol)
As indicated in my comment, i think this problem is NP-hard and therefore it's not easy to tackle it.
It's a problem with some kind of cardinality-constraints, which means, it's hard/impossible to use continuous-optimization (assumed for nearly everything in scipy.optimize) in general.
Furthermore, minimizing the minimum-distance is not particularly smooth, also assumed by pretty much everything within scipy. But reformulations are possible (at least when constraints are supported!).
Without the cardinality-constraints (core of the discrete nature of this problem), it collapses into the Smallest Enclosing Circle Problem problem, easily solved in linear-time. More general wiki-article. But this does not help much for the given problem.
Here is a demo, probably not for the faint of heart (depending on the background-knowledge):
based on Mixed Integer Second Order Cone Programming
using cvxpy as modelling-tool
The following code will solve the 10 out of 20 problem (first 20 of your points; where one duplicate was thrown away for better visualization!)
It will fail for the full problem (quite ok approximation will be returned; but not optimal!). I blame ECOS_BB (but being very thankful to Han Wang for the code at the same time!). Your problem most surely will be solved easily by better alternatives. But don't expect any solver to do it for 1000 points :-).
Code:
Output:
Plot:
EDIT
I ported the above code to julia to be able to use the mentioned solver Pajarito. Despite not much julia-programming for me in the past, it is working now:
This approach is solving your full problem (again i dropped the duplicate-point) to optimality!
It outputs some numerical instability warning, but claims the result to be optimal! (Pajarito is still pretty new research-software and i did not tune the numerous options; and we use open-source solvers within which are not that robust compared to commercial alternatives)
I'm very satisfied with the result and i'm glad i gave Julia/Convex.jl/Pajarito a try!
Code:
Output:
Visualization:
And one more plot for select-23 out of all points: