-
Notifications
You must be signed in to change notification settings - Fork 1
/
DFS_Modified.py
186 lines (154 loc) · 5.19 KB
/
DFS_Modified.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#This implementation shows the minimum number of stops that the salesman must take
#It does not take the distance into consideration
#This is a strict implementation of the BFS algorithm
#Import Libraries
from collections import deque
import math
from matplotlib import style
import matplotlib.pyplot as plt
#Initialize variables
x = []
y = []
stack = deque()
#Used to store which cities are reachable from each city
cityGraph = {
1 : [2, 3, 4],
2 : [3],
3 : [4, 5],
4 : [5, 6, 7],
5 : [7, 8],
6 : [8],
7 : [9, 10],
8 : [9, 10, 11],
9 : [11],
10 : [11],
11 : []
}
############################################################################
def read_datafile(path):
#Import data file
i = 0
x = []
y = []
with open((path), "r") as file:
for line in file:
split_line = line.strip().split(" ")
#Track line number to remove header info
if i > 6:
#Populate x,y coordinate pairs into arrays
x.append(float(split_line[1]))
y.append(float(split_line[2]))
#increment line counter
i += 1
return x, y
########################################
#graph sets of xy coordinates
def graph_coords(x, y, x2, y2, min_dist):
#Define graph style
style.use('dark_background')
# plotting the points
plt.plot(x, y,'ro', label="Non-Visited Vertices")
plt.plot(x2, y2,'yo-', label="Optimum Path")
for i in range(len(x2)):
plt.annotate((len(x2) - (i + 1)), (x2[i], y2[i]), textcoords="offset points", xytext=(0,5), ha = 'center')
# naming the axes
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.legend()
# giving a title to my graph
plt.title(("Optimum Distance : " + str(min_dist)))
# function to show the plot
plt.pause(.05)
plt.show()
return
########################################
def DFS_modified(stack):
while(stack):
currCity = stack[-1]
#currCity.firstVisited = time
cityArrSorted.append(currCity)
#time += 1
#print(currCity.name)
for city in currCity.cities: #Grab left most element from stack
#Check if the new distance to the city is better than the old best distance
distance = calculate_dist(currCity, cityArr[city - 1])
#print(distance)
#if a better distance is found to the city, add it to the array to update paths to next cities if better
#store best distance and predecessor for backtracking
if(cityArr[city - 1].distance > distance):
#print(cityArr[city - 1].name)
stack.append(cityArr[city - 1])
cityArr[city - 1].distance = distance
cityArr[city - 1].previous = currCity.name
DFS_modified(stack)
#else ignore
#currCity.lastVisited = time
cityArrSorted.append(currCity)
if(stack):
stack.pop()
#time += 1
#return time
########################################
def backtrack(cityArr):
i = 11
out = []
out.append(i)
xcoords = []
xcoords.append(cityArr[i - 1].x)
ycoords = []
ycoords.append(cityArr[i - 1].y)
while(cityArr[i - 1].previous != 0):
i = cityArr[i - 1].previous
out.append(i)
xcoords.append(cityArr[i - 1].x)
ycoords.append(cityArr[i - 1].y)
return xcoords, ycoords, out
########################################
def calculate_dist(city1, city2):
dist = city1.distance
dist = dist + (math.hypot(city1.x - city2.x, city1.y - city2.y))
return dist
########################################
class city():
def __init__(self, name, x, y, cities):
self.name = name
self.x = x
self.y = y
self.cities = cities
self.previous = 0
self.distance = math.inf
############################################################################
#######
#INPUT
######
#data file path
file_path = str(r'C:\Users\burkh\OneDrive\Desktop\AI\Project2\nodes.tsp')
#used to read and parse the tsp file
x, y = read_datafile(file_path)
#############
#FORMAT DATA
############
#Initalize and store array of cities
cityArr = []
cityArrSorted = []
for i in range(11):
c = city(i+1, x[i], y[i], cityGraph[i+1])
cityArr.append(c)
############
#PROCESSING
###########
#Initalize Stack with first city
cityArr[0].distance = 0 #Set distance to 0 - measuring distance from city 1
stack.append(cityArr[0]) #Starting at node 1 = cityArr[0]
#Run BFS to update city array
DFS_modified(stack)
########
#OUTPUT
#######
#list path taken
sequence = []
x1, y1, sequence = backtrack(cityArr)
#Graph Path/ Unused Points
graph_coords(x, y, x1, y1, cityArr[10].distance)
print("Distance to city 11: " + str(cityArr[10].distance))
#print("Path: " + str((path)).strip('[]'))