def init_state_flows(init_value): flow.state_flows = {} for comm in enum_commodity(): flow.state_flows[comm] = {} for timeslice in xrange(min2slice(conf.DAY)+1): flow.state_flows[comm][timeslice] = {} for state in enum_state(comm, timeslice): flow.state_flows[comm][timeslice][state] = init_value # initialize the population at timeslice 0 for each zone for comm in enum_commodity(): flow.state_flows[comm][0][comm.init_state] = flow.commodity_steps[comm]
def init_state_flows(init_value): flow.state_flows = {} for comm in enum_commodity(): flow.state_flows[comm] = {} for timeslice in xrange(min2slice(conf.DAY) + 1): flow.state_flows[comm][timeslice] = {} for state in enum_state(comm, timeslice): flow.state_flows[comm][timeslice][state] = init_value # initialize the population at timeslice 0 for each zone for comm in enum_commodity(): flow.state_flows[comm][0][comm.init_state] = flow.commodity_steps[comm]
def calc_activity_choice_prob(): # activity/zone choice probability for comm in enum_commodity(): for timeslice in xrange(min2slice(conf.DAY)+1): for state in enum_state(comm, timeslice): prob.activity_choice_prob[comm.person][timeslice][(state.activity,state.zone)] += \ flow.state_flows[comm][timeslice][state] / elem.person_flows[comm.person]
def calc_state_util(): math.exp = math.exp math.log = math.log # find the optimal uitility for comm in enum_commodity(): # print " %s" % comm # backtrack from the ending to the beginning for timeslice in xrange(min2slice(conf.DAY) - 1, -1, -1): for state in enum_state(comm, timeslice): these_util = {} for transition, starting_time, travel_cost, schedule_delay in \ enum_transition(comm, timeslice, state): these_util[transition] = conf.THETA_tour * ( conf.discount * \ util.state_util[comm][starting_time][transition.state] + \ util.solo_util[timeslice][state.activity] + \ util.socio_util[comm.person][timeslice][(state.activity,state.zone)] - \ (travel_cost + schedule_delay) ) # if these_util == [], the decision space is empty and then continue to next if these_util == {}: continue # scale the utility of each decision min_util = min(these_util.values()) max_util = max(these_util.values()) sum_exp_util = sum([math.exp(utility-(max_util+min_util)/2.0) \ for utility in these_util.values()]) # calculate the expected optimal utility util.state_util[comm][timeslice][state] = \ ((max_util+min_util)/2.0 + math.log(sum_exp_util)) / conf.THETA_tour # calculate the transition choice probability for trans, utility in these_util.items(): prob.transition_choice_prob[comm][timeslice][state][trans] = \ math.exp(utility - (max_util+min_util)/2.0) / sum_exp_util util.commodity_util[comm] = util.state_util[comm][0][comm.init_state]
def calc_state_util(): math.exp = math.exp math.log = math.log # find the optimal uitility for comm in enum_commodity(): # print " %s" % comm # backtrack from the ending to the beginning for timeslice in xrange(min2slice(conf.DAY)-1,-1,-1): for state in enum_state(comm, timeslice): these_util = {} for transition, starting_time, travel_cost, schedule_delay in \ enum_transition(comm, timeslice, state): these_util[transition] = conf.THETA_tour * ( conf.discount * \ util.state_util[comm][starting_time][transition.state] + \ util.solo_util[timeslice][state.activity] + \ util.socio_util[comm.person][timeslice][(state.activity,state.zone)] - \ (travel_cost + schedule_delay) ) # if these_util == [], the decision space is empty and then continue to next if these_util == {}: continue # scale the utility of each decision min_util = min(these_util.values()) max_util = max(these_util.values()) sum_exp_util = sum([math.exp(utility-(max_util+min_util)/2.0) \ for utility in these_util.values()]) # calculate the expected optimal utility util.state_util[comm][timeslice][state] = \ ((max_util+min_util)/2.0 + math.log(sum_exp_util)) / conf.THETA_tour # calculate the transition choice probability for trans, utility in these_util.items(): prob.transition_choice_prob[comm][timeslice][state][trans] = \ math.exp(utility - (max_util+min_util)/2.0) / sum_exp_util util.commodity_util[comm] = util.state_util[comm][0][comm.init_state]
def calc_activity_choice_prob(): # activity/zone choice probability for comm in enum_commodity(): for timeslice in xrange(min2slice(conf.DAY) + 1): for state in enum_state(comm, timeslice): prob.activity_choice_prob[comm.person][timeslice][(state.activity,state.zone)] += \ flow.state_flows[comm][timeslice][state] / elem.person_flows[comm.person]
def export_state_flows(export): print>>export, '\n-------- state flows ---------\n' for comm in enum_commodity(): print>>export, " %s " % comm for timeslice in xrange(min2slice(conf.DAY)+1): print>>export, " [%03d]\t" % timeslice, for state in enum_state(comm, timeslice): print>>export, " %s: %8.2f\t" % (state, flow.state_flows[comm][timeslice][state]), print>>export
def export_state_util(export): print>>export, '\n------ state utility ------\n' for comm in enum_commodity(): print>>export, " %s " % comm for timeslice in xrange(min2slice(conf.DAY)): print>>export, "[%3d] " % timeslice, for state in enum_state(comm, timeslice): print>>export, ("\t %s: %4.2f" % (state, util.state_util[comm][timeslice][state])), print>>export print>>export
def export_static_population(export): print>>export, '\n-------- temporal flows ---------\n' for comm in enum_commodity(): print>>export, " %s " % comm print>>export, "\t static\t traveling" for timeslice in xrange(min2slice(conf.DAY)+1): print>>export, " [%03d]\t" % timeslice, print>>export, "%8.2f\t" % flow.static_population[comm][timeslice], print>>export, "%8.2f\t" % (flow.commodity_steps[comm] - flow.static_population[comm][timeslice]) print>>export
def export_state_flows(export): print >> export, '\n-------- state flows ---------\n' for comm in enum_commodity(): print >> export, " %s " % comm for timeslice in xrange(min2slice(conf.DAY) + 1): print >> export, " [%03d]\t" % timeslice, for state in enum_state(comm, timeslice): print >> export, " %s: %8.2f\t" % ( state, flow.state_flows[comm][timeslice][state]), print >> export
def export_average_temporal_util(export): print>>export, '\n------- average temporal utility -------\n' for comm in enum_commodity(): print>>export, " %s " % comm print>>export, "\t utility\t disutility" average_utility, average_travel_disutil = calc_average_temporal_util(comm) for timeslice in xrange(min2slice(conf.DAY)+1): print>>export, " [%d]\t" % timeslice, print>>export, "%8.1f\t%8.1f\t" % (average_utility[timeslice], average_travel_disutil[timeslice]), print>>export print>>export
def init_transition_choice_prob(): prob.transition_choice_prob = {} for comm in enum_commodity(): prob.transition_choice_prob[comm] = {} for timeslice in xrange(min2slice(conf.DAY)): prob.transition_choice_prob[comm][timeslice] = {} for state in enum_state(comm, timeslice): prob.transition_choice_prob[comm][timeslice][state] = {} for transition_info in enum_transition(comm, timeslice, state): transition = transition_info[0] prob.transition_choice_prob[comm][timeslice][state][transition] = 0.0
def init_transition_flows(init_value): flow.transition_flows = {} for comm in enum_commodity(): flow.transition_flows[comm] = {} for timeslice in xrange(min2slice(conf.DAY)): flow.transition_flows[comm][timeslice] = {} for state in enum_state(comm, timeslice): flow.transition_flows[comm][timeslice][state] = {} for transition_info in enum_transition(comm, timeslice, state): transition = transition_info[0] flow.transition_flows[comm][timeslice][state][transition] = init_value
def init_transition_choice_prob(): prob.transition_choice_prob = {} for comm in enum_commodity(): prob.transition_choice_prob[comm] = {} for timeslice in xrange(min2slice(conf.DAY)): prob.transition_choice_prob[comm][timeslice] = {} for state in enum_state(comm, timeslice): prob.transition_choice_prob[comm][timeslice][state] = {} for transition_info in enum_transition(comm, timeslice, state): transition = transition_info[0] prob.transition_choice_prob[comm][timeslice][state][ transition] = 0.0
def init_transition_flows(init_value): flow.transition_flows = {} for comm in enum_commodity(): flow.transition_flows[comm] = {} for timeslice in xrange(min2slice(conf.DAY)): flow.transition_flows[comm][timeslice] = {} for state in enum_state(comm, timeslice): flow.transition_flows[comm][timeslice][state] = {} for transition_info in enum_transition(comm, timeslice, state): transition = transition_info[0] flow.transition_flows[comm][timeslice][state][ transition] = init_value
def export_state_util(export): print >> export, '\n------ state utility ------\n' for comm in enum_commodity(): print >> export, " %s " % comm for timeslice in xrange(min2slice(conf.DAY)): print >> export, "[%3d] " % timeslice, for state in enum_state(comm, timeslice): print >> export, ( "\t %s: %4.2f" % (state, util.state_util[comm][timeslice][state])), print >> export print >> export
def export_static_population(export): print >> export, '\n-------- temporal flows ---------\n' for comm in enum_commodity(): print >> export, " %s " % comm print >> export, "\t static\t traveling" for timeslice in xrange(min2slice(conf.DAY) + 1): print >> export, " [%03d]\t" % timeslice, print >> export, "%8.2f\t" % flow.static_population[comm][ timeslice], print >> export, "%8.2f\t" % ( flow.commodity_steps[comm] - flow.static_population[comm][timeslice]) print >> export
def export_average_temporal_util(export): print >> export, '\n------- average temporal utility -------\n' for comm in enum_commodity(): print >> export, " %s " % comm print >> export, "\t utility\t disutility" average_utility, average_travel_disutil = calc_average_temporal_util( comm) for timeslice in xrange(min2slice(conf.DAY) + 1): print >> export, " [%d]\t" % timeslice, print >> export, "%8.1f\t%8.1f\t" % ( average_utility[timeslice], average_travel_disutil[timeslice]), print >> export print >> export
def init_state_util(): util.state_util = {} for comm in enum_commodity(): util.state_util[comm] = {} for timeslice in xrange(min2slice(conf.DAY)+1): util.state_util[comm][timeslice] = {} for state in enum_state(comm, timeslice): # flag the states that has not been visited by # setting the state utilities to negative infinity util.state_util[comm][timeslice][state] = float('-inf') # initialize the value of terminal state to zeros # and the backward recursive process starts here util.state_util[comm][min2slice(conf.DAY)][comm.term_state] = 0.0
def init_state_util(): util.state_util = {} for comm in enum_commodity(): util.state_util[comm] = {} for timeslice in xrange(min2slice(conf.DAY) + 1): util.state_util[comm][timeslice] = {} for state in enum_state(comm, timeslice): # flag the states that has not been visited by # setting the state utilities to negative infinity util.state_util[comm][timeslice][state] = float('-inf') # initialize the value of terminal state to zeros # and the backward recursive process starts here util.state_util[comm][min2slice(conf.DAY)][comm.term_state] = 0.0
def calc_population_flows(): # initialize the population flows init_zone_population(0.0) init_activity_population(0.0) init_static_population(0.0) # calculate population flow variables based on state flows for comm in enum_commodity(): for timeslice in xrange(min2slice(conf.DAY)+1): for state in enum_state(comm, timeslice): # calculate static population flow.static_population[comm][timeslice] += \ flow.state_flows[comm][timeslice][state] # calculate zone population flow.zone_population[timeslice][state.zone] += \ flow.state_flows[comm][timeslice][state] # calculate activity population flow.activity_population[timeslice][state.activity] += \ flow.state_flows[comm][timeslice][state]
def calc_population_flows(): # initialize the population flows init_zone_population(0.0) init_activity_population(0.0) init_static_population(0.0) # calculate population flow variables based on state flows for comm in enum_commodity(): for timeslice in xrange(min2slice(conf.DAY) + 1): for state in enum_state(comm, timeslice): # calculate static population flow.static_population[comm][timeslice] += \ flow.state_flows[comm][timeslice][state] # calculate zone population flow.zone_population[timeslice][state.zone] += \ flow.state_flows[comm][timeslice][state] # calculate activity population flow.activity_population[timeslice][state.activity] += \ flow.state_flows[comm][timeslice][state]
def export_activity_duration(export): print >> export, '\n------- activity duration -------\n' # initialize corr = conf.corr.values() stat.average_travel_time[corr[0]] = 0.0 stat.joint_activity_duration[corr[0]] = 0.0 stat.indep_activity_duration[corr[0]] = 0.0 # calculate joint time use joint_time_use = calc_joint_time_use() stat.joint_time_use[corr[0]] = sum( joint_time_use.values()) / len(joint_time_use) for comm in enum_commodity(): print >> export, " [%s] " % comm activity_list = sorted(list(comm.bundle.activity_set)) average_activity_duration, average_travel_time = calc_average_activity_duration( comm) # subtotal for joint and independent activities joint_activity_duration = 0.0 indep_activity_duration = 0.0 for each_actv in activity_list: if each_actv.is_joint: joint_activity_duration += average_activity_duration[each_actv] else: indep_activity_duration += average_activity_duration[each_actv] # print activity names print >> export, "%12s\t%12s\t%12s" % ('travel', 'joint', 'indep'), for each_actv in activity_list: print >> export, "%12s\t" % (each_actv.name), print >> export # print average activty durations print >> export, "%12.1f\t%12.1f\t%12.1f" % (average_travel_time, joint_activity_duration, indep_activity_duration), for each_actv in activity_list: print >> export, "%12.1f\t" % ( average_activity_duration[each_actv]), print >> export, "\n" # save the statistics for each scenario percent = flow.commodity_steps[comm] / sum(elem.person_flows.values()) stat.average_travel_time[corr[0]] += percent * average_travel_time stat.joint_activity_duration[ corr[0]] += percent * joint_activity_duration stat.indep_activity_duration[ corr[0]] += percent * indep_activity_duration
def calc_state_flows(): # calculate flow variables based on state optimal utility for comm in enum_commodity(): # print " %s" % comm # from the beginning to the ending for timeslice in xrange(min2slice(conf.DAY)): for state in enum_state(comm, timeslice): for transition, starting_time, cost, delay in enum_transition(comm, timeslice, state): # calculate transition flows flow.transition_flows[comm][timeslice][state][transition] = \ flow.state_flows[comm][timeslice][state] * \ prob.transition_choice_prob[comm][timeslice][state][transition] # add transition flows to edge steps add_movement_steps(transition.path, timeslice, \ flow.transition_flows[comm][timeslice][state][transition]) # update state flows flow.state_flows[comm][starting_time][transition.state] = \ flow.state_flows[comm][starting_time][transition.state] + \ flow.transition_flows[comm][timeslice][state][transition]
def calc_state_flows(): # calculate flow variables based on state optimal utility for comm in enum_commodity(): # print " %s" % comm # from the beginning to the ending for timeslice in xrange(min2slice(conf.DAY)): for state in enum_state(comm, timeslice): for transition, starting_time, cost, delay in enum_transition(comm, timeslice, state): # calculate transition flows flow.transition_flows[comm][timeslice][state][transition] = ( flow.state_flows[comm][timeslice][state] * prob.transition_choice_prob[comm][timeslice][state][transition] ) # add transition flows to edge steps add_movement_steps( transition.path, timeslice, flow.transition_flows[comm][timeslice][state][transition] ) # update state flows flow.state_flows[comm][starting_time][transition.state] = ( flow.state_flows[comm][starting_time][transition.state] + flow.transition_flows[comm][timeslice][state][transition] )
def export_activity_duration(export): print>>export, '\n------- activity duration -------\n' # initialize corr = conf.corr.values() stat.average_travel_time[corr[0]] = 0.0 stat.joint_activity_duration[corr[0]] = 0.0 stat.indep_activity_duration[corr[0]] = 0.0 # calculate joint time use joint_time_use = calc_joint_time_use() stat.joint_time_use[corr[0]] = sum(joint_time_use.values())/len(joint_time_use) for comm in enum_commodity(): print>>export, " [%s] " % comm activity_list = sorted(list(comm.bundle.activity_set)) average_activity_duration, average_travel_time = calc_average_activity_duration(comm) # subtotal for joint and independent activities joint_activity_duration = 0.0 indep_activity_duration = 0.0 for each_actv in activity_list: if each_actv.is_joint: joint_activity_duration += average_activity_duration[each_actv] else: indep_activity_duration += average_activity_duration[each_actv] # print activity names print>>export, "%12s\t%12s\t%12s" % ('travel', 'joint', 'indep'), for each_actv in activity_list: print>>export, "%12s\t" % (each_actv.name), print>>export # print average activty durations print>>export, "%12.1f\t%12.1f\t%12.1f" % (average_travel_time, joint_activity_duration, indep_activity_duration), for each_actv in activity_list: print>>export, "%12.1f\t" % (average_activity_duration[each_actv]), print>>export, "\n" # save the statistics for each scenario percent = flow.commodity_steps[comm] / sum(elem.person_flows.values()) stat.average_travel_time[corr[0]] += percent * average_travel_time stat.joint_activity_duration[corr[0]] += percent * joint_activity_duration stat.indep_activity_duration[corr[0]] += percent * indep_activity_duration
def init_flow_variables(): flow.movement_flows = {} flow.commodity_flows = {} for comm in enum_commodity(): flow.commodity_flows[comm] = 0.0
def init_static_population(init_values): flow.static_population = {} for comm in enum_commodity(): flow.static_population[comm] = {} for timeslice in xrange(min2slice(conf.DAY) + 1): flow.static_population[comm][timeslice] = 0.0
def init_static_population(init_values): flow.static_population = {} for comm in enum_commodity(): flow.static_population[comm] = {} for timeslice in xrange(min2slice(conf.DAY)+1): flow.static_population[comm][timeslice] = 0.0