コード例 #1
0
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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)
コード例 #6
0
ファイル: tsp.py プロジェクト: Jaleksi/path_planner
def tsp(distances=None, num_of_nodes=None):
    '''https://mlrose.readthedocs.io/en/stable/source/tutorial2.html'''

    fitness_coordinates = TravellingSales(distances=distances)
    problem_fit = TSPOpt(length=num_of_nodes,
                         fitness_fn=fitness_coordinates,
                         maximize=False)
    best_route, _ = genetic_alg(problem_fit, random_state=2)
    return best_route
コード例 #7
0
def generateTSP(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=True)
コード例 #8
0
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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)
コード例 #9
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)

        g = nx.Graph()
        for a, b, distance in distances:

            g.add_edge(a, b, length=int(round(distance)))

        return TSPOpt(coords=coords, distances=distances, maximize=False, source_graph=g)
コード例 #10
0
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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)
コード例 #11
0
ファイル: test_opt_probs.py プロジェクト: ziweiwu/mlrose
    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)