Tải bản đầy đủ (.pdf) (22 trang)

Final project of analysis and design of algorithms ESSAY list all possible algorithm design strategies to solve the problem

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (284.01 KB, 22 trang )

VIETNAM GENERAL CONFEDERATION OF LABOUR
TON DUC THANG UNIVERSITY
FACULTY OF INFORMATION TECHNOLOGY

Final project of analysis and design of algorithms

ESSAY

Instructing Lecturer: Teacher NGUYỄN CHÍ THIỆN
Students: NGUYỄN VĂN TÀI – 518H0050
TRẦN CƠNG PHÚ – 518H0550
Khố

THÀNH PHỐ HỒ CHÍ MINH, NĂM 2022

: 22


VIETNAM GENERAL CONFEDERATION OF LABOUR
TON DUC THANG UNIVERSITY
FACULTY OF INFORMATION TECHNOLOGY

Final project of analysis and design of algorithms

ESSAY

Instructing Lecturer: Teacher NGUYỄN CHÍ THIỆN
Students: NGUYỄN VĂN TÀI – 518H0050
TRẦN CƠNG PHÚ – 518H0550
Khố


THÀNH PHỐ HỒ CHÍ MINH, NĂM 2022

: 22



i

LỜI CẢM ƠN
Để hoàn thành tiểu luận này, em xin gửi lời cảm ơn chân thành đến:
Các thầy cô trong khoa Công nghệ thông tin đã cung cấp các kiến thức nền tảng để áp
dụng và nghiên cứu thông tin trong bài tiểu luận.
Em xin bày tỏ lòng biết ơn sâu sắc đến thầy Nguyễn Chí Thiện người đã trực tiếp giảng
dạy và hướng dẫn tạo mọi điều kiện thuận lợi giúp đỡ em trong quá trình thực hiện đề
tài.
Tuy đã có nhiều cố gắng, nhưng chắc chắn tiểu luận của em cịn có vài điểm sai sót.
Rất mong nhận được sự nhận xét, ý kiến đóng góp, phê bình từ phía Thầy để bài tiểu
luận được hồn thiện hơn.
Xin chân thành cám ơn!


ii

LỜI CAM ĐOAN
Tôi xin cam đoan đây là sản phẩm đồ án của riêng tôi và được sự hướng dẫn của
Bui Thanh Hung. Các kết quả trong đề tài này là trung thực và chưa cơng bố dưới bất
kỳ hình thức nào trước đây. Những số liệu trong các bảng biểu phục vụ cho việc phân
tích, nhận xét, đánh giá được chính tác giả thu thập từ các nguồn khác nhau có ghi rõ
trong phần tài liệu tham khảo.
Ngồi ra, trong tiểu luận còn sử dụng một số nhận xét, đánh giá cũng như số

liệu của các tác giả khác, cơ quan tổ chức khác đều có trích dẫn và chú thích nguồn
gốc.
Nếu phát hiện có bất kỳ sự gian lận nào tơi xin hồn tồn chịu trách nhiệm
về nội dung đồ án của mình. Trường đại học Tơn Đức Thắng không liên quan đến
những vi phạm tác quyền, bản quyền do tơi gây ra trong q trình thực hiện (nếu có).
TP. Hồ Chí Minh, ngày 31 tháng 03 năm 2022
Tác giả
(ký tên và ghi rõ họ tên)

Trần Công Phú
Nguyễn Văn Tài


iii

PHẦN XÁC NHẬN VÀ ĐÁNH GIÁ CỦA GIẢNG VIÊN
Phần xác nhận của GV hướng dẫn

_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
Tp. Hồ Chí Minh, ngày
tháng
(kí và ghi họ tên)

năm


Phần đánh giá của GV chấm bài

_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
_________________________________________________________
Tp. Hồ Chí Minh, ngày tháng năm
(kí và ghi họ tên)


iv


v

TÓM TẮT


1

