Ejemplo n.º 1
0
def simulation(trajectories, path, start, count):

    if os.path.exists(path):

        parampath = path
        try:
            g = load.load_graph_traj(trajectories)
            g.set_start(start)
            gw = gridworld.Gridworld(g, 0.9, "")
            feature_matrix = gw.feature_matrix(g)

            alpha = load.load_param(parampath)
            print(alpha)

            r = dict()
            for t in range(12, 48):
                r[t] = dict().fromkeys(g.get_edges(), 0)

            for t in range(12, 48):
                for edge in g.get_edges():
                    if t in alpha.keys():
                        r[t][edge] = feature_matrix[t][edge].dot(alpha[t])
            print(r)
            for i in range(count):
                print("****************")
                directory = "/home/ubuntu/Data/PT_Result/100expert_1agent/" + start + "/sim/"
                if not os.path.exists(directory):
                    os.mkdir(directory)
                tools.simple_trajectory(
                    g, r, start,
                    "/home/ubuntu/Data/PT_Result/100expert_1agent/" + start +
                    "/", start + "_" + str(i + 50))

        except KeyError:
            return 0
Ejemplo n.º 2
0
def simulation(trajectories, path, start, count):

    if os.path.exists(path):

        parampath = path + start + "_0_param.csv"
        try:
            g = load.load_graph_traj(trajectories)
            g.set_start(start)
            gw = gridworld.Gridworld(g, 0.9)
            feature_matrix = gw.feature_matrix(g)

            alpha = load_param(parampath)

            r = dict()
            for t in range(12, 48):
                r[t] = dict().fromkeys(g.get_edges(), 0)

            for t in range(12, 48):
                for edge in g.get_edges():
                    if t in alpha.keys():
                        r[t][edge] = feature_matrix[edge].dot(alpha[t])
            # print r
            for i in range(count):
                print("****************")
                directory = "/home/t-iho/Result/sim/" + start
                if not os.path.exists(directory):
                    os.mkdir(directory)
                tools.simple_trajectory(
                    g, r, start, "/home/t-iho/Result/sim/" + start + "/",
                    start + "_" + str(i))

        except KeyError:
            return 0
Ejemplo n.º 3
0
def main(discount, epochs, learning_rate):
    """
    Run maximum entropy inverse reinforcement learning on the gridworld MDP.

    discount: MDP discount factor. float.
    epochs: Gradient descent iterations. int.
    learning_rate: Gradient descent learning rate. float.
    """

    try:
        starttime = datetime.datetime.now()
        path = "/home/ubuntu/Data/KDDI/#201111.CDR-data/vks2564k/slot/"

        id_traj = load.load_directory_trajectory(path)

        print(len(id_traj))

        trajectories = id_traj.values()
        g = load.load_graph_traj(trajectories)
        g.set_start("53397561")
        gw = gridworld.Gridworld(g, discount)
        feature_matrix = gw.feature_matrix(g)

        if not os.path.exists(path + "param/"):
            os.mkdir(path + "param/")

        maxent.t_irl(g, feature_matrix, trajectories, epochs, learning_rate,
                     path + "param/")

        endtime = datetime.datetime.now()

        print("finished reading files with time of" + str(endtime - starttime))
    except Exception:
        print("mian class wrong")
        raise
