<p>A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new center in a position such that <strong>the sum of the euclidean distances to all customers is minimum</strong>.</p>

<p>Given an array <code>positions</code> where <code>positions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> is the position of the <code>ith</code> customer on the map, return <em>the minimum sum of the euclidean distances</em> to all customers.</p>

<p>In other words, you need to choose the position of the service center <code>[x<sub>centre</sub>, y<sub>centre</sub>]</code> such that the following formula is minimized:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" />
<p>Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" style="width: 377px; height: 362px;" />
<pre>
<strong>Input:</strong> positions = [[0,1],[1,0],[1,2],[2,1]]
<strong>Output:</strong> 4.00000
<strong>Explanation:</strong> As shown, you can see that choosing [x<sub>centre</sub>, y<sub>centre</sub>] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" style="width: 419px; height: 419px;" />
<pre>
<strong>Input:</strong> positions = [[1,1],[3,3]]
<strong>Output:</strong> 2.82843
<strong>Explanation:</strong> The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= positions.length &lt;= 50</code></li>
	<li><code>positions[i].length == 2</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 100</code></li>
</ul>

class Solution:
    def getMinDistSum(self, positions: List[List[int]]) -> float:
        EPSILON = 1.0
        ALPHA = 0.5
        BETA = 0.8
        N = len(positions)
        def calcgradient(x, y):
            ans = [0, 0]
            for i in range(N):
                denom = math.sqrt(pow(positions[i][0]-x, 2) + pow(positions[i][1] - y, 2))
                
                ans[0] += (x - positions[i][0])/denom
                ans[1] += (y - positions[i][1])/denom
            return ans
        
        def object(x, y):
            res = 0.0
            for i in range(N):
                res += math.sqrt(pow(positions[i][0]-x, 2) + pow(positions[i][1] - y, 2)) 
            # print("x:", x, " obj:", res)
            return res
        
        def armijo(x, y, epsilon):
            grad = calcgradient(x, y)
            left = object(x - epsilon * grad[0], y - epsilon * grad[1]) - object(x, y)
            right =  -1 * ALPHA * epsilon *  (grad[0] * grad[0] + grad[1] * grad[1])
            res = left <= right
            # print(left, right)
            return res
        def backtrack(x, y):
            eps = EPSILON
            while armijo(x, y, eps) is False:
                eps = BETA * eps
            return eps
        x = 0.1
        y = 0.1
        res = object(x, y)
        flag = True
        while flag:
            # print(i)
            stepsize = backtrack(x, y)
            grad = calcgradient(x, y)
            x -= stepsize * grad[0]
            y -= stepsize * grad[1]
            nres = object(x, y)
            # print(x, y, nres)
            if nres < res - 0.00000001:
                flag = True
            else:
                flag = False
            res = nres
        return  res
16600