MỤC LỤC
LỜI CẢM ƠN.................................................................................................................i
LỜI CAM ĐOAN..........................................................................................................ii
PHẦN XÁC NHẬN VÀ ĐÁNH GIÁ CỦA GIẢNG VIÊN.........................................iii
TĨM TẮT.....................................................................................................................iv
MỤC LỤC...................................................................................................................... 1

DANH MỤC KÍ HIỆU VÀ CHỮ VIẾT TẮT................................................................2
DANH MỤC CÁC BẢNG BIỂU, HÌNH VẼ, ĐỒ THỊ..................................................3
CHƯƠNG I - What is Traveling Salesman Problem?....................................................4
CHƯƠNG II - List all possible algorithm design strategies to solve the problem:.........6
1.

Nearest-neighbor algorithm:.....................................................................6

2.

Brute force algorithm:...............................................................................8

3.

Ant colony optimization algorithm:........................................................10

CHƯƠNG III - FUTURE SCOPE................................................................................11
CHƯƠNG IV - REFERENCES...................................................................................12


2

DANH MỤC KÍ HIỆU VÀ CHỮ VIẾT TẮT


3

DANH MỤC CÁC BẢNG BIỂU, HÌNH VẼ, ĐỒ THỊ



4

CHƯƠNG I - What is Traveling Salesman Problem
The traveling salesman problem (TSP) is an algorithmic problem tasked with
finding the shortest route between a set of points and locations that must be
visited. In the problem statement, the points are the cities a salesperson might
visit. The salesman’s goal is to keep both the travel costs and the distance
traveled as low as possible.
Given a list of cities, what is the shortest possible route that visits each city and
returns to the origin city?
Rules:
 Each city needs to be visited exactly one time
 We must return to the starting city, so our total distance needs to be
calculated accordingly
import matplotlib.pyplot as plt
x=[1, 3, 2, 4, 5.5, 5, 7]
y=[4.5, 7, 3, 1, 2.5, 7.5, 4]
alphabet = 'abcdefghijklmnopqrstuvwxyz'
plt.plot(x,y, 'ro')
def
   
   
   

connectPoints(x,y,p1,p2):
x1, x2 = x[p1], x[p2]
y1, y2 = y[p1], y[p2]
plt.plot([x1,x2],[y1,y2],'g-')

def calDistance(i, j):

    return ((x[i]-x[j])**2+(y[i]-y[j])**2)**(1/2)
allPaths = []
for i in range(len(x)):
    plt.annotate(alphabet[i].upper(), (x[i], y[i]))
    for j in range(i+1, len(x)):
        allPaths.append(str(i)+str(j))


5

for path in allPaths:
    connectPoints(x, y, int(path[0]), int(path[1]))
plt.axis('equal')
plt.show()

Fig. 1. An instance of TSP


6

CHƯƠNG II - List all possible algorithm design strategies to solve
the problem:
1. Nearest-neighbor algorithm:

Fig. 2. Solving TSP using NN
Describe:
The nearest neighbor algorithm was one of the first algorithms used to solve the
travelling salesman problem approximately. In that problem, the salesman starts
at a random city and repeatedly visits the nearest city until all have been visited.
The algorithm quickly yields a short tour, but usually not the optimal one

Steps:
 Choose a city randomly to start


7

 Moving to the nearest city which haven't ever been arrived
 Repeat the first two steps until there is no city left
 Comeback to the starting city
Implementation:
graph = {}
for i in range(len(x)):
    rowList = {}
    for j in range(len(x)):
        if calDistance(i, j) != 0:
            rowList[alphabet[j].upper()] = calDistance(i, j)
    graph[alphabet[i].upper()] = rowList
print(graph)
def TSP(G):
    V = [i for i in G.keys()]
    s = [V[0]]
    visited = [V[0]]
    for i in range(len(V)):
        adj = findVertices(G, s[i])
        for j in adj:
            if j not in visited:
                s.append(j)
                visited.append(j)
                break
    s.append(s[0])

    return s
def
   
   
   
   
   
   
   
   
   

