Пример #1
0
def load_graph_traj(trajectories):
    nodes = {}
    edges = {}

    for trajectory in trajectories:
        for slot in trajectory.keys():
            mode = trajectory[slot][1].get_mode()
            origin = trajectory[slot][1].get_origin()
            destination = trajectory[slot][1].get_destination()
            if origin not in nodes:
                nodes[origin] = origin
            if destination not in nodes:
                nodes[destination] = destination
            e = graph.Edge(origin, destination, mode)
            e2 = graph.Edge(destination, origin, mode)
            if e not in edges:
                edges[e] = e
            if e2 not in edges:
                edges[e2] = e2

    g = graph.Graph(nodes.values(),
                    edges.values(),
                    directed=False,
                    normalization=True)

    print "edge number = ", g.get_edge_number()
    print "node number = ", g.get_node_number()

    return g
Пример #2
0
def trip_getter(trajectory, order):
    trips = []
    for t in trajectory.keys():
        if trajectory[t][1].get_origin() != trajectory[t][1].get_destination():
            trips.append(trajectory[t][1])
    if order <= len(trips):
        return trips[order - 1]
    else:
        return graph.Edge("", "", "")
Пример #3
0
def load_graph(lines):
    """

    :return:
    """
    nodes = {}
    edges = {}

    count = 0
    with open("D:/training data/PTtraj3.csv") as f:
        for line in f.readlines()[0:lines]:
            count += 1
            if count % 10000 == 0:
                print count

            tokens = line.strip('\n').split(",")
            mode = tokens[4]
            origin = tokens[2]
            destination = tokens[3]
            if origin not in nodes:
                nodes[origin] = origin
            if destination not in nodes:
                nodes[destination] = destination
            e = graph.Edge(origin, destination, mode)
            e2 = graph.Edge(destination, origin, mode)
            if e not in edges:
                edges[e] = e
            if e2 not in edges:
                edges[e2] = e2

    f.close()
    try:
        g = graph.Graph(nodes.values(), edges.values(), directed=False)
    except (AttributeError, ValueError, IndexError, TypeError):
        print "errrrrrrrrrrrrrrrroooooooooooooooooooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrr........"

    print "edge number = ", g.get_edge_number()
    print "node number = ", g.get_node_number()

    return g
Пример #4
0
    def load_trajectory(self):
        """
        trajectory format: (state, action)-> State(node, timestamp), Edge(origin, destination, mode)
        :return: dict->id_trajectories, trajectory -> list() of traj tuple(state, action)
        """
        id_traj = {}
        count = 0

        starttime = datetime.datetime.now()
        with open(self.path) as f:
            for line in f.readlines():
                try:
                    count += 1
                    if count % 100000 == 0:
                        print("finish " + str(count) + " lines")

                    line = line.strip('\r\n')
                    tokens = line.split(",")
                    agent_id = tokens[0]
                    timeslot = int(tokens[1])
                    start = tokens[2]
                    end = tokens[3]
                    mode = tokens[4]

                    state = State(start, timeslot)
                    e = graph.Edge(start, end, mode)

                    traj = (state, e)

                    if agent_id not in id_traj.keys():
                        trajectory = []
                        id_traj[agent_id] = trajectory
                        id_traj[agent_id].append(traj)
                    else:
                        id_traj[agent_id].append(traj)

                except (AttributeError, ValueError, IndexError, TypeError):
                    print("Loading Trajectory Error")

        f.close()
        endtime = datetime.datetime.now()
        print("finished loading" + str(len(id_traj)) + "trajectories in" +
              str(endtime - starttime))

        return id_traj
Пример #5
0
def load_trajectory(num):

    id_traj = {}
    count = 0

    starttime = datetime.datetime.now()
    # with open("home/t-iho/Result/trainingdata/trainingdata"+date+".csv") as f:
    with open("D:/training data/PTtraj3.csv") as f:
        for line in f.readlines():
            try:
                count += 1
                if count % 100000 == 0:
                    print "finish " + str(count) + " lines"
                line = line.strip('\n')
                tokens = line.split(",")
                agent_id = tokens[0]
                timeslot = int(tokens[1])
                start = tokens[2]
                end = tokens[3]
                mode = tokens[4]
                e = graph.Edge(start, end, mode)
                traj = (start, e)

                if agent_id not in id_traj.keys():
                    trajectory = {}
                    id_traj[agent_id] = trajectory
                    id_traj[agent_id][timeslot] = traj
                else:
                    id_traj[agent_id][timeslot] = traj
                if len(id_traj) > num:
                    break
            except (AttributeError, ValueError, IndexError, TypeError):
                print "errrrrrrrrrrrrrrrroooooooooooooooooooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrr........"

    f.close()
    endtime = datetime.datetime.now()
    print "finished reading trajectories with time of" + str(endtime -
                                                             starttime)
    print "id count", len(id_traj)
    return id_traj
