def update_movement_flows(iter_num): for each_move in flow.movement_flows: flow.movement_flows[each_move] *= (iter_num / (iter_num + 1.0)) for each_move in flow.movement_steps: get_move_flow(each_move) flow.movement_flows[each_move] += (flow.movement_steps[each_move] / (iter_num + 1.0))
def get_movements(self, timeslice): ''' Return the list of movements at timeslice or generate the list if neccessary. The travel time and travel cost is calculated at the same time. ''' if timeslice in self.moves_on_path: return self.moves_on_path[timeslice] # first calculate transfer cost total_travel_cost = self.num_transfer * conf.ALPHA_tran # then generate memoization for movements on path, and calculate other travel costs self.moves_on_path[timeslice] = [] self.path_travel_time[timeslice] = 0.0 timeline = timeslice for each_edge in self.edges_on_path: if isinstance(each_edge.related_vector, TransitLine): # if the edge is a transit line line = each_edge.related_vector (arrival_time, wait_time) = line.calc_arrival_time(slice2min(timeline), each_edge.head_node, each_edge.tail_node) # when there is no public transport service at timeslice, return infinite travel time/cost if arrival_time == float('inf') or wait_time == float('inf'): self.path_travel_timeslice[timeslice] = float('inf') self.path_travel_cost[timeslice] = float('inf') return [] in_vehicle_time = arrival_time - slice2min( timeline) - wait_time # create transit line move line_move = Move(timeline + min2slice(wait_time), each_edge) self.moves_on_path[timeslice].append(line_move) timeline = min2slice(arrival_time) # calculate travel cost wait_cost = wait_time * conf.ALPHA_wait move_flow = get_move_flow(line_move) in_vehicle_cost = line.calc_travel_cost( in_vehicle_time, move_flow) total_travel_cost = total_travel_cost + in_vehicle_cost + wait_cost else: # if the edge is NOT a transit line each_vector = each_edge.related_vector next_move = Move(timeline, each_edge) self.moves_on_path[timeslice].append(next_move) move_flow = get_move_flow(next_move) travel_time = each_vector.calc_travel_time(move_flow) travel_cost = each_vector.calc_travel_cost(travel_time) timeline = timeline + min2slice(travel_time) total_travel_cost = total_travel_cost + travel_cost self.path_travel_time[timeslice] += travel_time self.path_travel_timeslice[timeslice] = timeline - timeslice self.path_travel_cost[timeslice] = total_travel_cost return self.moves_on_path[timeslice]
def get_movements(self, timeslice): ''' Return the list of movements at timeslice or generate the list if neccessary. The travel time and travel cost is calculated at the same time. ''' if timeslice in self.moves_on_path: return self.moves_on_path[timeslice] # first calculate transfer cost total_travel_cost = self.num_transfer * conf.ALPHA_tran # then generate memoization for movements on path, and calculate other travel costs self.moves_on_path[timeslice] = [] self.path_travel_time[timeslice] = 0.0 timeline = timeslice for each_edge in self.edges_on_path: if isinstance(each_edge.related_vector, TransitLine): # if the edge is a transit line line = each_edge.related_vector (arrival_time, wait_time) = line.calc_arrival_time( slice2min(timeline), each_edge.head_node, each_edge.tail_node) # when there is no public transport service at timeslice, return infinite travel time/cost if arrival_time == float('inf') or wait_time == float('inf'): self.path_travel_timeslice[timeslice] = float('inf') self.path_travel_cost[timeslice] = float('inf') return [] in_vehicle_time = arrival_time - slice2min(timeline) - wait_time # create transit line move line_move = Move(timeline + min2slice(wait_time), each_edge) self.moves_on_path[timeslice].append(line_move) timeline = min2slice(arrival_time) # calculate travel cost wait_cost = wait_time * conf.ALPHA_wait move_flow = get_move_flow(line_move) in_vehicle_cost = line.calc_travel_cost(in_vehicle_time, move_flow) total_travel_cost = total_travel_cost + in_vehicle_cost + wait_cost else: # if the edge is NOT a transit line each_vector = each_edge.related_vector next_move = Move(timeline, each_edge) self.moves_on_path[timeslice].append(next_move) move_flow = get_move_flow(next_move) travel_time = each_vector.calc_travel_time(move_flow) travel_cost = each_vector.calc_travel_cost(travel_time) timeline = timeline + min2slice(travel_time) total_travel_cost = total_travel_cost + travel_cost self.path_travel_time[timeslice] += travel_time self.path_travel_timeslice[timeslice] = timeline - timeslice self.path_travel_cost[timeslice] = total_travel_cost return self.moves_on_path[timeslice]
def export_movement_flows(export): print >> export, '\n------ movement flows ------\n' sorted_moves = sorted(flow.movement_flows.keys(), key=repr) max_bus_flow, max_sub_flow,max_hwy_flow, max_ped_flow = \ float('-inf'), float('-inf'), float('-inf'), float('-inf') for each_move in sorted_moves: # print>>export, " %s\t%6.1f" % (each_move, flow.movement_flows[each_move]) if each_move.related_edge.related_vector.capacity == conf.CAPACITY_bus: if max_bus_flow < flow.movement_flows[each_move]: max_bus_flow = flow.movement_flows[each_move] elif each_move.related_edge.related_vector.capacity == conf.CAPACITY_sub: if max_sub_flow < flow.movement_flows[each_move]: max_sub_flow = flow.movement_flows[each_move] elif each_move.related_edge.related_vector.capacity == conf.CAPACITY_ped: if max_ped_flow < flow.movement_flows[each_move]: max_ped_flow = flow.movement_flows[each_move] else: if max_hwy_flow < flow.movement_flows[each_move]: max_hwy_flow = flow.movement_flows[each_move] print >> export, " maximum transit line flows %6.1f png" % (max_bus_flow) print >> export, " maximum subway line flows %6.1f png" % (max_sub_flow) print >> export, " maximum highway flows %6.1f png" % (max_hwy_flow) print >> export, " maximum pedestrian flows %6.1f png" % (max_ped_flow) print >> export, '\n movement flow variables (exceeding capacity) \n' for origin in elem.zone_list: for dest in elem.zone_list: # print>>export, [origin, dest] path = elem.shortest_path[origin][dest] print >> export, "\n%s" % path for timeslice in xrange(min2slice(conf.DAY)): path_travel_timeslice, path_travel_cost = path.calc_travel_impedences( timeslice) print >> export, "[%3d] " % timeslice, print >> export, "%8.2f\t%8.2f\t" % ( path.get_travel_time(timeslice), path_travel_cost) for each_move in path.moves_on_path[timeslice]: get_move_flow(each_move) print >> export, " %s:\t" % each_move, print >> export, "%8.2f / %8.2f" % ( flow.movement_flows[each_move], each_move.related_edge.related_vector.capacity), if flow.movement_flows[ each_move] > each_move.related_edge.related_vector.capacity: print >> export, " !!", print >> export
def calc_total_emission(): total_emission = 0.0 for each_move in flow.movement_flows: move_flow = get_move_flow(each_move) related_vector = each_move.related_edge.related_vector if isinstance(related_vector, Road): travel_time = related_vector.calc_travel_time(move_flow) vehicle_emission = related_vector.calc_vehicle_emission(travel_time) total_emission += vehicle_emission return total_emission
def export_movement_flows(export): print>>export, '\n------ movement flows ------\n' sorted_moves = sorted(flow.movement_flows.keys(), key = repr) max_bus_flow, max_sub_flow,max_hwy_flow, max_ped_flow = \ float('-inf'), float('-inf'), float('-inf'), float('-inf') for each_move in sorted_moves: # print>>export, " %s\t%6.1f" % (each_move, flow.movement_flows[each_move]) if each_move.related_edge.related_vector.capacity == conf.CAPACITY_bus: if max_bus_flow < flow.movement_flows[each_move]: max_bus_flow = flow.movement_flows[each_move] elif each_move.related_edge.related_vector.capacity == conf.CAPACITY_sub: if max_sub_flow < flow.movement_flows[each_move]: max_sub_flow = flow.movement_flows[each_move] elif each_move.related_edge.related_vector.capacity == conf.CAPACITY_ped: if max_ped_flow < flow.movement_flows[each_move]: max_ped_flow = flow.movement_flows[each_move] else: if max_hwy_flow < flow.movement_flows[each_move]: max_hwy_flow = flow.movement_flows[each_move] print>>export, " maximum transit line flows %6.1f png" % (max_bus_flow) print>>export, " maximum subway line flows %6.1f png" % (max_sub_flow) print>>export, " maximum highway flows %6.1f png" % (max_hwy_flow) print>>export, " maximum pedestrian flows %6.1f png" % (max_ped_flow) print>>export, '\n movement flow variables (exceeding capacity) \n' for origin in elem.zone_list: for dest in elem.zone_list: # print>>export, [origin, dest] path = elem.shortest_path[origin][dest] print>>export, "\n%s" % path for timeslice in xrange(min2slice(conf.DAY)): path_travel_timeslice, path_travel_cost = path.calc_travel_impedences(timeslice) print>>export, "[%3d] " % timeslice, print>>export, "%8.2f\t%8.2f\t" % (path.get_travel_time(timeslice), path_travel_cost) for each_move in path.moves_on_path[timeslice]: get_move_flow(each_move) print>>export, " %s:\t" % each_move, print>>export, "%8.2f / %8.2f" % (flow.movement_flows[each_move], each_move.related_edge.related_vector.capacity), if flow.movement_flows[each_move] > each_move.related_edge.related_vector.capacity: print>>export, " !!", print>>export
def calc_total_emission(): total_emission = 0.0 for each_move in flow.movement_flows: move_flow = get_move_flow(each_move) related_vector = each_move.related_edge.related_vector if isinstance(related_vector, Road): travel_time = related_vector.calc_travel_time(move_flow) vehicle_emission = related_vector.calc_vehicle_emission( travel_time) total_emission += vehicle_emission return total_emission