示例#1
0
    def test_random():
        """Test random method"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)
        rand = problem.random()

        assert (len(rand) == 5 and len(set(rand)) == 5)
示例#2
0
    def test_adjust_probs_all_zero():
        """Test adjust_probs method when all elements in input vector sum to
        zero."""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)

        probs = np.zeros(5)

        assert np.array_equal(problem.adjust_probs(probs), np.zeros(5))
示例#3
0
    def test_adjust_probs_non_zero():
        """Test adjust_probs method when all elements in input vector sum to
        some value other than zero."""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)

        probs = np.array([0.1, 0.2, 0, 0, 0.5])
        x = np.array([0.125, 0.25, 0, 0, 0.625])

        assert np.array_equal(problem.adjust_probs(probs), x)
示例#4
0
    def test_reproduce_mut1():
        """Test reproduce method when mutation_prob is 1"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)

        father = np.array([0, 1, 2, 3, 4])
        mother = np.array([4, 3, 2, 1, 0])

        child = problem.reproduce(father, mother, mutation_prob=1)

        assert (len(child) == 5 and len(set(child)) == 5)
示例#5
0
    def __compute_shortest_path(self, starting_zone_id, zones_ids):
        """
        Compute the shortest path within a sequence of geo-localized zones by solving the
        Travelling Salesperson Problem (TSP) optimization problem with a genetic algorithm.
        """
        zones = [starting_zone_id] + zones_ids
        coords_list = []

        # Extract zones coordinates
        grid_h = self.sim_input.grid_matrix.shape[0]

        for zone in zones:
            c = int(np.floor(zone / grid_h))
            r = int(zone - c * grid_h)

            coords_list.append((c, r))

        # Solve the TSP optimization problem
        tsp_problem = TSPOpt(length=len(coords_list),
                             coords=coords_list,
                             maximize=False)

        best_path, _ = genetic_alg(tsp_problem, max_iters=10, random_state=2)
        best_path = deque(best_path)

        # Rotate the path to its best form until
        # the starting point P is not 0
        P = best_path[0]

        while P != 0:
            best_path.rotate(1)
            P = best_path[0]

        return [zones[i] for i in list(best_path)]
示例#6
0
    def test_random_neighbor():
        """Test random_neighbor method"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)

        x = np.array([0, 1, 2, 3, 4])
        problem.set_state(x)

        neigh = problem.random_neighbor()
        abs_diff = np.abs(x - neigh)
        abs_diff[abs_diff > 0] = 1

        sum_diff = np.sum(abs_diff)

        assert (len(neigh) == 5 and sum_diff == 2 and len(set(neigh)) == 5)
示例#7
0
    def test_random_mimic():
        """Test random_mimic method"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        pop = np.array([[1, 0, 3, 2, 4], [0, 2, 1, 3, 4], [0, 2, 4, 3, 1],
                        [4, 1, 3, 2, 0], [3, 4, 0, 2, 1], [2, 4, 0, 3, 1]])

        problem = TSPOpt(5, distances=dists)
        problem.keep_sample = pop
        problem.eval_node_probs()
        problem.find_sample_order()

        rand = problem.random_mimic()

        assert (len(rand) == 5 and len(set(rand)) == 5)
示例#8
0
    def generate(seed, number_of_cities, area_width=250, area_height=250):
        np.random.seed(seed)
        x_coords = np.random.randint(area_width, size=number_of_cities)
        y_coords = np.random.randint(area_height, size=number_of_cities)

        coords = list(tuple(zip(x_coords, y_coords)))
        duplicates = TSPGenerator.list_duplicates_(coords)

        while len(duplicates) > 0:
            for d in duplicates:
                x_coords = np.random.randint(area_width, size=len(d))
                y_coords = np.random.randint(area_height, size=len(d))
                for i in range(len(d)):
                    coords[d[i]] = (x_coords[i], y_coords[i])
                    pass
            duplicates = TSPGenerator.list_duplicates_(coords)
        distances = TSPGenerator.get_distances(coords, False)

        return TSPOpt(coords=coords, distances=distances, maximize=False)
示例#9
0
    def test_find_neighbors():
        """Test find_neighbors method"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        problem = TSPOpt(5, distances=dists)

        x = np.array([0, 1, 2, 3, 4])
        problem.set_state(x)
        problem.find_neighbors()

        neigh = np.array([[1, 0, 2, 3, 4], [2, 1, 0, 3, 4], [3, 1, 2, 0, 4],
                          [4, 1, 2, 3, 0], [0, 2, 1, 3, 4], [0, 3, 2, 1, 4],
                          [0, 4, 2, 3, 1], [0, 1, 3, 2, 4], [0, 1, 4, 3, 2],
                          [0, 1, 2, 4, 3]])

        assert np.array_equal(np.array(problem.neighbors), neigh)
示例#10
0
    def compute_shortest_path(self, starting_zone_id, other_zone_ids):
        zones = [starting_zone_id]
        [zones.append(zone) for zone in other_zone_ids]

        coords_list = []
        for zone in zones:
            zone_column = int(np.floor(
                zone / self.simInput.grid_matrix.shape[0]
            ))
            zone_row = int(
                zone - zone_column * self.simInput.grid_matrix.shape[0]
            )
            coords_list.append((zone_column, zone_row))

        problem = TSPOpt(length=len(coords_list), coords=coords_list, maximize=False)
        best_path, _ = genetic_alg(problem, max_iters=10, random_state=2)

        best_path = deque(best_path)
        starting_point = best_path[0]
        while starting_point != 0:  # Starting point is not worker position
            best_path.rotate(1)
            starting_point = best_path[0]

        return [zones[i] for i in list(best_path)]
示例#11
0
    def test_sample_pop():
        """Test sample_pop method"""

        dists = [(0, 1, 3), (0, 2, 5), (0, 3, 1), (0, 4, 7), (1, 3, 6),
                 (4, 1, 9), (2, 3, 8), (2, 4, 2), (3, 2, 8), (3, 4, 4)]

        pop = np.array([[1, 0, 3, 2, 4], [0, 2, 1, 3, 4], [0, 2, 4, 3, 1],
                        [4, 1, 3, 2, 0], [3, 4, 0, 2, 1], [2, 4, 0, 3, 1]])

        problem = TSPOpt(5, distances=dists)

        problem.keep_sample = pop
        problem.eval_node_probs()

        sample = problem.sample_pop(100)
        row_sums = np.sum(sample, axis=1)

        assert (np.shape(sample)[0] == 100 and np.shape(sample)[1] == 5
                and max(row_sums) == 10 and min(row_sums) == 10)