Пример #6
0
def load_directory_trajectory(directory):

    id_traj = {}
    count = 0

    files = os.listdir(directory)

    for filename in files:
        print filename
        path = directory + filename
        if not os.path.isdir(path):
            with open(path) as f:
                count += 1
                for line in f.readlines():
                    try:
                        line = line.strip('\n')
                        tokens = line.split(",")
                        agent_id = tokens[0]
                        timeslot = int(tokens[1])
                        start = tokens[2]
                        end = tokens[3]
                        mode = tokens[4]
                        e = graph.Edge(start, end, mode)
                        traj = (start, e)

                        if agent_id not in id_traj.keys():
                            trajectory = {}
                            id_traj[agent_id] = trajectory
                            id_traj[agent_id][timeslot] = traj
                        else:
                            id_traj[agent_id][timeslot] = traj
                    except (AttributeError, ValueError, IndexError, TypeError):
                        print "errrrrrrrrrrrrrrrroooooooooooooooooooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrrrr........"
                f.close()

    return id_traj
Пример #7
0
def choose_action(start, state, time, g, x, pop, office):
    """

    :param start:
    :param state:
    :param time:
    :param g:
    :param x:
    :param pop:
    :param office:
    :return: activity type, edge
    """

    walk = np.exp(x[0])
    vehicle = np.exp(x[1])
    train = np.exp(x[2])

    logsum_mode = np.log(walk + vehicle + train)

    dest_utility = {}

    deno_dest = 0

    for edge in g.get_node(state).get_edges():

        dest = edge.get_destination()
        distance = dist(state, dest)
        if dest in pop.keys() and dest != start:
            dest_utility[dest] = x[3] * logsum_mode + office[dest][1] * x[
                5] + pop[dest] * x[6] + distance * x[7] + x[8]
        else:
            continue

        deno_dest += np.exp(dest_utility[dest])

    logsum_commute_dest = np.log(deno_dest)
    logsum_other_dest = np.log(deno_dest)

    period_0_9 = 1 if time < 18 else 0
    period_9_17 = 1 if time >= 18 and time < 34 else 0
    period_17_24 = 1 if time >= 34 else 0

    commute = np.exp(x[9] * logsum_commute_dest + x[11] * period_0_9 +
                     x[12] * period_9_17 + x[13] * period_17_24 + x[14] +
                     x[15])
    other = np.exp(x[10] * logsum_other_dest + x[10] * period_0_9 +
                   x[11] * period_9_17 + x[12] * period_17_24 + x[14] + x[15])
    home = np.exp(x[4] * logsum_mode + x[11] * period_0_9 +
                  x[12] * period_9_17 + x[13] * period_17_24 + x[14] +
                  x[15]) if state != start else 0
    # stop = np.exp(x[11] * period_0_9 + x[12] * period_9_17 + x[13] * period_17_24 + x[14] + x[15])
    stop = 0

    # print "home", start, state, home, commute, other

    deno = commute + other + home + stop

    activity_prob = {}

    activity_prob["commute"] = commute
    activity_prob["home"] = home
    activity_prob["other"] = other
    activity_prob["stop"] = stop

    activity = max(activity_prob.items(), key=lambda x: x[1])[0]
    # print activity_prob
    # print activity

    action_prob = dict()

    # stop
    e_stop = graph.Edge(state, state, "stay")
    action_prob[e_stop] = stop / deno

    # home
    e_home_walk = graph.Edge(state, start, "walk")
    action_prob[e_home_walk] = (home / deno) * (walk / walk + vehicle + train)

    e_home_vehicle = graph.Edge(state, start, "vehicle")
    action_prob[e_home_vehicle] = (home / deno) * (vehicle / walk + vehicle +
                                                   train)

    e_home_train = graph.Edge(state, start, "train")
    action_prob[e_home_train] = (home / deno) * (train / walk + vehicle +
                                                 train)

    # commute and other
    for edge in g.get_node(state).get_edges():
        if edge not in action_prob.keys() and edge.get_destination(
        ) in dest_utility.keys() and edge.get_destination() != start:

            if edge.get_mode() == "walk":
                action_prob[edge] = (commute + other) * (
                    np.exp(dest_utility[edge.get_destination()]) /
                    np.exp(logsum_commute_dest)) * (
                        walk / (walk + vehicle + train)) / 8.0
            elif edge.get_mode() == "vehicle":
                action_prob[edge] = (commute + other) * (
                    np.exp(dest_utility[edge.get_destination()]) /
                    np.exp(logsum_commute_dest)) * (
                        vehicle / (walk + vehicle + train)) / 8.0
            elif edge.get_mode() == "train":
                action_prob[edge] = (commute + other) * (
                    np.exp(dest_utility[edge.get_destination()]) /
                    np.exp(logsum_commute_dest)) * (
                        train / (walk + vehicle + train)) / 8.0

    # print max(action_prob.items(), key=lambda x:x[1])[0]
    return max(activity_prob.items(),
               key=lambda x: x[1])[0], max(action_prob.items(),
                                           key=lambda x: x[1])[0]