def load_experience_memory(self, path):
     sd_path = os.path.join(path, "sd_history.pkl")
     sars_path = os.path.join(path, "sars_history.pkl")
     self.supply_demand_history = pickle.load(open(sd_path, "rb"))
     self.experience_memory = pickle.load(open(sars_path, "rb"))
     # print(len(self.experience_memory))
     state_action, _, _ = self.experience_memory[0]
     t_start, _, _ = state_action
     state_action, _, _ = self.experience_memory[-1]
     t_end, _, _ = state_action
     print("period: {} ~ {}".format(get_local_datetime(t_start),
                                    get_local_datetime(t_end)))
Beispiel #2
0
 def load_OD_matrix(t, alpha=0.1):
     localtime = get_local_datetime(t)
     dayofweek, hour = localtime.weekday(), localtime.hour
     hours_bin = int(hour / DESTINATION_PROFILE_TEMPORAL_AGGREGATION)
     query = """
       SELECT origin_x, origin_y, destination_x, destination_y, demand, trip_time
       FROM od_profile
       WHERE dayofweek = {dayofweek} and hours_bin = {hours_bin};
             """.format(dayofweek=dayofweek, hours_bin=hours_bin)
     df = pd.read_sql(query, engine, index_col=["origin_x", "origin_y", "destination_x", "destination_y"])
     X_size = int(MAP_WIDTH / DESTINATION_PROFILE_SPATIAL_AGGREGATION) + 1
     Y_size = int(MAP_HEIGHT / DESTINATION_PROFILE_SPATIAL_AGGREGATION) + 1
     OD = np.full((X_size, Y_size, X_size, Y_size), alpha)
     TT = np.zeros((X_size, Y_size, X_size, Y_size))
     for od, row in df.iterrows():
         OD[od] += row.demand
         TT[od] = row.trip_time
     for ox in range(X_size):
         for oy in range(Y_size):
             OD[ox, oy] /= OD[ox, oy].sum()
     average_TT = np.zeros((X_size, Y_size))
     for ox in range(X_size):
         for oy in range(Y_size):
            average_TT[ox, oy] = (TT[ox, oy] * OD[ox, oy]).sum()
     # TT = np.tensordot(TT, OD, axes=[(2, 3), (2, 3)])
     return OD, average_TT
Beispiel #3
0
    def par_step_push(self):
        t1 = time.time()
        self.push_vehicles(self.__dt, self.__t)
        # t_p_update = time.time() - t1

        #after the push, call the vehicle to step for all vehicles in each zone
        # t1 = time.time()
        self.vehicle_step_update(
            self.__dt,
            self.__t)  # interpolate routes and update vehicle status
        # t_v_update=time.time()-t1

        # update the demand for each matching zone

        ray.get([
            c.async_demand_gen.remote(self.__t)
            for c in self.match_zone_collection
        ])
        '''
        todo: add a download and push function for charging stations. 
        '''

        self.download_match_zone_metrics()

        if self.__t % STORE_TRANSITION_CYCLE == 0:  # STORE_TRANSITION_CYCLE = 60*60 sec
            self.store_transitions_from_veh()

        self.__update_time()
        if self.__t % 3600 == 0:
            self.logger.info("Elapsed : {}".format(get_local_datetime(
                self.__t)))  # added origin UNIX time inside func.
 def get_customer_waiting_time(self, customer_df, bin_width=300):
     """ Customer Waiting time (discretized time) """
     customer_df["time_bin"] = self.add_time_bin(customer_df, bin_width)
     df = customer_df[customer_df.status == 2].groupby(
         "time_bin").waiting_time.mean()
     df.index = [time_utils.get_local_datetime(x) for x in df.index]
     return df
Beispiel #5
0
    def step(self):
        for customer in CustomerRepository.get_all():
            customer.step(self.__dt)
            if customer.is_arrived() or customer.is_disappeared():
                CustomerRepository.delete(customer.get_id())

        for vehicle in VehicleRepository.get_all():
            vehicle.step(self.__dt)
            # vehicle.print_vehicle()
            if vehicle.exit_market():
                score = ','.join(
                    map(str, [
                        self.get_current_time(),
                        vehicle.get_id(),
                        vehicle.get_total_dist(),
                        vehicle.compute_profit()
                    ] + vehicle.get_score()))
                if vehicle.agent_type == agent_codes.dqn_agent:
                    self.current_dqnV -= 1
                else:
                    self.current_dummyV -= 1
                sim_logger.log_score(score)
                VehicleRepository.delete(vehicle.get_id())

        self.__populate_new_customers()
        self.__update_time()
        if self.__t % 3600 == 0:
            # print("Elapsed : {}".format(get_local_datetime(self.__t)))
            self.logger.info("Elapsed : {}".format(get_local_datetime(
                self.__t)))
