Esempio n. 1
0
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]
Esempio n. 2
0
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]
Esempio n. 3
0
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
Esempio n. 4
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
Esempio n. 5
0
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
Esempio n. 6
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
Esempio n. 7
0
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]
Esempio n. 8
0
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]
                    )