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_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 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_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_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_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 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_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_average_activity_duration(commodity): average_activity_duration = {} # initialize average duration for activity in commodity.bundle.activity_set: average_activity_duration[activity] = 0.0 # average duration for timeslice in xrange(min2slice(conf.DAY)): for state in enum_state(commodity, timeslice): average_activity_duration[state.activity] += conf.TICK * \ flow.state_flows[commodity][timeslice][state] / flow.commodity_steps[commodity] # average travel time average_travel_time = 1440 - sum(average_activity_duration.values()) return average_activity_duration, average_travel_time
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_average_temporal_util(commodity): average_temporal_utility = {} average_travel_disutil = {} for timeslice in xrange(min2slice(conf.DAY)+1): # initialize temporal utility average_temporal_utility[timeslice] = 0.0 for state in enum_state(commodity, timeslice): # average temporal utility average_temporal_utility[timeslice] += (util.solo_util[timeslice][state.activity] + \ util.socio_util[commodity.person][timeslice][(state.activity,state.zone)])* \ flow.state_flows[commodity][timeslice][state] / flow.commodity_steps[commodity] # travel disutility for timeslice in xrange(min2slice(conf.DAY)+1): average_travel_disutil[timeslice] = (flow.commodity_steps[commodity] - \ flow.static_population[commodity][timeslice]) * \ conf.ALPHA_car / flow.commodity_steps[commodity] return average_temporal_utility, average_travel_disutil
def calc_average_temporal_util(commodity): average_temporal_utility = {} average_travel_disutil = {} for timeslice in xrange(min2slice(conf.DAY) + 1): # initialize temporal utility average_temporal_utility[timeslice] = 0.0 for state in enum_state(commodity, timeslice): # average temporal utility average_temporal_utility[timeslice] += (util.solo_util[timeslice][state.activity] + \ util.socio_util[commodity.person][timeslice][(state.activity,state.zone)])* \ flow.state_flows[commodity][timeslice][state] / flow.commodity_steps[commodity] # travel disutility for timeslice in xrange(min2slice(conf.DAY) + 1): average_travel_disutil[timeslice] = (flow.commodity_steps[commodity] - \ flow.static_population[commodity][timeslice]) * \ conf.ALPHA_car / flow.commodity_steps[commodity] return average_temporal_utility, average_travel_disutil
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 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] )