Example #1
0
    def test_matrix_has_proper_shape(self):
        """N sources and M destinations should produce an (N x M) array"""
        distance_matrix = distances.euclidean_distance_matrix(
            sources, destinations)

        N, M = sources.shape[0], destinations.shape[0]
        assert distance_matrix.shape == (N, M)
Example #2
0
    def test_distance_is_euclidean(self):
        """It must return an actual Euclidean distance
        In this case, it is easy to see that the distance from [1, 1] to [4, 5]
        is:
            sqrt((4 - 1)**2 + (5 - 1)**2) = sqrt(3**2 + 4**2) = 5
        """
        sources = np.array([1, 1])
        destinations = np.array([4, 5])

        distance_matrix = distances.euclidean_distance_matrix(
            sources, destinations)

        assert distance_matrix[0] == 5.
Example #3
0
    def test_square_matrix_is_symmetric(self):
        distance_matrix = distances.euclidean_distance_matrix(sources)

        assert np.allclose(distance_matrix, distance_matrix.T)
Example #4
0
    def test_square_matrix_has_zero_diagonal(self):
        """Main diagonal is the distance from a point to itself"""
        distance_matrix = distances.euclidean_distance_matrix(sources)

        assert np.all(np.diag(distance_matrix) == 0)
Example #5
0
    def test_all_elements_are_non_negative(self):
        """Being distances, all elements must be non-negative"""
        distance_matrix = distances.euclidean_distance_matrix(
            sources, destinations)

        assert np.all(distance_matrix >= 0)
Example #6
0
def get_solution(locations):
    distance_matrix = euclidean_distance_matrix(locations)

    return solve_tsp_dynamic_programming(distance_matrix)
                best_neighbor, best_neighbor_path = neighbors(matrix, neighbor)

        if current_path < global_path:
            global_path = current_path
            global_solution = current_solution

        current_solution = random_solution(matrix, initial_point)
        current_path = calculate_distance(matrix, current_solution)
        neighbor = neighbors(matrix, current_solution)[0]
        best_neighbor, best_neighbor_path = neighbors(matrix, neighbor)

    return global_path, global_solution, time.time() - start_time


sources = np.array([[40.73024833, -73.79440675], [41.47362495, -73.92783272],
                    [41.26591, -73.21026228], [41.3249908, -73.507788]])

distance_with_numba, b, c = best_solution(sources)
print(distance_with_numba)

from python_tsp.distances import euclidean_distance_matrix
from python_tsp.exact import solve_tsp_dynamic_programming

distance_matrix = euclidean_distance_matrix(sources)
permutation, distance_without_numba = solve_tsp_dynamic_programming(
    distance_matrix)


def test_distance():
    assert np.linalg.norm(distance_without_numba - distance_with_numba) < 1e-3