Ejemplo n.º 4
0
def main(epochs, learning_rate, discount, number):
    """
    discount: MDP discount factor. float.
    epochs: Gradient descent iterations. int.
    learning_rate: Gradient descent learning rate. float.
    """

    try:
        starttime = datetime.datetime.now()

        mesh_list = read_list("/home/ubuntu/Data/Tokyo/MeshCode/Tokyo.csv",
                              number)
        print(len(mesh_list))
        print(mesh_list)

        for mesh_id in mesh_list:
            # if not os.path.exists("/home/ubuntu/Data/PT_Result/commuter/test_sim/" + mesh_id + "/"):
            #     os.mkdir("/home/ubuntu/Data/PT_Result/commuter/test_sim/" + mesh_id + "/")
            #
            # if not os.path.exists("/home/ubuntu/Data/PT_Result/commuter/test_param/" + mesh_id + "/"):
            #     os.mkdir("/home/ubuntu/Data/PT_Result/commuter/test_param/" + mesh_id + "/")

            if os.path.exists("/home/ubuntu/Data/pflow_data/pflow-csv/" +
                              mesh_id + "/train_irl.csv"):
                id_traj = load.load_trajectory(
                    "/home/ubuntu/Data/pflow_data/pflow-csv/" + mesh_id +
                    "/train_irl.csv")

                # parameter set numbers
                if len(id_traj) > 200:
                    # for i in range(len(id_traj)/50):
                    trajectories = random.sample(id_traj.values(), 200)
                    g = load.load_graph_traj(trajectories)
                    g.set_start(mesh_id)
                    print(g.get_start())
                    gw = gridworld.Gridworld(g, discount, "")
                    feature_matrix = gw.feature_matrix(g)

                    # train#

                    maxent.t_irl(
                        g, feature_matrix, trajectories, epochs, learning_rate,
                        "/home/ubuntu/Data/PT_Result/param_15/" + mesh_id +
                        "_" + str(1) + "_")

            fo = open("/home/ubuntu//Data/PT_Result/finished_mesh.csv", "a")
            fo.write(mesh_id + "/n")
            fo.close()

        endtime = datetime.datetime.now()

        print("finished reading files with time of" + str(endtime - starttime))
    except Exception:
        print("mian class wrong")
        raise
Ejemplo n.º 5
0
def main():
    # ---------------------------------------------------------------------------------------------------
    print("start")
    X_train, X_test, y_train, y_test = load_data(
        "/home/ubuntu/Data/baseline_test.csv")
    id_traj = load.load_trajectory("/home/ubuntu/Data/baseline_test.csv")
    trajectories = id_traj.values()
    g = load.load_graph_traj(trajectories)
    print(X_train.shape, y_train.shape)
    model = build_linear_model(X_train, y_train)
    ref = pd.DataFrame.from_csv('action_ref.csv')

    bc_policy = policy(model, ref, g)

    mesh_count = generate_initial(trajectories)

    for mesh in mesh_count:
        simulation(bc_policy, ref, mesh, mesh_count[mesh])
Ejemplo n.º 6
0
def simulation(path):
    id_traj = load.load_directory_trajectory(path + "slot/")

    files = os.listdir(path + "param/")

    if not os.path.exists(path + "sim/"):
        os.mkdir(path + "sim/")

    for filename in files:
        parampath = path + "param/" + filename
        if not os.path.isdir(parampath):

            trajectories = id_traj.values()
            g = load.load_graph_traj(trajectories)
            gw = gridworld.Gridworld(g, 0.9)
            feature_matrix = gw.feature_matrix(g)

            alpha = load.load_param(parampath)
            print(alpha)

            r = dict()
            for t in range(12, 48):
                r[t] = dict().fromkeys(g.get_edges(), 0)

            for t in range(12, 48):
                for edge in g.get_edges():
                    if t in alpha.keys():
                        r[t][edge] = feature_matrix[t][edge].dot(alpha[t])
            print(r)

            for i in range(10):
                print("****************")
                directory = "/home/ubuntu/Data/KDDI/#201111.CDR-data/abf7380g/sim/"
                if not os.path.exists(directory):
                    os.mkdir(directory)
                tools.simple_trajectory(
                    g, r, "53397561",
                    "/home/ubuntu/Data/KDDI/#201111.CDR-data/abf7380g/sim/",
                    "53397561" + "_" + str(i))

            start = "53397561"
            tools.generate_temporal_traj(g, r, start, 0.5, path + "sim/",
                                         filename[0:2])
Ejemplo n.º 7
0
def simulation(trajectories, path, start, count, job):

    files = os.listdir(path + "param/")

    parampath = path + "param/" + random.choice(files)
    if not os.path.isdir(parampath):

        # trajectories = random.sample(trajectories, 50)
        try:
            g = load.load_graph_traj(trajectories)
            gw = gridworld.Gridworld(g, 0.9)
            feature_matrix = gw.feature_matrix(g)

            t_alpha = {}
            with open(parampath, 'r') as f:
                t = 12
                for line in f:
                    line = line.strip('\n')
                    tokens = line.split(",")
                    param = numpy.zeros(11)
                    for j in range(11):
                        if len(tokens) > j:
                            param[j] = tokens[j]
                    t_alpha[t] = param.copy()
                    t += 1

            r = dict()
            for t in range(12, 48):
                r[t] = dict().fromkeys(g.get_edges(), 0)

            for edge in g.get_edges():
                for t in range(12, 48):
                    if t in t_alpha.keys():
                        r[t][edge] = feature_matrix[edge].dot(t_alpha[t])

            for i in range(count):
                # start = random.choice(initial)
                tools.simple_trajectory(g, r, start,
                                        "/home/ubuntu/Data/PT_Result/exp3",
                                        start + "_" + job + "_" + str(i))

        except KeyError:
            return 0