Beispiel #6
0
def create_training_dataset(df, n_weeks):
    t_start = df.request_datetime.min()
    t_end = t_start + 3600 * 24 * 7 * n_weeks
    df = df[(df.request_datetime >= t_start) & (df.request_datetime < t_end)]
    df["datetime_obj"] = df.request_datetime.apply(
        lambda x: get_local_datetime(x))
    return df
Beispiel #7
0
    def par_step(self):  # we use parallel update to call the step function.
        '''
        Parallel run of the simulator that involves the following key steps:
        1. conduct the matching for each matching zone
        2. Update passenger status
        3. Update vehicle status
        4. Dispatch vehicles
        5. Generate new passengers
        :return:
        '''
        # conduct matching first
        tick = self.__t - self.start_time  # unit: sec
        t_start = time.time()

        [m.match() for m in self.match_zone_collection]  # force this to complete

        # dispatched vehicles which have been attached dispatch actions.
        [m.dispatch(tick) for m in self.match_zone_collection]

        #self.download_match_zone_metrics()
        # update passenger status
        [m.update_passengers() for m in self.match_zone_collection]

        self.update_vehicles(self.__t) # push routes inside if needed


        t1=time.time()
        #update charging stations...
        [cs.step(self.__dt, self.__t) for cs in self.charging_station_collections]

        cs_time=time.time()-t1

        self.enter_market()

        #after the push, call the vehicle to step for all vehicles in each zone
        # t1 = time.time()
        self.vehicle_step_update(self.__dt,self.__t) # interpolate routes and update vehicle status
        # t_v_update=time.time()-t1

        # update the demand for each matching zone

        [c.async_demand_gen(tick) for c in self.match_zone_collection]

        '''
        todo: add a download and push function for charging stations. 
        '''


        self.download_match_zone_metrics()

        STORE_TRANSITION_CYCLE=1 #store everytick
        if self.__t % STORE_TRANSITION_CYCLE == 0: # STORE_TRANSITION_CYCLE = 60*60 sec
            self.store_transitions_from_veh()
        self.__update_time()
        if self.__t % 3600 == 0:
            self.logger.info("Elapsed : {}".format(get_local_datetime(self.__t)))
        t_end = time.time() - t_start

        print('Iteration {} completed, total cpu time={:.3f}'.format(tick / 60, t_end))
Beispiel #8
0
    def update_hourly_demand(self, t, max_hours=4):
        localtime = get_local_datetime(t - 60 * 30)
        current_time = localtime.month, localtime.day, localtime.hour
        if len(self.hourly_demand) == 0 or self.current_time != current_time:
            self.current_time = current_time
            self.hourly_demand = [self.load_demand_profile(t + 60 * (60 * i - 30)) for i in range(max_hours)]

        x = (localtime.minute - 30) / 60.0
        return x
Beispiel #9
0
 def construct_time_features(self, timestamp):
     t = get_local_datetime(timestamp)
     hourofday = t.hour / 24.0 * 2 * np.pi
     dayofweek = t.weekday() / 7.0 * 2 * np.pi
     return [
         np.sin(hourofday),
         np.cos(hourofday),
         np.sin(dayofweek),
         np.cos(dayofweek)
     ]
Beispiel #10
0
    def update(self):
        # t_v_update=time.time()-t1

        self.download_match_zone_metrics()

        if self.__t % STORE_TRANSITION_CYCLE == 0:  # STORE_TRANSITION_CYCLE = 60*60 sec
            self.store_transitions_from_veh()
        self.__update_time()
        if self.__t % 3600 == 0:
            self.logger.info("Elapsed : {}".format(get_local_datetime(
                self.__t)))  # added origin UNIX time inside func.
Beispiel #11
0
 def get_customer_status(self, customer_df, bin_width=300):
     customer_df["time_bin"] = self.add_time_bin(customer_df, bin_width)
     df = customer_df.groupby(["time_bin",
                               "status"]).size().reset_index().pivot(
                                   index="time_bin",
                                   columns="status",
                                   values=0).fillna(0)
     df = df.rename(columns={2: "ride_on", 4: "rejected"})
     df["total"] = sum([x for _, x in df.iteritems()])
     df.index = [time_utils.get_local_datetime(x) for x in df.index]
     return df
