Ejemplo n.º 1
0
def main():
    images = {"eagle": EAGLE, "toucan": TOUCAN, "winnie": WINNIE}
    args = sys.argv[2:]
    art = images[args[0] if len(sys.argv) > 1 else "eagle"]
    lines = art.split("\n")[1:]
    rows = len(lines)
    columns = max(len(line) for line in lines)
    buffer = []
    for line in lines:
        whitespaces = columns - len(line) + 1
        buffer_line = line + " " * whitespaces
        buffer.append(list(buffer_line))

    initial, goal = ("4 2 5 3 6 8 1 7 0", "0 1 2 3 4 5 6 7 8")
    factory = problem.ProblemFactory()
    problem_instance = factory.from_npuzzle(initial, goal)
    heuristic = factory.heuristic_for(problem_instance)

    size = int(len(goal.replace(" ", ""))**0.5)
    step_row = len(buffer) // size
    step_col = len(buffer[0]) // size

    for i in range(step_row):
        for j in range(step_col):
            buffer[i][j] = "#"

    solution = search.astar(problem_instance, heuristic)
    image = deepcopy(buffer)
    for state, action in solution:
        draw_image(state, image, buffer, size)
        sleep(1)
Ejemplo n.º 2
0
    def __generate_plan(self, body, fruit):
        problem = RouteProblem(body[0], self.size, self.obstacles, body, fruit)
        plan = astar(problem, problem.heuristic)
        if plan != FAILURE:
            plan.pop(0)

        return plan
Ejemplo n.º 3
0
 def test_romania(self):
     # Distances to Bucharest
     heuristic_dict = {
         "Arad": 366,
         "Bucharest": 0,
         "Craiova": 160,
         "Drobeta": 242,
         "Eforie": 161,
         "Fagaras": 176,
         "Giurgiu": 77,
         "Hirsova": 151,
         "Iasi": 226,
         "Lugoj": 244,
         "Mehadia": 241,
         "Neamt": 234,
         "Oradea": 380,
         "Pitesti": 100,
         "RimnicuVilcea": 193,
         "Sibiu": 253,
         "Timisoara": 329,
         "Urziceni": 80,
         "Vaslui": 199,
         "Zerind": 374
     }
     heuristic = lambda source: heuristic_dict[source]
     assert_astar = functools.partial(
         self.assert_solution,
         lambda problem: search.astar(problem, heuristic), "romania_map")
     expected = ["Arad", "Sibiu", "RimnicuVilcea", "Pitesti", "Bucharest"]
     assert_astar("Arad", "Bucharest", expected)
Ejemplo n.º 4
0
def main():
    images = {"eagle": EAGLE, "toucan": TOUCAN, "winnie": WINNIE}
    args = sys.argv[2:]
    art = images[args[0] if len(sys.argv) > 1 else "eagle"]
    lines = art.split("\n")[1:]
    rows = len(lines)
    columns = max(len(line) for line in lines)
    buffer = []
    for line in lines:
        whitespaces = columns - len(line) + 1
        buffer_line = line + " " * whitespaces
        buffer.append(list(buffer_line))

    initial, goal = ("4 2 5 3 6 8 1 7 0", "0 1 2 3 4 5 6 7 8")
    factory = problem.ProblemFactory()
    problem_instance = factory.from_npuzzle(initial, goal)
    heuristic = factory.heuristic_for(problem_instance)

    size = int(len(goal.replace(" ", "")) ** 0.5)
    step_row = len(buffer) // size
    step_col = len(buffer[0]) // size

    for i in range(step_row):
        for j in range(step_col):
            buffer[i][j] = "#"

    solution = search.astar(problem_instance, heuristic)
    image = deepcopy(buffer)
    for state, action in solution:
        draw_image(state, image, buffer, size)
        sleep(1)
Ejemplo n.º 5
0
    def __generate_plan(self, body, fruit):
        problem = RouteProblem(body[0], self.size,
                               self.obstacles, body,
                               fruit)
        plan = astar(problem, problem.heuristic)
        if plan != FAILURE:
            plan.pop(0)

        return plan
Ejemplo n.º 6
0
    def test_Bulgaria(self):
        # Straight line distances to Pernik
        # source http://distance.bg360.net/
        pernik_dist = {
            "Sofia": 25.4,
            "Pernik": 0,
            "Kustendil": 45.3,
            "Dupnica": 38,
            "Blagoevgrad": 65,
            "Sandanski": 117,
            "Kulata": 138,
            "Botevgrad": 70,
            "Vraca": 80,
            "Montana": 90,
            "Belogradchik": 117,
            "Lom": 136,
            "Vidin": 155,
            "Lovech": 149,
            "Pleven": 156,
            "Tarnovo": 217,
            "Biala": 239,
            "Ruse": 274,
            "Razgrad": 303,
            "Shumen": 326,
            "Dobrich": 403,
            "Silistra": 381,
            "Varna": 402,
            "Burgas": 362.9,
            "Iambol": 284,
            "Plovdiv": 149,
            "Karlovo": 144,
            "StaraZagora": 214,
            "Kazanlak": 192,
            "Gabrovo": 188,
            "Haskovo": 220,
            "Kardzhali": 221,
            "Smolian": 178,
            "Pazardzhik": 116,
            "Pirdop": 93,
            "Troian": 140,
            "Sliven": 269
        }

        heuristic = lambda town: pernik_dist[town]
        assert_astar = functools.partial(
            self.assert_solution,
            lambda problem: search.astar(problem, heuristic), "Bulgaria_map")

        assert_astar(
            "Varna", "Pernik",
            ['Varna', 'Shumen', 'Tarnovo', 'Botevgrad', 'Sofia', 'Pernik'])
        assert_astar("Vidin", "Pernik",
                     ['Vidin', 'Montana', 'Sofia', 'Pernik'])
        assert_astar("Silistra", "Pernik", [
            'Silistra', 'Ruse', 'Biala', 'Lovech', 'Botevgrad', 'Sofia',
            'Pernik'
        ])