Ejemplo n.º 8
0
def main(discount, epochs, learning_rate, target):
    """
    Run maximum entropy inverse reinforcement learning on the gridworld MDP.

    discount: MDP discount factor. float.
    epochs: Gradient descent iterations. int.
    learning_rate: Gradient descent learning rate. float.
    """

    try:
        starttime = datetime.datetime.now()
        path = "/home/ubuntu/Data/PT_Result/" + target + "/"

        if not os.path.exists(path + "sim/"):
            os.mkdir(path + "sim/")

        if not os.path.exists(path + "param/"):
            os.mkdir(path + "param/")

        if os.path.exists(path + "training/"):
            id_traj = load.load_trajectory(
                "/home/ubuntu/Data/PT_Result/commuter/training/PT_commuter_irl_revised.csv"
            )

            # parameter set numbers
            for i in range(10000):
                trajectories = random.sample(id_traj.values(), 200)
                print trajectories
                g = load.load_graph_traj(trajectories)
                gw = gridworld.Gridworld(g, discount)
                feature_matrix = gw.feature_matrix(g)

                # train#
                print("training ", path)
                maxent.t_irl(g, feature_matrix, trajectories, epochs,
                             learning_rate, path + "param/" + str(i))

        endtime = datetime.datetime.now()

        print("finished reading files with time of" + str(endtime - starttime))
    except Exception:
        print("mian class wrong")
        raise
Ejemplo n.º 9
0
def main(mesh_id):
    """
    discount: MDP discount factor. float.
    epochs: Gradient descent iterations. int.
    learning_rate: Gradient descent learning rate. float.
    """

    discount = .9
    epochs = 400
    learning_rate = 3

    try:
        starttime = datetime.datetime.now()

        if not os.path.exists("/home/ubuntu/Data/PT_Result/100expert_1agent/" + mesh_id + "/"):
            os.mkdir("/home/ubuntu/Data/PT_Result/100expert_1agent/" + mesh_id + '/')

        if os.path.exists("/home/ubuntu/Data/pflow_data/pflow-csv/" + mesh_id + "/train_irl.csv"):
            id_traj = load.load_trajectory("/home/ubuntu/Data/pflow_data/pflow-csv/" + mesh_id + "/train_irl.csv")

            # parameter set numbers
            for i in range(3):
                print(type(list(id_traj.values())))
                trajectories = random.sample(list(id_traj.values()), 100)

                # save out expert data
                writeout.write_trajs(trajectories, "/home/ubuntu/Data/PT_Result/100expert_1agent/"
                                     + mesh_id + "/training_data.csv")

                g = load.load_graph_traj(trajectories)
                g.set_start(mesh_id)
                print(g.get_start())
                gw = gridworld.Gridworld(g, discount)
                feature_matrix = gw.feature_matrix(g)

                # train#

                maxent.t_irl(g, feature_matrix, trajectories, epochs, learning_rate,
                             "/home/ubuntu/Data/PT_Result/100expert_1agent/" + mesh_id + "/" + str(i+3)+"_")

                # alpha = load.load_param("/home/ubuntu/Data/PT_Result/100expert_1agent/" + mesh_id + "/" + str(i) +
                #                         "_" + 'param.csv')

                # r = dict()
                # for t in range(12, 48):
                #     r[t] = dict().fromkeys(g.get_edges(), 0)
                #
                # for t in range(12, 48):
                #     for edge in g.get_edges():
                #         if t in alpha.keys():
                #             r[t][edge] = feature_matrix[t][edge].dot(alpha[t])
                #
                # for j in range(20):
                #     tools.simple_trajectory(g, r, mesh_id, "/home/ubuntu/Data/PT_Result/100expert_1agent/" + mesh_id +
                #                             "/", mesh_id + "_" + str(j))

        endtime = datetime.datetime.now()

        print ("finished reading files with time of" + str(endtime - starttime))
    except Exception:
        print("mian class wrong")
        raise
