Example #1
0
def GBFS(start, goal):
    q = queue.deque()
    q.append(start)

    previous = []  # khởi tạo previous kiểu dictionary
    for city in myMap.keys():
        previous.append((city, {'from': None, 'heuristic': heuristic[city]}))
    previous = dict(previous)

    counter = 0
    while 1:
        counter += 1
        showStep(counter, q, previous,
                 'heuristic')  # Show các bước thực hiện thuật toán

        curCity = q.popleft()  # dequeue

        if curCity == goal:
            print('Success!')
            showResultWithAttr(previous, start, goal, 'heuristic')
            return True  # Tìm thấy goal

        for city in myMap[curCity].keys(
        ):  # Các thành phố có thể đi đến được từ thành phố hiện tại
            if previous[city]['from'] == None:  # Nếu chưa đi qua thành phố này
                q.append(city)  # Thêm vào hàng đợi
                previous[city]['from'] = curCity  # Lưu lại dấu vết

        if len(q) == 0:
            print('Fail!')
            return False  # Không thể đi đến goal từ start
        q = queue.deque(aweSomeSort(q, previous, 'heuristic'))
Example #2
0
def LCBFS(start, goal):
    q = queue.deque()  # khởi tạo queue
    q.append(start)  # enqueue

    previous = []  # khởi tạo previous kiểu dictionary
    for city in myMap.keys():
        previous.append((city, {'from': None, 'total_cost': 0}))
    previous = dict(previous)

    counter = 0
    while 1:
        counter += 1
        showStep(counter, q, previous)  # Show các bước thực hiện thuật toán

        curCitys = []
        while len(q) != 0:
            curCitys.append(
                q.popleft())  # Lấy ra hết tất cả thành phố có thể đi đến được

        for curCity in curCitys:
            curCityTotalCost = previous[curCity][
                'total_cost']  # Chi phí để đi từ start đến curCity

            if curCity != goal:
                for city in myMap[curCity].keys(
                ):  # Các thành phố có thể đi đến được từ curCity
                    cityTotalCost = previous[city][
                        'total_cost']  # Chi phí được tính ở bước trước đó
                    totalCost = myMap[curCity][city][
                        'cost'] + curCityTotalCost  # Chi phí để đi từ start -> curCity -> city

                    # Nếu chưa đi qua thành phố này hoặc chi phi đi từ start -> curCity -> city tốt hơn chi phí trước đó
                    if previous[city][
                            'from'] == None or totalCost < cityTotalCost:
                        if q.count(
                                city
                        ) != 0:  # Nếu đã có city này trong hàng đợi thì xóa nó đi
                            q.remove(city)
                        q.append(city)  # Thêm vào hàng đợi
                        previous[city]['from'] = curCity  # Lưu lại dấu vết
                        previous[city][
                            'total_cost'] = totalCost  # Cập nhật lại chi phí mới

        if len(q) == 0: break

    if previous[goal]['from'] != None:  # Nếu có thể đi tới goal
        showResultWithAttr(previous, start, goal)
        return True
    else:
        return False
Example #3
0
def UCS(start, goal):
    q = queue.deque()  # khởi tạo queue
    q.append(start)  # enqueue

    previous = []  # khởi tạo previous kiểu dictionary
    for city in myMap.keys():
        previous.append((city, {'from': None, 'total_cost': 0}))
    previous = dict(previous)

    counter = 0
    while 1:
        counter += 1
        showStep(counter, q, previous)  # Show các bước thực hiện thuật toán
        curCity = q.popleft(
        )  # enqueue (Đây cũng là phần tử nhỏ nhất vì đã sắp xếp queue tăng dần)
        curCityTotalCost = previous[curCity][
            'total_cost']  # Chi phí để đi từ start đến curCity

        if curCity != goal:
            for city in myMap[curCity].keys(
            ):  # Các thành phố có thể đi đến được từ curCity
                cityTotalCost = previous[city][
                    'total_cost']  # Chi phí được tính ở bước trước đó
                totalCost = myMap[curCity][city][
                    'cost'] + curCityTotalCost  # Chi phí để đi từ start -> curCity -> city

                # Nếu chưa đi qua thành phố này hoặc chi phi đi từ start -> curCity -> city tốt hơn chi phí trước đó
                if previous[city]['from'] == None or totalCost < cityTotalCost:
                    if q.count(
                            city
                    ) != 0:  # Nếu đã có city này trong hàng đợi thì xóa nó đi
                        q.remove(city)
                    q.append(city)  # Thêm vào hàng đợi
                    previous[city]['from'] = curCity  # Lưu lại dấu vết
                    previous[city][
                        'total_cost'] = totalCost  # Cập nhật lại chi phí mới
        else:
            showResultWithAttr(previous, start, goal)
            return True

        if len(q) == 0: break
        q = queue.deque(aweSomeSort(
            q, previous))  # Sắp xếp lại hàng đợi tăng dần theo total_cost

    return False
Example #4
0
def BFS(start, goal):
  q = queue.deque() # khởi tạo queue
  q.append(start)  # enqueue
  
  previous = [] # khởi tạo previous kiểu dictionary
  for city in myMap.keys():
    previous.append((city, None))
  previous = dict(previous)
  
  while 1:
    curCity = q.popleft() # dequeue
    for city in myMap[curCity].keys(): # Các thành phố có thể đi đến được từ thành phố hiện tại
      if previous[city] == None: # Nếu chưa đi qua thành phố này
        q.append(city) # Thêm vào hàng đợi
        previous[city] = curCity # Lưu lại dấu vết
        if city == goal:
          print('Success!')
          showResult(previous, start, goal)
          return True # Tìm thấy goal

    if len(q) == 0:
      print('Fail!')
      return False # Không thể đi đến goal từ start
Example #5
0
def DFS(start, goal):
    previous = []  # khởi tạo previous kiểu dictionary
    for city in myMap.keys():
        previous.append((city, None))
    previous = dict(previous)

    def _DFS(_start):  # Chạy đệ qui DFS
        for city in myMap[_start].keys(
        ):  # Các thành phố có thể đi đến được từ thành phố hiện tại
            if previous[city] == None:  # Nếu chưa đi qua thành phố này
                previous[city] = _start  # Lưu lại dấu vết
                if city == goal:
                    return True
                elif _DFS(city):
                    return True
        return False

    if (_DFS(start)):
        print('Success!')
        showResult(previous, start, goal)
        return True  # Tìm thấy goal
    else:
        print('Fail!')
        return False