def cost_list_generator(G, graph_ordered_dict, car_spacing=5): """This function takes updated pos_lists at the end of each iteration and converts them to cost_lists""" nodes = list(G.nodes()) cost_lists = [[0, 0, 0, 0] for i in repeat(None, len(nodes))] for i in range(len(nodes)): node = nodes[i] up, down, right, left = nearby_nodes_from_ordered_dict( graph_ordered_dict, node) if not (down == 'False'): cost_lists[i][0] = cost_list_from_pos_list( G[down][node]['pos_list'], G[down][node]['weight'], car_spacing) if not (up == 'False'): cost_lists[i][1] = cost_list_from_pos_list(G[up][node]['pos_list'], G[up][node]['weight'], car_spacing) if not (left == 'False'): cost_lists[i][2] = cost_list_from_pos_list( G[left][node]['pos_list'], G[left][node]['weight'], car_spacing) if not (right == 'False'): cost_lists[i][3] = cost_list_from_pos_list( G[right][node]['pos_list'], G[right][node]['weight'], car_spacing) return cost_lists
def traffic_sim(graph_ordered_dict, G, fs, modes, time_interval): nodes = list(G.nodes()) for j in range(0, len(nodes)): node = nodes[j] mode = modes[j] f = fs[j] up, down, right, left = nearby_nodes_from_ordered_dict( graph_ordered_dict, node) # move_cars(pos_list, distance, weight, signal_bool, f_list, car_spacing=5) # a = [pop_list_straight, pop_list_right, pop_list_left], [pos_list_straight, pos_list_aut], [straight, right, left] TRUE case # [[], [], pop_list], [pos_list, pos_list], [0, 0, left] FALSE CASE if mode == 0: sim_thanos_straight(G, node, time_interval, f, up, down, right, left) if mode == 1: sim_thanos_aut(G, node, time_interval, f, up, down, right, left) if mode == 2: sim_thanos_aut(G, node, time_interval, f, down, up, left, right) if mode == 3: sim_thanos_straight(G, node, time_interval, f, right, left, down, up) if mode == 4: sim_thanos_aut(G, node, time_interval, f, right, left, down, up) if mode == 5: sim_thanos_aut(G, node, time_interval, f, left, right, up, down) # current_signals = modes_to_signals(modes) # last_signals_list = [] for i in list(G.edges): G[i[0]][i[1]]['pos_list'] = G[i[0]][i[1]]['pos_list_temp'] + G[i[0]][ i[1]]['pos_list'] # metric(last_signals_list[-1], current_signals[i][1], G[i[0]][i[1]]['pos_list_temp']) # last_signals_list.append(current_signals[:]) G[i[0]][i[1]]['pos_list_stick'] = G[i[0]][i[1]]['pos_list_temp'][:] G[i[0]][i[1]]['pos_list_temp'] = [] # print("G[i[0]][i[1]]['pos_list_temp'] ", G[i[0]][i[1]]['pos_list_temp']) for i in list(G.edges): error_corrector(G[i[0]][i[1]]['pos_list'])
def traffic_init(graph_ordered_dict, G, cost_lists, car_spacing=5): """This function initializes the graph by adding position list and and effective length by using the cost_list.""" """In this loop, we assign effective lengths of each segment based on cost lists. Assume each car in a cost_list takes 5 meters of space.""" # this loop adds the effective length on each segment by using the cost list """Adding cars in the position list. Take the number of cars from the cost list and add them in the position list""" nodes = list(G.nodes()) for i in range(len(nodes)): node = nodes[i] up, down, right, left = nearby_nodes_from_ordered_dict(graph_ordered_dict, node) if not (up == 'False'): G[node][up]['pos_list'] = pos_list_gen(G[node][up]['weight'], cost_lists[up][0], car_spacing) G[up][node]['pos_list'] = pos_list_gen(G[up][node]['weight'], cost_lists[node][1], car_spacing) G[node][up]['effective'] = G[node][up]['weight'] - cost_lists[up][0] * car_spacing G[up][node]['effective'] = G[up][node]['weight'] - cost_lists[node][1] * car_spacing if not (down == 'False'): G[node][down]['pos_list'] = pos_list_gen(G[node][down]['weight'], cost_lists[down][1], car_spacing) G[down][node]['pos_list'] = pos_list_gen(G[down][node]['weight'], cost_lists[node][0], car_spacing) G[node][down]['effective'] = G[node][down]['weight'] - cost_lists[down][1] * car_spacing G[down][node]['effective'] = G[down][node]['weight'] - cost_lists[node][0] * car_spacing if not (right == 'False'): G[node][right]['pos_list'] = pos_list_gen(G[node][right]['weight'], cost_lists[right][2], car_spacing) G[right][node]['pos_list'] = pos_list_gen(G[right][node]['weight'], cost_lists[node][3], car_spacing) G[node][right]['effective'] = G[node][right]['weight'] - cost_lists[right][2] * car_spacing G[right][node]['effective'] = G[right][node]['weight'] - cost_lists[node][3] * car_spacing if not (left == 'False'): G[node][left]['pos_list'] = pos_list_gen(G[node][left]['weight'], cost_lists[left][3], car_spacing) G[left][node]['pos_list'] = pos_list_gen(G[left][node]['weight'], cost_lists[node][2], car_spacing) G[node][left]['effective'] = G[node][left]['weight'] - cost_lists[left][3] * car_spacing G[left][node]['effective'] = G[left][node]['weight'] - cost_lists[node][2] * car_spacing
def node_mode_table(j, node, graph_ordered_dict): # neighbors = nx.neighbors(G, node) # if node[0] up, down, right, left = nearby_nodes_from_ordered_dict(graph_ordered_dict, node) # print("up | down | right | left: ", up, down, right, left) if j == 0: return [I(up, 0), I(up, 1), I(down, 0), I(down, 2)], [up, down, right, left], [0, 1, 0, 2] if j == 1: return [I(up, 1), I(up, 0), I(right, 4), I(right, 3)], [up, down, right, left], [1, 0, 4, 3] if j == 2: return [I(down, 2), I(down, 0), I(left, 5), I(left, 3)], [up, down, right, left], [2, 0, 5, 3] if j == 3: return [I(right, 3), I(right, 4), I(left, 3), I(left, 5)], [up, down, right, left], [3, 4, 3, 5] if j == 4: return [I(right, 4), I(right, 3), I(down, 2), I(down, 0)], [up, down, right, left], [4, 3, 2, 0] if j == 5: return [I(left, 5), I(left, 3), I(up, 1), I(up, 0)], [up, down, right, left], [5, 3, 1, 0]
def q3(G, node_list, dim, graph_ordered_dict, runtime, run, cost_lists, lembda, lembda1=0.7, lembda2=0.3, buff=4): q3 = np.zeros((dim, dim)) cutoff_speed = 0 for i in range(len(node_list)): q1_coeff_ = q1_coeff(cost_lists[i]) # q1_coeff_ = [1,1,1,1,1,1] node = node_list[i] up, down, right, left = nearby_nodes_from_ordered_dict(graph_ordered_dict, node) nearby_nodes_list = [up, down, right, left] distance_list = [] speed_list = [] # t_list = [] runtime_list = [] # print("RUNTIME: ", runtime) for k in range(len(nearby_nodes_list)): # print('Node', 'nearby_nodes_list', node, nearby_nodes_list[k]) if nearby_nodes_list[k] == 'False': distance_list.append(0) speed_list.append(0) # t_list.append(0) runtime_list.append(0) else: distance_list.append(G[node][nearby_nodes_list[k]]['weight']) speed_list.append(G[node][nearby_nodes_list[k]]['speed']) # t_list.append(distance_list[k]/speed_list[k]) runtime_list.append(int(round((distance_list[k] / speed_list[k]) / runtime))) for i_ in nearby_nodes_list: rel = nearby_nodes_list.index(i_) if i_ != 'False': nearby_nodes_x = nearby_nodes_from_ordered_dict(graph_ordered_dict, i_) x = nearby_nodes_x[rel] if i_ != 'False' and x != 'False': # if G[i][i_]['speed'] >= cutoff_speed: if G[i_][x]['speed'] >= cutoff_speed: # print("ohahahajuuuu Allah uhhhhhhhh") for j in range(6): nm_table = node_mode_table(j, node, graph_ordered_dict) nm_list = nm_table[0] # nearby_nodes_list = nm_table[1] j_prime = nm_table[2] I_ = I(i, j) # print("nm_table: ", nm_table) # print("I_", I_) # print("nm_list: ", nm_list) # print("RUNTIME LIST UHHHHH: ", runtime_list) if runtime_list[0] != 0 and run != 0: # print("\nruntime_list[0]: ", runtime_list[0]) # print("run: ", run) if runtime_list[0] > run: if runtime_list[0] - run <= buff: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[0] - run)) if not (I_ == 'False' or nm_list[0] == 'False'): q3[I_][nm_list[0]] -= lembda1 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[0]])[j_prime[0]]) if runtime_list[0] <= run: if run % runtime_list[0] <= buff: if not (I_ == 'False' or nm_list[0] == 'False'): q3[I_][nm_list[0]] -= lembda1 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[0]])[j_prime[0]]) if runtime_list[1] != 0 and run != 0: # print("\nruntime_list[1]: ", runtime_list[1]) # print("run: ", run) if runtime_list[1] > run: if runtime_list[1] - run <= buff: # or run % runtime_list[1] <= 4: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[1] - run)) if not (I_ == 'False' or nm_list[1] == 'False'): q3[I_][nm_list[1]] -= lembda2 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[1]])[j_prime[1]]) if runtime_list[1] <= run: if run % runtime_list[1] <= buff: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[1] - run)) if not (I_ == 'False' or nm_list[1] == 'False'): q3[I_][nm_list[1]] -= lembda2 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[1]])[j_prime[1]]) if runtime_list[2] != 0 and run != 0: # print("\nruntime_list[2]: ", runtime_list[2]) # print("run: ", run) if runtime_list[2] > run: if runtime_list[2] - run <= buff: # or run % runtime_list[2] <= 4: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[2] - run)) if not (I_ == 'False' or nm_list[2] == 'False'): q3[I_][nm_list[2]] -= lembda1 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[2]])[j_prime[2]]) if runtime_list[2] <= run: if run % runtime_list[2] <= buff: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[2] - run)) if not (I_ == 'False' or nm_list[2] == 'False'): q3[I_][nm_list[2]] -= lembda1 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[2]])[j_prime[2]]) if runtime_list[3] != 0 and run != 0: # print("\nruntime_list[3]: ", runtime_list[3]) # print("run: ", run) if runtime_list[3] > run: if runtime_list[3] - run <= buff: # run % runtime_list[3] <= 4: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[3] - run)) if not (I_ == 'False' or nm_list[3] == 'False'): q3[I_][nm_list[3]] -= lembda2 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[3]])[j_prime[3]]) if runtime_list[3] <= run: if run % runtime_list[3] <= buff: # print("PERCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO ", abs(runtime_list[3] - run)) if not (I_ == 'False' or nm_list[3] == 'False'): q3[I_][nm_list[3]] -= lembda2 * (q1_coeff_[j] + q1_coeff(cost_lists[nearby_nodes_list[3]])[j_prime[3]]) return lembda * q3
def metric_q3_bad(G, graph_ordered_dict_, mode, cost_list, b): """This function returns a parameter that quantifies how well Q3 is working - namely, how many cars pass through signals unhindered""" badness = 0 up, down, right, left = nearby_nodes_from_ordered_dict( graph_ordered_dict_, b) if mode == 0: if right != 'False': badness += G[b][right]['speed'] * 0.7 * cost_list[2] if down != 'False': badness += G[b][down]['speed'] * 0.3 * cost_list[2] if left != 'False': badness += G[b][left]['speed'] * 0.7 * cost_list[3] if up != 'False': badness += G[b][up]['speed'] * 0.3 * cost_list[3] if mode == 1: if down != 'False': badness += G[b][down]['speed'] * 0.7 * cost_list[1] badness += G[b][down]['speed'] * 0.3 * cost_list[2] if left != 'False': badness += G[b][left]['speed'] * 0.3 * cost_list[1] badness += G[b][left]['speed'] * 0.7 * cost_list[3] if right != 'False': badness += G[b][right]['speed'] * 0.7 * cost_list[2] if up != 'False': badness += G[b][up]['speed'] * 0.3 * cost_list[3] if mode == 2: if up != 'False': badness += G[b][up]['speed'] * 0.7 * cost_list[0] badness += G[b][up]['speed'] * 0.3 * cost_list[3] if right != 'False': badness += G[b][right]['speed'] * 0.3 * cost_list[0] badness += G[b][right]['speed'] * 0.7 * cost_list[2] if down != 'False': badness += G[b][down]['speed'] * 0.3 * cost_list[2] if left != 'False': badness += G[b][left]['speed'] * 0.7 * cost_list[3] if mode == 3: if up != 'False': badness += G[b][up]['speed'] * 0.7 * cost_list[0] if right != 'False': badness += G[b][right]['speed'] * 0.3 * cost_list[0] if down != 'False': badness += G[b][down]['speed'] * 0.7 * cost_list[1] if left != 'False': badness += G[b][left]['speed'] * 0.3 * cost_list[1] if mode == 4: if up != 'False': badness += G[b][up]['speed'] * 0.7 * cost_list[0] badness += G[b][up]['speed'] * 0.3 * cost_list[3] if right != 'False': badness += G[b][right]['speed'] * 0.3 * cost_list[0] if down != 'False': badness += G[b][down]['speed'] * 0.7 * cost_list[1] if left != 'False': badness += G[b][left]['speed'] * 0.3 * cost_list[1] badness += G[b][left]['speed'] * 0.7 * cost_list[3] if mode == 5: if up != 'False': badness += G[b][up]['speed'] * 0.7 * cost_list[0] if right != 'False': badness += G[b][right]['speed'] * 0.3 * cost_list[0] badness += G[b][right]['speed'] * 0.7 * cost_list[2] if down != 'False': badness += G[b][down]['speed'] * 0.7 * cost_list[1] badness += G[b][down]['speed'] * 0.3 * cost_list[2] if left != 'False': badness += G[b][left]['speed'] * 0.3 * cost_list[1] return badness