diff --git a/ov9/program1.py b/ov9/program1.py index 37434f29fcc40e3658a6ff98d678a8537d09524e..b419bf76a604ec4d28db41fdaeab52cbc4cbc7a2 100644 --- a/ov9/program1.py +++ b/ov9/program1.py @@ -75,32 +75,40 @@ def init_table(): for i in range(num_of_nodes): res_arr.append(results(i, max_num.time+1, False, None)) -path=[] -def rec_func(node, counter, global_fra, end_switch): - if not end_switch: - counter += node.time - if node.node == global_fra: - return counter - else: - node = [a for a in res_arr if a.node == node.prev][0] - return rec_func(node, counter, global_fra, end_switch) - elif end_switch: - counter += node.time +# def rec_func(node, counter, global_fra, end_switch): +# if not end_switch: +# counter += node.time +# if node.node == global_fra: +# return counter +# else: +# node = [a for a in res_arr if a.node == node.prev][0] +# return rec_func(node, counter, global_fra, end_switch) +# elif end_switch: +# counter += node.time +# path.append(node.node) +# if node.node == global_fra: +# return counter, path +# else: +# node = [a for a in res_arr if a.node == node.prev][0] +# return rec_func(node, counter, global_fra, end_switch) + + +def rec_func2(node, counter, global_fra): + path=[] + while node.node != global_fra: + counter +=node.time path.append(node.node) - if node.node == global_fra: - return counter, path - else: - node = [a for a in res_arr if a.node == node.prev][0] - return rec_func(node, counter, global_fra, end_switch) + node = [a for a in res_arr if a.node == node.prev][0] + path.append(node.node) + return counter,path def calculate_shortest_path(current_node, time, current_neighbour, slutt): - end_switch = False count_a = 0 - count_a = rec_func(current_neighbour, count_a, slutt, end_switch) + count_a, _ = rec_func2(current_neighbour, count_a, slutt) count_b = 0 - count_b = rec_func(current_node, count_b, slutt, end_switch ) + count_b, _ = rec_func2(current_node, count_b, slutt) count_b += time if count_b < count_a: return True @@ -148,8 +156,7 @@ def dijkstras(slutt, fra, start): #not finished end_node = [b for b in res_arr if b.node == slutt][0] total_time = 0 - end_switch = True - total_time,travel_arr = rec_func(end_node, total_time, start, end_switch) + total_time,travel_arr = rec_func2(end_node, total_time, start) travel_arr = reversed(travel_arr) print(f'total time: {total_time}\nshortest path: {"->".join(str(v) for v in travel_arr)}') exit() @@ -171,7 +178,7 @@ def init_things(): if sys.argv[1] == '-d': #node_id start = 20 - slutt = 27 + slutt = 25 init_table() queue.append(start) start_node = [a for a in res_arr if a.node == start][0] diff --git a/ov9/test_files/set_test.py b/ov9/test_files/set_test.py new file mode 100644 index 0000000000000000000000000000000000000000..7a0fbb4a192df4fb0809aee868d6a8f1eb696eb0 --- /dev/null +++ b/ov9/test_files/set_test.py @@ -0,0 +1,199 @@ +import sys + +class int_points (object): + def __init__(self, id, type, name): + self.id = id + self.type = type + self.name = name + +class nodes (object): + def __init__(self, node, bredde, lengde): + self.node = node + self.bredde = bredde + self.lengde = lengde + +class edges (object): + def __init__(self, fra, til, time, lenght, speed): + self.fra = fra + self.til = til + self.time = time + self.lenght = lenght + self.speed = speed + +class results(object): + def __init__(self, node, time, visited, prev): + self.node = node + self.time = time + self.visited = visited + self.prev = prev + + +#prep nodes +with open('./noder.txt','r') as file: + node_file = file.read() + +node_file = node_file.split() +num_of_nodes = int(node_file[0]) +node_file.pop(0) + +node_arr = set() +for i in range(0, len(node_file), 3): + #nodenr breddegrad lengdegrad + node_arr.add(nodes(int(node_file[i]), float(node_file[i+1]), float(node_file[i+2]))) + +#prep edges +with open('./kanter.txt','r') as file: + edge_file = file.read() + +edge_file = edge_file.split() +num_of_edges = int(edge_file[0]) +edge_file.pop(0) + +edge_arr = set() +for i in range(0, len(edge_file), 5): + #franode tilnode kjøretid lengde fartsgrense + edge_arr.add(edges(int(edge_file[i]), int(edge_file[i+1]), int(edge_file[i+2]), int(edge_file[i+3]), int(edge_file[i+4]))) + +#prep intresting +with open('./interessepkt.txt','r') as file: + intresting_file = file.read() + +intresting_file = intresting_file.split('\n') +num_of_intresting = int(intresting_file[0]) +intresting_file.pop(0) + +intresting_arr = set() +for i in range(len(intresting_file)): + #nodenr kode "Navn på stedet" + tmp_values = intresting_file[i].split() + intresting_arr.add(int_points(int(tmp_values[0]), int(tmp_values[1]), ' '.join(tmp_values[2:]))) + + +max_num = max(edge_arr,key=lambda item:item.time) +res_arr = set() +def init_table(): + for i in range(num_of_nodes): + res_arr.add(results(i, max_num.time+1, False, None)) + +# def rec_func(node, counter, global_fra, end_switch): +# if not end_switch: +# counter += node.time +# if node.node == global_fra: +# return counter +# else: +# node = [a for a in res_arr if a.node == node.prev][0] +# return rec_func(node, counter, global_fra, end_switch) +# elif end_switch: +# counter += node.time +# path.append(node.node) +# if node.node == global_fra: +# return counter, path +# else: +# node = [a for a in res_arr if a.node == node.prev][0] +# return rec_func(node, counter, global_fra, end_switch) + + +def rec_func2(node, counter, global_fra): + path=[] + while node.node != global_fra: + counter +=node.time + path.append(node.node) + node = [a for a in res_arr if a.node == node.prev][0] + path.append(node.node) + return counter,path + + + +def calculate_shortest_path(current_node, time, current_neighbour, slutt): + count_a = 0 + count_a, _ = rec_func2(current_neighbour, count_a, slutt) + count_b = 0 + count_b, _ = rec_func2(current_node, count_b, slutt) + count_b += time + if count_b < count_a: + return True + else: + return False + + + +def update_que(que): + tmp_arr = [a for a in res_arr if a.node in que] + tmp_arr.sort(key=lambda x: x.time, reverse=True) + que = [a.node for a in tmp_arr if a in tmp_arr] + return que + +end_que = [] +queue = [] +we_soon_to_be_done = False +def dijkstras(slutt, fra, start): + global we_soon_to_be_done + global queue + + if we_soon_to_be_done: + end_que.pop(0) + current_node = [b for b in res_arr if b.node == fra][0] + for tmp_edge in edge_arr: + if tmp_edge.fra == fra: + current_neighbour = [a for a in res_arr if a.node == tmp_edge.til][0] + if current_neighbour.visited == False and current_neighbour.node not in queue: + current_neighbour.time = tmp_edge.time + current_neighbour.prev = fra + if tmp_edge.til == slutt: + current_node.visited = True + we_soon_to_be_done = True + end_que.append(tmp_edge.til) + else: + if tmp_edge.fra == slutt: + end_que.append(tmp_edge.til) + else: + queue.append(tmp_edge.til) + + elif current_neighbour.node != start: + if calculate_shortest_path(current_node, tmp_edge.time, current_neighbour, start): + current_neighbour.time = tmp_edge.time + current_neighbour.prev = tmp_edge.fra + if we_soon_to_be_done and not len(end_que): + #not finished + end_node = [b for b in res_arr if b.node == slutt][0] + total_time = 0 + total_time,travel_arr = rec_func2(end_node, total_time, start) + travel_arr = reversed(travel_arr) + print(f'total time: {total_time}\nshortest path: {"->".join(str(v) for v in travel_arr)}') + exit() + else: + if we_soon_to_be_done: + current_node.visited = True + dijkstras(slutt,end_que[0],start) + else: + queue.pop(0) + current_node.visited = True + queue = update_que(queue) + dijkstras(slutt,queue[0],start) + + + + +def init_things(): + if len(sys.argv) > 1: + if sys.argv[1] == '-d': + #node_id + start = 20 + slutt = 30 + init_table() + queue.append(start) + start_node = [a for a in res_arr if a.node == start][0] + start_node.time = 0 + dijkstras(slutt, start, start) + elif sys.argv[1] == '-a': + print('A* not yet implemented') + else: + print('Unknown argument') + else: + print('Missing argument') + +init_things() + + + +