Beispiel #12
0
 def load_demand_profile(t):
     localtime = get_local_datetime(t)
     dayofweek, hour = localtime.weekday(), localtime.hour
     query = """
       SELECT x, y, demand
       FROM demand_profile
       WHERE dayofweek = {dayofweek} and hour = {hour};
             """.format(dayofweek=dayofweek, hour=hour)
     demand = pd.read_sql(query, engine, index_col=["x", "y"]).demand
     M = np.zeros((MAP_WIDTH, MAP_HEIGHT))
     for (x, y), c in demand.iteritems():
         M[x, y] += c
     return M
Beispiel #13
0
    def plot_summary(self, paths, labels, plt):
        plt.figure(figsize=(12, 5))
        plt.subplots_adjust(wspace=0.2, hspace=0.4)
        for i, path in enumerate(paths):
            summary = self.load_summary_log(path)
            summary['t'] = (summary.t / 3600).astype(int) * 3600
            summary = summary.groupby('t').mean().reset_index()
            summary.t = [time_utils.get_local_datetime(t) for t in summary.t]

            plt.subplot(len(paths), 2, +i * 2 + 1)
            plt.plot(summary.t, summary.n_requests, label="request")
            plt.plot(summary.t,
                     summary.n_requests - summary.n_matching,
                     label="reject",
                     linestyle=':')
            plt.plot(summary.t,
                     summary.n_dispatch,
                     label="dispatch",
                     alpha=0.7)
            plt.ylabel("count/minute")
            plt.ylim([0, 610])
            if i != len(paths) - 1:
                plt.xticks([])
            if i == 0:
                plt.legend(loc='upper right')

            plt.subplot(len(paths), 2, i * 2 + 2)
            plt.title(labels[i])
            plt.plot(summary.t, summary.n_vehicles, label="working")
            plt.plot(summary.t,
                     summary.occupied_vehicles,
                     label="occupied",
                     linestyle=':')
            plt.ylabel("# of vehicles")
            plt.ylim([0, 10100])
            if i != len(paths) - 1:
                plt.xticks([])
            if i == 0:
                plt.legend(loc='upper right')
                # plt.subplot(313)
            # plt.plot(summary.t, summary.average_wt, alpha=1.0)
            # plt.ylim([0, 450])
            # plt.ylabel("waiting time (s)")
            # plt.xlabel("simulation time (s)")
        return plt
Beispiel #14
0
    def step(self):
        for customer in CustomerRepository.get_all():
            customer.step(self.__dt)
            if customer.is_arrived() or customer.is_disappeared():
                CustomerRepository.delete(customer.get_id())

        for vehicle in VehicleRepository.get_all():
            vehicle.step(self.__dt)
            if vehicle.exit_market():
                score = ','.join(
                    map(str, [self.get_current_time(),
                              vehicle.get_id()] + vehicle.get_score()))
                sim_logger.log_score(score)
                VehicleRepository.delete(vehicle.get_id())

        self.__populate_new_customers()
        self.__update_time()
        if self.__t % 3600 == 0:
            self.logger.info("Elapsed : {}".format(get_local_datetime(
                self.__t)))
                self.vehicle_queue.pop(0)
                self.simulator.populate_vehicle(vehicle_id, location)
            else:
                break


if __name__ == '__main__':

    start = time.time()
    # For DQN
    dispatch_policy = load()
    # setup_base_log_dir(FLAGS.tag)
    if FLAGS.days > 0:
        start_time = FLAGS.start_time + int(60 * 60 * 24 * FLAGS.start_offset)
        # print(start_time, MAX_DISPATCH_CYCLE, MIN_DISPATCH_CYCLE)
        print("Start Datetime: {}".format(get_local_datetime(start_time)))
        end_time = start_time + int(60 * 60 * 24 * FLAGS.days)
        print("End Datetime  : {}".format(get_local_datetime(end_time)))

        # For DQN
        Sim_experiment = simulator_driver(
            start_time, TIMESTEP, matching_policy.GreedyMatchingPolicy(),
            dispatch_policy, pricing_policy.PricingPolicy())
        # Sim_experiment = simulator_driver(start_time, TIMESTEP, matching_policy.GreedyMatchingPolicy())

        # header = "TimeStamp, Unix TIme, Vehicles, Occupied Vehicles, Requests, Matchings, Rejects, Accepts, Avg Wait Time per request, Avg Earnings, " \
        #          "Avg Cost, Avg Profit for DQN, Avg Profit for dummy, Avg Total Dist, Avg Capacity per vehicle, Avg Idle Time"
        # sim_logger.log_summary(header)
        # header = "TimeStamp, Request ID, Status Code, Waiting_Time"
        # sim_logger.log_customer_event(header)
        # header = "V_id', V_lat, V_lon, Speed, Status, Dest_lat, Dest_lon, Type, Travel_Dist, Price_per_travel_m, Price_per_wait_min, Gas_price,"\
