Code Monkey home page Code Monkey logo

code_ai's Introduction

BÀI TẬP LỚN TRÍ TUỆ NHÂN TẠO

NHÓM 11 - 66PM4

Bài Tập:

Sử dụng danh sách móc nối để thực hiện tìm kiếm trên đồ thị
sử dụng thuật toán tìm kiếm đường đi với giá cực tiểu với tri thức bổ sung

Hướng dẫn sử dụng và ý tưởng

I. Hướng dẫn sử dụng

image

  • Dòng đầu tiên chính là nhập các đỉnh và giá trị dự đoán, ở ví dụ trên A h(A) = 14: image

  • Dòng thứ 2 nhập khoảng cách giữa 2 cạnh, ở ví dụ trên độ cài A đến B là 5: image

  • Sau đó chúng ta nhập điểm bắt đầu và điểm kết thúc và enter là ra kết quả: image

    Lưu ý: có thể viết chữ cái là chữ thường

II. Ý tưởng

  1. Ta tạo 1 class node để lưu thông tin của đỉnh bao gồm tên đỉnh(value) và giá trị dự đoán(h_value):
class Node:
   def __init__(self, value, h_value=0):
        self.value = value
        self.h_value = h_value
        self.next = None
  1. Tạo 1 class dinh_ke để lưu thông tin các đỉnh kề của node:
 class dinh_ke:
    def __init__(self, node, cost):
        self.node = node
        self.cost = cost
        self.next = None
  1. Tạo class doThi:
  • khởi tạo nodes để lưu trữ các đỉnh đã được nhập
class doThi:
    def __init__(self):
        self.nodes = {}
  • hàm add_node được gọi add node mới vào nodes
    def add_node(self, value, h_value=0):
        self.nodes[value] = Node(value, h_value)
  • Khi nhập cạnh A,B,5 thì hàm add_canh, hàm này gọi 2 lần hàm add_dinh_ke với thông số ngược nhau

  • nhằm tạo ra và trỏ dến đỉnh kề của thằng còn lại, dễ hiểu là:

    node A (A,14) -> đỉnh kề B (node B, cost) node B (B,10) -> đỉnh kề A (node A, cost) trong đó cost là khoảng cách A tới B

def add_canh(self, node1, node2, cost):
        if node1 not in self.nodes or node2 not in self.nodes:
            print("Đỉnh được nhập không tồn tại! KẾT THÚC BÀI TOÁN")
            sys.exit()
        self.add_dinh_ke(node1, node2, cost)
        self.add_dinh_ke(node2, node1, cost)
def add_dinh_ke(self, node_value, dinh_ke_value, cost):
        dinh_ke_node = self.nodes[dinh_ke_value]
        new_dinh_ke = dinh_ke(dinh_ke_node, cost)

        current_node = self.nodes[node_value]
        if not current_node.next:
            current_node.next = new_dinh_ke
        else:
            temp = current_node.next
            while temp.next:
                temp = temp.next
            temp.next = new_dinh_ke
  • Đoạn thuật toán lưu ý 2 cái

  • thứ nhât: lưu f_m bằng 1 dictionary với f_m[tên đỉnh] = giá trị

    • nếu là đỉnh mới thì thêm f_m[tên đỉnh] = giá trị

    • còn là đỉnh cũ thì xem giá trị f_m mới có bé hơn k, nếu bé hơn thì cập nhật f_m[tên đỉnh mới] = giá trị

  • thứ hai: cùng lúc cập nhật f_m ta cũng cập nhật đỉnh dẫn đến nó node_duong_di[đỉnh đang xét] = đỉnh dẫn đến đỉnh đang xét

    • từ đó ta đảo ngược lại lần lượt và tìm ra đường đi ngắn nhất và cost thấp nhất

code_ai's People

Contributors

nghiendam2601 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.