def shortest_route_state(): ''' uses dijkstra's algorithm ''' start = raw_input( "Type in the city name you wish to start from or type in -9 to exit\n") while start != '-9' and start not in graph.vertex_dict: print "There is no city by that name." start = raw_input( "Type in the city name you wish to start from or type in -9 to exit\n" ) if start == '-9': exit_state() dest = raw_input( "Type in the city name you wish to end at or type in -9 to exit\n") while dest != '-9' and dest not in graph.vertex_dict: print "There is no city by that name." dest = raw_input( "Type in the city name you wish to end at or type in -9 to exit\n") if dest == '-9': exit_state() route_list = calculations.shortest_path(graph, start, dest) print "\nThe shortest route is", route_list print "The total distance of your route is", calculations.calculate_dist( graph, route_list), "kilometers" print "The total cost of your route is", calculations.calculate_cost( graph, route_list), "dollars" print "The total time of your route is", calculations.calculate_time( graph, route_list), "minutes\n" start_state()
def route_info_state(): ''' get a route from the user. Store it in a list. Then calculate cost, distance, time ''' route_list = [] input_text = raw_input( "Type in the city you wish to start from or -9 to exit\n") while input_text not in graph.vertex_dict and input_text != '-9': print "Sorry, your request cannot be understood." input_text = raw_input( "Type in the city you wish to start form or -9 to exit\n") if input_text == '-9': exit_state() route_list.append(input_text) vertices = graph.vertex_dict while 1 > 0: input_text = raw_input( "Type in your following routes one at a time and separate each with a new line. Type in 0 when you're done or -9 to exit\n" ) prev_city_name = route_list[len(route_list) - 1] prev_edges = vertices[prev_city_name].edges while input_text != '0' and input_text != '-9' and ( (input_text not in vertices) or (graph.vertex_dict[input_text].city.code not in prev_edges)): if input_text in vertices and graph.vertex_dict[ input_text].city.code not in prev_edges: print "Sorry, from your previously listed city, you cannot fly to your current selection.\n" print "You can only go to these cities. Please print their full names.\n", prev_edges input_text = raw_input("Please type in a different city\n") continue print "Sorry, your request cannot be understood." input_text = raw_input( "Type in your following routes one at a time and separate each with with a new line. Type in 0 when you're done or -9 to exit\n" ) if input_text == '-9': exit_state() if input_text == '0': break route_list.append(input_text) print "Your current route is", route_list print "Your final route is", route_list print "The total distance of your route is", calculations.calculate_dist( graph, route_list), "kilometers" print "The total cost of your route is", calculations.calculate_cost( graph, route_list), "dollars" print "The total time of your route is", calculations.calculate_time( graph, route_list), "minutes"
def test_calculations(self): ''' using my json file, tests shortest path, cost, distance, and time ''' json_file = open("test.json") json_data = json.load(json_file) json_file.close() self.graph = Graph() self.metros = json_data["metros"] self.routes = json_data["routes"] self.graph = self.graph.construct_json_graph(self.metros, self.routes) self.assertEqual(['cityTwo','cityOne','cityFour'] , calculations.shortest_path(self.graph, 'cityTwo', 'cityFour')) shortest_path = calculations.shortest_path(self.graph, 'cityTwo', 'cityFour') self.assertEqual(['cityTwo', 'cityOne', 'cityFour'] , shortest_path) self.assertEqual(8000, calculations.calculate_dist(self.graph, shortest_path)) self.assertEqual(804.0, calculations.calculate_time(self.graph, shortest_path)) self.assertEqual(2550.0, calculations.calculate_cost(self.graph, shortest_path))
def test_calculations(self): ''' using my json file, tests shortest path, cost, distance, and time ''' json_file = open("test.json") json_data = json.load(json_file) json_file.close() self.graph = Graph() self.metros = json_data["metros"] self.routes = json_data["routes"] self.graph = self.graph.construct_json_graph(self.metros, self.routes) self.assertEqual(['cityTwo', 'cityOne', 'cityFour'], calculations.shortest_path(self.graph, 'cityTwo', 'cityFour')) shortest_path = calculations.shortest_path(self.graph, 'cityTwo', 'cityFour') self.assertEqual(['cityTwo', 'cityOne', 'cityFour'], shortest_path) self.assertEqual( 8000, calculations.calculate_dist(self.graph, shortest_path)) self.assertEqual( 804.0, calculations.calculate_time(self.graph, shortest_path)) self.assertEqual( 2550.0, calculations.calculate_cost(self.graph, shortest_path))
def route_info_state(): ''' get a route from the user. Store it in a list. Then calculate cost, distance, time ''' route_list = [] input_text = raw_input("Type in the city you wish to start from or -9 to exit\n") while input_text not in graph.vertex_dict and input_text != '-9': print "Sorry, your request cannot be understood." input_text = raw_input("Type in the city you wish to start form or -9 to exit\n") if input_text == '-9': exit_state() route_list.append(input_text) vertices = graph.vertex_dict while 1 > 0: input_text = raw_input("Type in your following routes one at a time and separate each with a new line. Type in 0 when you're done or -9 to exit\n") prev_city_name = route_list[len(route_list)-1] prev_edges = vertices[prev_city_name].edges while input_text != '0' and input_text != '-9' and((input_text not in vertices) or (graph.vertex_dict[input_text].city.code not in prev_edges)): if input_text in vertices and graph.vertex_dict[input_text].city.code not in prev_edges: print "Sorry, from your previously listed city, you cannot fly to your current selection.\n" print "You can only go to these cities. Please print their full names.\n",prev_edges input_text = raw_input("Please type in a different city\n") continue print "Sorry, your request cannot be understood." input_text = raw_input("Type in your following routes one at a time and separate each with with a new line. Type in 0 when you're done or -9 to exit\n") if input_text == '-9': exit_state() if input_text == '0': break route_list.append(input_text) print "Your current route is",route_list print "Your final route is",route_list print "The total distance of your route is",calculations.calculate_dist(graph, route_list), "kilometers" print "The total cost of your route is", calculations.calculate_cost(graph, route_list), "dollars" print "The total time of your route is", calculations.calculate_time(graph, route_list), "minutes"
def shortest_route_state(): ''' uses dijkstra's algorithm ''' start = raw_input("Type in the city name you wish to start from or type in -9 to exit\n") while start != '-9' and start not in graph.vertex_dict: print "There is no city by that name." start = raw_input("Type in the city name you wish to start from or type in -9 to exit\n") if start == '-9': exit_state() dest = raw_input("Type in the city name you wish to end at or type in -9 to exit\n") while dest != '-9' and dest not in graph.vertex_dict: print "There is no city by that name." dest = raw_input("Type in the city name you wish to end at or type in -9 to exit\n") if dest == '-9': exit_state() route_list = calculations.shortest_path(graph, start, dest) print "\nThe shortest route is", route_list print "The total distance of your route is", calculations.calculate_dist(graph, route_list), "kilometers" print "The total cost of your route is", calculations.calculate_cost(graph, route_list), "dollars" print "The total time of your route is", calculations.calculate_time(graph, route_list), "minutes\n" start_state()