Beispiel #16
0
    TIMESTEP, START_OFFSET, SIM_DAYS, START_TIME, TRAINING_CYCLE, UPDATE_CYCLE, SAVING_CYCLE
from common.time_utils import get_local_datetime
from simulator.simulator_sequential import Simulator
from dqn_agent.dqn_agent import DeepQNetworkAgent
import numpy as np
import time

# ---------------MAIN FILE---------------

if __name__ == '__main__':

    if SIM_DAYS > 0:
        start_time = START_TIME + int(
            60 * 60 * 24 * START_OFFSET)  # start_time = 0
        print("Simulate Episode Start Datetime: {}".format(
            get_local_datetime(start_time)))
        end_time = start_time + int(60 * 60 * 24 * SIM_DAYS)
        print("Simulate Episode End Datetime : {}".format(
            get_local_datetime(end_time)))

        simulator = Simulator(start_time, TIMESTEP)
        simulator.init(HEX_SHP_PATH, CS_SHP_PATH, TRIP_FILE, TRAVEL_TIME_FILE,
                       NUM_NEAREST_CS)
        dqn_agent = DeepQNetworkAgent()
        n_steps = int(3600 * 24 / TIMESTEP)  # 60 per minute
        with open('logs/parsed_results_inf.csv', 'w') as f, open(
                'logs/target_charging_stations_inf.csv',
                'w') as g, open('logs/training_hist_inf_new.csv', 'a') as h:
            f.writelines(
                '{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(
                    "time", "num_idle", "num_serving", "num_charging",
Beispiel #17
0
from config.hex_setting import HEX_SHP_PATH, CS_SHP_PATH, NUM_NEAREST_CS, TRIP_FILE, TRAVEL_TIME_FILE, \
    TIMESTEP, START_OFFSET, SIM_DAYS, START_TIME, TRAINING_CYCLE, UPDATE_CYCLE, SAVING_CYCLE
from common.time_utils import get_local_datetime
from simulator.simulator_sequential import Simulator
from dqn_agent.dqn_agent import DeepQNetworkAgent
import numpy as np
import time
# ---------------MAIN FILE---------------

if __name__ == '__main__':

    if SIM_DAYS > 0:
        start_time = START_TIME + int(60 * 60 * 24 * START_OFFSET)  # start_time = 0
        print("Simulate Episode Start Datetime: {}".format(get_local_datetime(start_time)))
        end_time = start_time + int(60 * 60 * 24 * SIM_DAYS)
        print("Simulate Episode End Datetime : {}".format(get_local_datetime(end_time)))

        simulator = Simulator(start_time, TIMESTEP)
        simulator.init(HEX_SHP_PATH, CS_SHP_PATH, TRIP_FILE, TRAVEL_TIME_FILE, NUM_NEAREST_CS)

        dqn_agent = DeepQNetworkAgent()
        n_steps = int(3600 * 24 / TIMESTEP)  # 60 per minute
        with open('logs/parsed_results_21days.csv', 'w') as f, open('logs/training_hist_21days.csv', 'w') as h:
            f.writelines(
                '{},{},{},{},{},{},{},{},{},{},{},{}\n'.format("time", "num_idle", "num_serving", "num_charging",
                                                               "num_cruising", "num_assigned", "num_waitpile",
                                                               "num_tobedisptached", "num_offduty",
                                                               "num_matches", "pass_arrivals", "removed_pass"))
            h.writelines('{},{},{},{}\n'.format("step","loss","reward","len_replay_buffer"))
            for day in range(SIM_DAYS):
                print("############################ SUMMARY ################################")