-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps2.py
294 lines (250 loc) · 11.4 KB
/
ps2.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# 6.100B Problem Set 2 Spring 2023
# Graph Optimization
# Name: Vivian Hir
# Collaborators:
# Problem Set 2
# =============
# Finding shortest paths to drive from home to work on a road network
from graph import DirectedRoad, Node, RoadMap
# PROBLEM 2: Building the Road Network
#
# PROBLEM 2.1: Designing your Graph
#
# What do the graph's nodes represent in this problem? What
# do the graph's edges represent? Where are the times
# represented?
#
# Write your answer below as a comment:
# The graph's nodes are the locations
# The graph's edges are the types of paths
#
#
# PROBLEM 2.2: Implementing create_graph
def create_graph(map_filename):
"""
Parses the map file and constructs a road map (graph).
Travel time and traffic multiplier should be each cast to a float.
Parameters:
map_filename : str
Name of the map file.
Assumes:
Each entry in the map file consists of the following format, separated by spaces:
source_node destination_node travel_time road_type traffic_multiplier
Note: hill road types always are uphill in the source to destination direction and
downhill in the destination to the source direction. Downhill travel takes
1/4 as long as uphill travel. The travel_time represents the time to travel
from source to destination (uphill).
e.g.
N0 N1 10 highway 1
This entry would become two directed roads; one from 'N0' to 'N1' on a highway with
a weight of 10.0, and another road from 'N1' to 'N0' on a highway using the same weight.
e.g.
N2 N3 7 uphill 2
This entry would become two directed roads; one from 'N2' to 'N3' on a hill road with
a weight of 7.0, and another road from 'N3' to 'N2' on a hill road with a weight of 1.75.
Note that the directed roads created should have both type 'hill', not 'uphill'!
Returns:
RoadMap
A directed road map representing the given map.
"""
file=open(map_filename, "r")
final_list=[]
formatted_list=[]
#initialize variables
while file:
line=file.readline()
final_list.append(line)
if line =="":
break
file.close()
final_list=final_list[:-1]
for path in final_list:
new_path=path.replace('\n', '')
new_path=new_path.split()
formatted_list.append(new_path)
#open file and add path to a formatted list
new_map=RoadMap()
for obj in formatted_list:
source_node=Node(obj[0])
dest_node=Node(obj[1]) #create source node and destination node
boolean_value_one=new_map.contains_node(source_node)
boolean_value_two=new_map.contains_node(dest_node)
if boolean_value_one is False:
new_map.insert_node(source_node)
if boolean_value_two is False:
new_map.insert_node(dest_node)
#string needs to be a node
if obj[3]=="uphill":
obj[3]="hill" #redefine as a hill type
new_road=DirectedRoad(source_node, dest_node,float(obj[2]), str(obj[3]), float(obj[4]))
another_road=DirectedRoad(dest_node, source_node, float(obj[2])/4, str(obj[3]), float(obj[4])) #divide by 4 for time
else:
new_road=DirectedRoad(source_node, dest_node,float(obj[2]), str(obj[3]), float(obj[4]))
another_road=DirectedRoad(dest_node, source_node, float(obj[2]), str(obj[3]), float(obj[4])) #default if not hill
new_map.insert_road(new_road)
new_map.insert_road(another_road)
return new_map
# PROBLEM 2.3: Testing create_graph
#
# Go to the bottom of this file, look for the section under FOR PROBLEM 2.3,
# and follow the instructions in the handout.
# PROBLEM 3: Finding the Shortest Path using Depth-First Search
# Problem 3.1: Objective function
#
# What is the objective function for this problem? What are the constraints?
#
# Answer:
# The objective function for this problem is to find the path providing the shortest travel time
# The constraint is that the path traveled cannot included restricted roads
#
# PROBLEM 3.2: Implement find_shortest_path
def find_shortest_path(roadmap, start, end, restricted_roads=None, has_traffic=False):
"""
Finds the shortest path between start and end nodes on the road map,
without using any restricted roads, following traffic conditions.
If restricted_roads is None, assume there are no restricted roads.
Use the depth first search algorithm (DFS) from 6.100B lecture 3.
Parameters:
roadmap: RoadMap
The graph on which to carry out the search.
start: Node
Node at which to start.
end: Node
Node at which to end.
restricted_roads: list of str or None
Road Types not allowed on path. If None, all are roads allowed
has_traffic: bool
Flag to indicate whether to get shortest path during traffic or not.
Returns:
A two element tuple of the form (best_path, best_time).
The first item is a list of Nodes, the shortest path from start to end.
The second item is a float, the length (time traveled) of the best path.
If there exists no path that satisfies constraints, then return None.
"""
start_boolean=roadmap.contains_node(start)
end_boolean=roadmap.contains_node(end)
if not start_boolean:
return None
if not end_boolean:
return None
if start==end:
return ([start], 0)
stack=[([start],0)]
best_time=float("inf") #initialize to infinity
best_path=[]
while stack:
current_path= stack.pop()
#check if last node in the path is the end node
end_node=current_path[0][-1]
travel_time=current_path[1]
if end_node==end:
if travel_time<best_time:
best_time=travel_time
best_path=current_path[0]
#if distance for this path is shorter than the current distance
#update the distance
if restricted_roads is not None:
roads=roadmap.get_reachable_roads_from_node(current_path[0][-1], restricted_roads) #last node of the path
else:
roads=roadmap.get_reachable_roads_from_node(current_path[0][-1], []) #search for an empty list
for road in roads:
neighbor=road.get_destination_node()
travel_time=road.get_travel_time(has_traffic)
if neighbor not in current_path[0]:
stack.append((current_path[0]+[neighbor],current_path[1]+travel_time))
if best_path==[]:
return None
return (best_path, best_time)
#sets work by hashing stuff (give it 5, puts it at a given address determine by some function h )
#can only put it at a given address if it is immutable. If you give it a list, change the list now it's a different list
#then you can't go to the list so it has to be unchangeable
#want to check if current_path end node is the end
#keep track of the best path and the best distance
#add the last node's neighbors update the path then add it back to the stack
#increase the distance by the length of the edge and time.
#keep the distance with the path.
#path as a list, distance is float (path, distance)
# PROBLEM 4.1: Implement find_shortest_path_no_traffic
def find_shortest_path_no_traffic(filename, start, end):
"""
Finds the shortest path from start to end during conditions of no traffic.
Assume there are no restricted roads.
You must use find_shortest_path.
Parameters:
filename: str
Name of the map file that contains the graph
start: Node
Node object at which to start.
end: Node
Node object at which to end.
Returns:
A two element tuple of the form (best_path, best_time).
The first item is a list of Nodes, the shortest path from start to end with no traffic.
The second item is a float, the length (time traveled) of the best path.
If there exists no path that satisfies constraints, then return None.
"""
roadmap=create_graph(filename)
shortest_path=find_shortest_path(roadmap, start, end)
return shortest_path
# PROBLEM 4.2: Implement find_shortest_path_restricted
def find_shortest_path_restricted(filename, start, end):
"""
Finds the shortest path from start to end when local roads and hill roads cannot be used.
Assume no traffic.
You must use find_shortest_path.
Parameters:
filename: str
Name of the map file that contains the graph
start: Node
Node object at which to start.
end: Node
Node object at which to end.
Returns:
A two element tuple of the form (best_path, best_time).
The first item is a list of Nodes, the shortest path from start to end given the aforementioned conditions.
The second item is a float, the length (time traveled) of the best path.
If there exists no path that satisfies constraints, then return None.
"""
roadmap=create_graph(filename)
restricted_roads=["local", "hill"]
#should delete the roads containing this type from roadmap
shortest_path=find_shortest_path(roadmap, start, end, restricted_roads)
return shortest_path
# PROBLEM 4.3: Implement find_shortest_path_in_traffic
def find_shortest_path_in_traffic(filename, start, end):
"""
Finds the shortest path from start to end in traffic,
i.e. when all roads' travel times are multiplied by their traffic multipliers.
You must use find_shortest_path.
Parameters:
filename: str
Name of the map file that contains the graph
start: Node
Node object at which to start.
end: Node
Node object at which to end.
Returns:
A two element tuple of the form (best_path, best_time).
The first item is a list of Nodes, the shortest path from start to end given the aforementioned conditions.
The second item is a float, the length (time traveled) of the best path.
If there exists no path that satisfies constraints, then return None.
"""
roadmap=create_graph(filename)
#if has traffic is true then you should multiply each by traffic multiplier when constructing the roadmap cause it affects time
shortest_path=find_shortest_path(roadmap, start, end, has_traffic=True)
return shortest_path
if __name__ == '__main__':
# UNCOMMENT THE LINES BELOW TO DEBUG OR TO EXECUTE PROBLEM 2.3
pass
small_map= create_graph('./maps/road_map.txt')
# # # ------------------------------------------------------------------------
# # # FOR PROBLEM 2.3
road_map = create_graph("maps/test_create_graph.txt")
# print(second_map)
# # ------------------------------------------------------------------------
print(road_map)
# start = Node('N0')
# end = Node('N4')
# restricted_roads = []
# print(find_shortest_path_no_traffic('./maps/small_map.txt', start, end))
#wrote expected and comparing it with result