Ejemplo n.º 7
0
 def test_Bulgaria_disconnected(self):
     heuristic_dict = {
         "Pernik": 20,
         "Sofia": 20,
         "Varna": 120,
         "Burgas": 120,
         "Dupnica": 35,
         "Kustendil": 35
     }
     heuristic = lambda source: heuristic_dict[source]
     search_algo = lambda problem: search.astar(problem, heuristic)
     self.assert_Bulgaria_disconnected(search_algo)
Ejemplo n.º 8
0
 def test_romania(self):
     # Distances to Bucharest
     heuristic_dict = {
         "Arad": 366, "Bucharest": 0, "Craiova": 160,
         "Drobeta": 242, "Eforie": 161, "Fagaras": 176,
         "Giurgiu": 77, "Hirsova": 151, "Iasi": 226, "Lugoj": 244,
         "Mehadia": 241, "Neamt": 234, "Oradea": 380, "Pitesti": 100,
         "RimnicuVilcea": 193, "Sibiu": 253, "Timisoara": 329,
         "Urziceni": 80, "Vaslui": 199, "Zerind": 374
     }
     heuristic = lambda source: heuristic_dict[source]
     assert_astar = functools.partial(self.assert_solution,
                     lambda problem: search.astar(problem, heuristic),
                     "romania_map")
     expected = ["Arad", "Sibiu", "RimnicuVilcea", "Pitesti", "Bucharest"]
     assert_astar("Arad", "Bucharest", expected)
Ejemplo n.º 9
0
    def test_Bulgaria(self):
        # Straight line distances to Pernik
        # source http://distance.bg360.net/
        pernik_dist = {
            "Sofia": 25.4, "Pernik": 0, "Kustendil": 45.3, "Dupnica": 38,
            "Blagoevgrad": 65, "Sandanski": 117, "Kulata": 138, "Botevgrad": 70,
            "Vraca": 80, "Montana": 90, "Belogradchik": 117, "Lom": 136,
            "Vidin": 155, "Lovech": 149, "Pleven": 156, "Tarnovo": 217,
            "Biala": 239, "Ruse": 274, "Razgrad": 303, "Shumen": 326,
            "Dobrich": 403, "Silistra": 381, "Varna": 402, "Burgas": 362.9,
            "Iambol": 284, "Plovdiv": 149, "Karlovo": 144, "StaraZagora": 214,
            "Kazanlak": 192, "Gabrovo": 188, "Haskovo": 220, "Kardzhali": 221,
            "Smolian": 178, "Pazardzhik": 116, "Pirdop": 93, "Troian": 140,
            "Sliven": 269
        }

        heuristic = lambda town: pernik_dist[town]
        assert_astar = functools.partial(self.assert_solution,
                        lambda problem: search.astar(problem, heuristic),
                        "Bulgaria_map")

        assert_astar("Varna", "Pernik", ['Varna', 'Shumen', 'Tarnovo', 'Botevgrad', 'Sofia', 'Pernik'])
        assert_astar("Vidin", "Pernik", ['Vidin', 'Montana', 'Sofia', 'Pernik'])
        assert_astar("Silistra", "Pernik", ['Silistra', 'Ruse', 'Biala', 'Lovech', 'Botevgrad', 'Sofia', 'Pernik'])
Ejemplo n.º 10
0
 def assert_npuzzle(self, initial, goal, expected_solution_length):
     factory = problem.ProblemFactory()
     problem_instance = factory.from_npuzzle(initial, goal)
     heuristic = factory.heuristic_for(problem_instance)
     solution = search.astar(problem_instance, heuristic)
     self.assertEqual(len(solution), expected_solution_length)
Ejemplo n.º 11
0
 def test_Bulgaria_disconnected(self):
     heuristic_dict = { "Pernik": 20, "Sofia": 20, "Varna": 120,
                       "Burgas": 120, "Dupnica": 35, "Kustendil": 35 }
     heuristic = lambda source: heuristic_dict[source]
     search_algo = lambda problem: search.astar(problem, heuristic)
     self.assert_Bulgaria_disconnected(search_algo)
Ejemplo n.º 12
0
 def assert_npuzzle(self, initial, goal, expected_solution_length):
     factory = problem.ProblemFactory()
     problem_instance = factory.from_npuzzle(initial, goal)
     heuristic = factory.heuristic_for(problem_instance)
     solution = search.astar(problem_instance, heuristic)
     self.assertEqual(len(solution), expected_solution_length)