edges(G):  
edges = []
for vertex in G.keys():
    for neighbour in G[vertex].keys():
        forward_edge = (vertex, neighbour, G[vertex][neighbour])
        backward_edge = (neighbour, vertex, G[vertex][neighbour], )
        if (forward_edge not in edges) and (backward_edge not in edges):
            edges.append(forward_edge)
edges = sorted(edges, key=lambda e: e[2])
return edges

def findVertices(G, v):
    E = edges(G)


8

 

 
 
 
 
 
 

 
 
 
 
 
 
 

connected = []
for e in E:
    if e[0] == v:
        connected.append(e[1])
    elif e[1] == v:
        connected.append(e[0])
return connected

print(TSP(graph))

2. Brute force algorithm:

Fig. 3. TSP solver solution using Brute-Force algorithm



9

Describe:
The Brute Force approach, also known as the Naive Approach, calculates and
compares all possible permutations of routes or paths to determine the shortest
unique solution.
Steps:
 Calculate the total number of routes
 List all the possible routes
 Calculate the distance of each route
 Choose the shortest one
Implementation:
routes = []
def find_paths(node, cities, path, distance):
    path.append(node)
    if len(path) > 1:
        distance += cities[path[-2]][node]
 
 
 
 
 
 

 
 
 
 
 
 


if (len(cities) == len(path)) and (path[0] in cities[path[-1]]):
    global routes
    path.append(path[0])
    distance += cities[path[-2]][path[0]]
    routes.append([distance, path])
    return

    for city in cities:
        if (city not in path) and (node in cities[city]):
            find_paths(city, dict(cities), list(path), distance)
find_paths('A', graph, [], 0)
routes.sort()
print (routes[0][1])


10

3. Ant colony optimization algorithm:

Fig. 4. TSP solver solution using Ant colony optimization algorithm
Describe:
Ants are social insects. They live in colonies. The behavior of ants is controlled
by the goal of searching for food. While searching, ants roaming around their
colonies. An ant repeatedly hops from one place to another to find the food.
While moving, it deposits an organic compound called pheromone on the
ground. Ants communicate with each other via pheromone trails. When an ant
finds some amount of food it carries as much as it can carry. When returning it
deposits pheromone on the paths based on the quantity and quality of the food.
Other ants can smell it and follow that path. The higher the pheromone level has,

a higher probability of choosing that path and the more ants follow the path, the
amount of pheromone will also increase on that path. Ant colony optimization
based on the foraging behavior of an ant for seeking a path between their colony
and source food was first introduced in the 90s by Marco Dorigo.
Steps:
 Each ant generates a solution
 Found paths are compared
 Pheromone is updated


11

Implementation (describe in Colab in this case)


12

CHƯƠNG III - FUTURE SCOPE
This assignment presents application analysis among possible algorithm for
solving TSP. There exists in order to classify which algorithm gives the best
optimal.
In this paper we have described on the basis of total distance travelled and
implement based on properties of these algorithms. In future the comparison can
be done on the basis of performance and cost to see which algorithm gives the
better result.


13

CHƯƠNG IV - REFERENCES

 />





On the Nearest Neighbor Algorithms for the Traveling Salesman Problem |
SpringerLink
Ant colony optimization algorithms - Wikipedia
Ant Colony Optimization - an overview | ScienceDirect Topics
Solving the Travelling Salesman Problem for deliveries (routific.com)


14

PHỤ LỤC
Phần này bao gồm những nội dung cần thiết nhằm minh họa hoặc hỗ trợ cho nội
dung luận văn như số liệu, biểu mẫu, tranh ảnh. . . . nếu sử dụng những câu trả lời cho
một bảng câu hỏi thì bảng câu hỏi mẫu này phải được đưa vào phần Phụ lục ở dạng
nguyên bản đã dùng để điều tra, thăm dị ý kiến; khơng được tóm tắt hoặc sửa đổi.
Các tính tốn mẫu trình bày tóm tắt trong các biểu mẫu cũng cần nêu trong Phụ lục của
luận văn. Phụ lục khơng được dày hơn phần chính của luận văn.



×