Ejemplo n.º 10
0
def generate_trajectory(trajectories, path, start, agent_id):
    files = os.listdir(path + "param/")

    parampath = path + "param/" + random.choice(files)
    if not os.path.isdir(parampath):

        # trajectories = random.sample(trajectories, 50)
        try:
            g = load.load_graph_traj(trajectories)

            pop = pop_feature()
            office = get_business()

            param = np.zeros(16)
            with open(parampath, 'r') as f:
                for line in f:
                    line = line.strip('\n')
                    tokens = line.split(",")
                    param = np.zeros(16)
                    for j in range(16):
                        if len(tokens) > j:
                            param[j] = tokens[j]
                print param

            current_state = start

            with open(path + "sim/" + agent_id + "_dcm.csv", "w") as f:
                t = 12
                while t in range(12, 48):
                    print "********************************************"
                    activity, action = choose_action(start, current_state, t,
                                                     g, param, pop, office)
                    if t in range(12, 24):
                        departure_time = random.randint(12, 18)

                        print t, action, start, action.get_destination(
                        ) == start

                        for _t in range(12, departure_time):
                            f.write(agent_id + "," + str(_t) + "," + start +
                                    "," + start + "," + "stay" + "," + "stay" +
                                    "\n")

                        f.write(agent_id + "," + str(departure_time) + "," +
                                action.get_origin() + "," +
                                action.get_destination() + "," +
                                action.get_mode() + "," + activity + "\n")

                        for _t in range(departure_time + 1, 32):
                            f.write(agent_id + "," + str(_t) + "," +
                                    action.get_destination() + "," +
                                    action.get_destination() + "," + "stay" +
                                    "," + "stay" + "\n")

                        t = 32
                        current_state = action.get_destination()
                        continue

                    if t in range(32, 48):
                        departure_time = random.randint(32, 48)
                        activity, action = choose_action(
                            start, current_state, t, g, param, pop, office)

                        print t, action, start, action.get_destination(
                        ) == start

                        for _t in range(32, departure_time):
                            f.write(agent_id + "," + str(_t) + "," + start +
                                    "," + start + "," + "stay" + "," + "stay" +
                                    "\n")

                        f.write(agent_id + "," + str(departure_time) + "," +
                                action.get_origin() + "," +
                                action.get_destination() + "," +
                                action.get_mode() + "," + activity + "\n")

                        for _t in range(departure_time + 1, 48):
                            f.write(agent_id + "," + str(_t) + "," +
                                    action.get_destination() + "," +
                                    action.get_destination() + "," + "stay" +
                                    "," + "stay" + "\n")

                        t = 48
                        current_state = action.get_destination()
                        continue

                        # if t in range(35, 48):
                        #     departure_time = random.randint(35, 48)
                        #     activity, action = choose_action(start, current_state, t, g, param, pop, office)
                        #
                        #     print t, action, start, action.get_destination() == start
                        #
                        #     for _t in range(35, departure_time):
                        #         f.write(agent_id + "," + str(
                        #             _t) + "," + current_state + "," + current_state + "," + "stay" + "," + "stay" + "\n")
                        #
                        #     f.write(agent_id + "," + str(
                        #         departure_time) + "," + action.get_origin() + "," + action.get_destination() + "," + action.get_mode() + "," + activity + "\n")
                        #
                        #     for _t in range(departure_time + 1, 48):
                        #         f.write(agent_id + "," + str(
                        #             _t) + "," + action.get_destination() + "," + action.get_destination() + "," + "stay" + "," + "stay" + "\n")

                        t = 48
                        current_state = action.get_destination()
                        continue

        except KeyError:
            return 0
Ejemplo n.º 11
0
    """

    :param mesh:
    :return:
    """
    if str(mesh) in feature['passenger'].keys():
        return feature['passenger'][str(mesh)]
    else:
        return 0


starttime = datetime.datetime.now()

# load graph->alterntives
id_trajectory = load.load_trajectory(300)
mobility_graph = load.load_graph_traj(id_trajectory.values())

initial_list = []

for uid in id_trajectory.keys():
    initial_list.append(id_trajectory[uid][12][0])

origin_set = set(initial_list)

# load features
pop = feature_extractor.pop_feature()
office = feature_extractor.get_business()
passenger = feature_extractor.passanger_feature()
school = feature_extractor.school_feature()

feature = {