Пример #1
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)))
Пример #2
0
 def reset(self, start_time=None, timestep=None):
     if start_time is not None:
         self.__t = start_time
     if timestep is not None:
         self.__dt = timestep
     VehicleRepository.init()
     CustomerRepository.init()
Пример #3
0
    def dispatch_vehicles(self, commands):
        # print("D: ", commands)
        od_pairs = []
        vehicles = []
        # Comamnd is a dictionary created in dummy_agent
        for command in commands:
            vehicle = VehicleRepository.get(command["vehicle_id"])
            if vehicle is None:
                self.logger.warning("Invalid Vehicle id")
                continue

            if "offduty" in command:
                off_duration = self.sample_off_duration()  # Rand time to rest
                vehicle.take_rest(off_duration)
            elif "cache_key" in command:
                l, a = command["cache_key"]
                route, triptime = self.routing_engine.get_route_cache(l, a)
                vehicle.cruise(route, triptime)
            else:
                vehicles.append(vehicle)
                od_pairs.append(
                    (vehicle.get_location(), command["destination"]))

        routes = self.routing_engine.route(od_pairs)

        for vehicle, (route, triptime) in zip(vehicles, routes):
            if triptime == 0:
                continue
            vehicle.cruise(route, triptime)
Пример #4
0
    def init_price(self, match_commands):
        m_commands = []
        for c in match_commands:
            vehicle = VehicleRepository.get(c["vehicle_id"])
            # vehicle.state.status = status_codes.V_ASSIGNED
            if vehicle is None:
                print("Invalid Vehicle id")
                continue
            customer = CustomerRepository.get(c["customer_id"])
            if customer is None:
                print("Invalid Customer id")
                continue

            triptime = c["duration"]

            # if FLAGS.enable_pricing:
            dist_for_pickup = c["distance"]
            dist_till_dropoff = great_circle_distance(
                customer.get_origin()[0],
                customer.get_origin()[1],
                customer.get_destination()[0],
                customer.get_destination()[1])
            total_trip_dist = dist_for_pickup + dist_till_dropoff
            [travel_price, wait_price] = vehicle.get_price_rates()
            # v_cap = vehicle.state.current_capacity
            initial_price = calculate_price(total_trip_dist, triptime,
                                            vehicle.state.mileage,
                                            travel_price, wait_price,
                                            vehicle.state.gas_price,
                                            vehicle.state.driver_base_per_trip)
            c["init_price"] = initial_price
            m_commands.append(c)
            # print(c)

        return m_commands
Пример #5
0
    def populate_vehicle(self, vehicle_id, location):
        type = 0
        r = randrange(2)
        if r == 0 and self.current_dummyV < FLAGS.dummy_vehicles:
            type = agent_codes.dummy_agent
            self.current_dummyV += 1

        # If r = 1 or num of dummy agent satisfied
        elif self.current_dqnV < FLAGS.dqn_vehicles:
            type = agent_codes.dqn_agent
            self.current_dqnV += 1

        else:
            type = agent_codes.dummy_agent
            self.current_dummyV += 1

        VehicleRepository.populate(vehicle_id, location, type)
    def predict_best_action(self, vehicle_id, vehicle_state):
        if vehicle_state.idle_duration >= MIN_DISPATCH_CYCLE and FLAGS.offduty_probability > np.random.random(
        ):
            a, offduty = (0, 0), 1

        elif self.q_network is None:
            a, offduty = (0, 0), 0

        else:
            x, y = mesh.convert_lonlat_to_xy(vehicle_state.lon,
                                             vehicle_state.lat)
            if (x, y) in self.q_cache:
                actions, Q, amax = self.q_cache[(x, y)]
            else:
                # Get state features and action features
                s, actions = self.feature_constructor.construct_current_features(
                    x, y)
                # print("A: ", actions, len(actions))
                # print("S: ", s)
                # Calculate Q values based on state features
                Q = self.q_network.compute_q_values(s)
                # print("Q values: ", Q)
                # only considers actions whose values are greater than wait action value
                wait_action_value = Q[0]
                actions = [
                    a for a, q in zip(actions, Q) if q >= wait_action_value
                ]
                Q = Q[Q >= wait_action_value]
                # print("Q ba: ", Q)
                amax = np.argmax(Q)  # Get the index of the max value
                # print("Q max, val, max: ", amax, Q[amax], max(Q))
                self.q_cache[(x, y)] = actions, Q, amax  # Save in cache
            # if actions[amax] == (0, 0):
            #     aidx = amax
            # else:
            aidx = self.q_network.get_action(
                Q, amax)  # Get action with max Q value
            a = actions[aidx]
            offduty = 1 if Q[aidx] < FLAGS.offduty_threshold else 0
            # print("Chosen A: ", a)
            vehicle = VehicleRepository.get(vehicle_id)
            # tmp_q_action = {a:q for a, q in zip(actions, Q)}
            q_action = {(x + ax, y + ay): q for (ax, ay), q in zip(actions, Q)}
            vehicle.q_action_dict = q_action
            vehicle.epsilon = int(len(vehicle.q_action_dict) * 0.05)
            # print("Loc: ", x, " , ", y)
            # print("Act: ", tmp_q_action)
            # print("Added:", vehicle.q_action_dict)
        return a, offduty
Пример #7
0
    def init_price(self, match_commands):
        m_commands = []
        for c in match_commands:
            vehicle = VehicleRepository.get(c["vehicle_id"])
            # vehicle.state.status = status_codes.V_ASSIGNED
            if vehicle is None:
                print("Invalid Vehicle id")
                continue
            customer = CustomerRepository.get(c["customer_id"])
            if customer is None:
                print("Invalid Customer id")
                continue

            triptime = c["duration"]

            # if FLAGS.enable_pricing:
            dist_for_pickup = c["distance"]

            od_route = self.routing_engine.route_time([
                (customer.get_origin(), customer.get_destination())
            ])
            # dist_dropoff = great_circle_distance(customer.get_origin()[0], customer.get_origin()[1],
            #                               customer.get_destination()[0], customer.get_destination()[1])
            route, time = od_route[0]
            lats, lons = zip(*route)
            distance = geoutils.great_circle_distance(
                lats[:-1], lons[:-1], lats[1:], lons[1:])  # Distance in meters
            dist_till_dropoff = sum(distance)

            # print(dist_dropoff, dist_till_dropoff)

            total_trip_dist = dist_for_pickup + dist_till_dropoff
            [travel_price, wait_price] = vehicle.get_price_rates()
            # v_cap = vehicle.state.current_capacity
            initial_price = calculate_price(total_trip_dist, triptime,
                                            vehicle.state.mileage,
                                            travel_price, wait_price,
                                            vehicle.state.gas_price,
                                            vehicle.state.driver_base_per_trip)
            c["init_price"] = initial_price
            m_commands.append(c)
            # print(c)

        return m_commands
Пример #8
0
 def startup_dispatch(self, current_time, vehicles):
     self.dispatch_policy.update_state(current_time, vehicles)
     if FLAGS.train:
         self.dispatch_policy.give_rewards(vehicles)
     dispatch_commands = self.dispatch_policy.get_dispatch_decisions(vehicles)
     self.dispatch_policy.record_dispatch(vehicles.index, current_time)
     for vid in vehicles.index:
         vehicle = VehicleRepository.get(vid)
         vehicle.first_dispatched = 1
     ######### Only with the DQN agent not the dummy #############
     if FLAGS.train:
         self.dispatch_policy.backup_supply_demand()
         # If size exceeded, run training
         if len(self.dispatch_policy.supply_demand_history) > INITIAL_MEMORY_SIZE:
             average_loss, average_q_max = self.dispatch_policy.train_network(FLAGS.batch_size)
             # print("iterations : {}, average_loss : {:.3f}, average_q_max : {:.3f}".format(
             #     self.q_network.n_steps, average_loss, average_q_max), flush=True)
             self.dispatch_policy.q_network.write_summary(average_loss, average_q_max)
     return dispatch_commands
 def give_rewards(self, vehicles):
     for vehicle_id, row in vehicles.iterrows():
         vehicle = VehicleRepository.get(vehicle_id)
         earnings = row.earnings - self.last_earnings.get(vehicle_id, 0)
         cost = vehicle.compute_fuel_consumption()
         if earnings > 0:
             cost -= self.last_cost.get(vehicle_id, 0)
             profit = earnings - cost
         else:
             profit = earnings
         # print("Earnings: ", earnings, "Cost: ", cost, "Profit: ", profit)
         self.rewards[vehicle_id] += (
             12 * profit
         ) - 5 * row.pickup_time + 10 * settings.STATE_REWARD_TABLE[
             row.status]
         # self.rewards[vehicle_id] += earnings + settings.STATE_REWARD_TABLE[row.status]
         self.last_earnings[vehicle_id] = row.earnings
         if earnings > 0:
             self.last_cost[vehicle_id] = vehicle.compute_fuel_consumption()
         if row.status == status_codes.V_OFF_DUTY:
             self.last_state_actions[vehicle_id] = None
Пример #10
0
 def get_vehicles(self):
     return VehicleRepository.get_all()
Пример #11
0
 def get_vehicles_state(self):
     return VehicleRepository.get_states()
Пример #12
0
    def match_RS(self, current_time, vehicles, requests):
        # print("SA: Inside GreedyMatching Match ", "V:", len(vehicles), "R:", len(requests))
        commands = []
        requestAssigned = defaultdict(list)

        vehicles, cap_list = self.find_available_vehicles_RS(vehicles)

        n_vehicles = len(vehicles)
        # nV = 4 * len(requests)
        nV = sum(cap_list)  ######### Updated based on capacity
        # print(nV)
        if (n_vehicles > nV):
            vehicles = vehicles.iloc[range(nV), :]

        # print("SA: Inside GreedyMatching Match ", "V:", len(vehicles), "R:", len(requests))

        if n_vehicles == 0:
            return commands

        # print("Available Vehicles and grid locations")
        v_latlon = vehicles[["lat", "lon"]]
        V = defaultdict(list)
        vid2coord = {}
        all_candidate_vids = []
        # all_candidate_vidx = {}
        # vcnt = 0
        for vid, row in v_latlon.iterrows():
            # print("AAAA")
            # print(vehicles.loc[vid])
            # print(vid, row.lon, row.lat)
            coord = self.get_coord(row.lon, row.lat)
            vid2coord[vid] = coord
            # print(vid, coord)
            V[coord].append(vid)
            all_candidate_vids.append(vid)
            # all_candidate_vidx[vid] = vcnt
            # vcnt = vcnt+1

        # print("Requests and Grid locations")
        r_latlon = requests[["origin_lat", "origin_lon"]]
        R = defaultdict(list)
        all_target_rids = []
        for rid, row in r_latlon.iterrows():
            coord = self.get_coord(row.origin_lon, row.origin_lat)
            # print(rid, coord)
            R[coord].append(rid)
            all_target_rids.append(rid)
        # print("Candidate IDs: ", all_candidate_vids)
        # print("Target IDs: ", all_target_rids)
        # Dictionary of vehicle IDs and their index in the candidate list
        all_candidate_vidx = dict(
            zip(all_candidate_vids, range(len(all_candidate_vids))))
        # print("All Candidates: ", all_candidate_vidx, len(all_candidate_vids))
        all_target_ridx = dict(
            zip(all_target_rids, range(len(all_target_rids))))
        # print("All targets: ", all_target_ridx, len(all_target_rids))
        target_ridx = [all_target_ridx[r] for r in all_target_rids]

        reject_range = int(
            self.reject_distance / self.unit_length / self.k) + 1

        d_latlon = requests[["destination_lon", "destination_lat"]]

        candidate_latlon = v_latlon.loc[all_candidate_vids]
        # print("Cand List: ", len(candidate_latlon))
        all_target_latlon = r_latlon.loc[all_target_rids]
        # print("Target List: ", len(all_target_latlon))
        all_destination_latlon = r_latlon.loc[all_target_rids]
        T, dist = self.eta_matrix(candidate_latlon, all_target_latlon)
        # print("match_D: ", dist, dist.shape)
        TRR, Dist_RR = self.eta_matrix(all_target_latlon, all_target_latlon)
        TDD, Dist_DD = self.eta_matrix(all_destination_latlon,
                                       all_destination_latlon)

        # for vid, row in vehicles.iterrows():
        # row.earnings = 10
        # print(type(row))
        # print(vehicles[["id"]])
        # print(vid, row.earnings, row.lon, row.lat, row.status)
        for coord in self.coord_iter():
            # print("SA: Inside COORD")
            # print(dist.shape)
            # print(T.shape)
            if not R[coord]:
                # print(coord, "does not exist")
                continue
            # print(coord, "exists")
            # for i in range(int(np.ceil(len(R[coord]) / self.max_locations))):
            # target_rids = R[coord][i * self.max_locations : (i + 1) * self.max_locations]
            # for i in range(int(np.ceil(len(R[coord]) / self.max_locations))):
            target_rids = R[coord]
            # print("TR: ", target_rids)
            candidate_vids = self.find_candidates(coord, len(target_rids), V,
                                                  reject_range)
            # print("Cand. Vehicles: ", len(candidate_vids))
            if len(candidate_vids) == 0:
                continue

            target_latlon = r_latlon.loc[target_rids]
            candidate_vids = self.filter_candidates(
                v_latlon.loc[candidate_vids], target_latlon)
            # print("Filtered Vehicles: ", len(candidate_vids))
            if len(candidate_vids) == 0:
                continue

            requestAssigned = defaultdict(list)
            # All candiaidate vehicles that can pickup this specific customer
            candidate_vidx = [all_candidate_vidx[v] for v in candidate_vids]
            assignments = self.assign_nearest_vehicle_RideShare(
                target_rids, all_target_ridx, requestAssigned, candidate_vids,
                candidate_vidx, T.T, TRR, TDD, dist.T, Dist_RR, Dist_DD)
            for vid, rid, tt, d in assignments:
                commands.append(self.create_matching_dict(vid, rid, tt, d))
                # vehicles[vid].current_capacity = vehicles[vid].current_capacity +1
                # print("COORD:", coord, "V ", vid, "assigned to R", rid)
                vehicle = VehicleRepository.get(vid)
                # vehicle.state.current_capacity += 1
                # vehicle.print_vehicle()
                # print(vehicle)

                # vehicles[vid].update_capacity()
                # if vid in V[vid2coord[vid]]:
                #   V[vid2coord[vid]].remove(vid)
                if vehicle.state.current_capacity >= vehicle.state.max_capacity and vid in V[
                        vid2coord[vid]]:
                    V[vid2coord[vid]].remove(vid)

        return commands
Пример #13
0
                        # print("B: ", len(requests), len(r_df))
                        requests = requests.append(r_df, ignore_index=True)

                sum_requests += len(requests)
                requests = requests.set_index("id")
                # print("R After: ", len(requests))

                current_time = Sim_experiment.simulator.get_current_time()

                # For DQN
                if FLAGS.enable_pricing:
                    # print("All: ", len(vehicles), m)
                    if len(vehicles) > 0:
                        new_vehicles = vehicles.loc[[
                            vehicle_id for vehicle_id in vehicles.index
                            if VehicleRepository.get(
                                vehicle_id).first_dispatched == 0
                        ]]
                        startup_dispatch = Sim_experiment.dqn_agent.startup_dispatch(
                            current_time, new_vehicles)
                        Sim_experiment.simulator.dispatch_vehicles(
                            startup_dispatch)
                        # print("Done", len(new_vehicles))

                if len(vehicles) == 0:
                    continue
                else:
                    # print("V1: ", len(vehicles))
                    m_commands, vehicles = Sim_experiment.central_agent.get_match_commands(
                        current_time, vehicles, requests)
                    # print("V2: ", len(vehicles))
Пример #14
0
    def match_vehicles(self, commands, dqn_agent, dummy_agent):
        # print("M: ", commands)
        vehicle_list = []
        rejected_requests = []
        accepted_commands = {}
        reject_count = 0
        # Comamnd is a dictionary created in dummy_agent
        # print("########################################################")
        for command in commands:
            rejected_flag = 0
            # print(command["vehicle_id"], command["customer_id"])
            vehicle = VehicleRepository.get(command["vehicle_id"])
            # vehicle.state.status = status_codes.V_ASSIGNED
            if vehicle is None:
                self.logger.warning("Invalid Vehicle id")
                continue
            customer = CustomerRepository.get(command["customer_id"])
            if customer is None:
                self.logger.warning("Invalid Customer id")
                continue

            triptime = command["duration"]
            vid = command["vehicle_id"]
            # print("Maching: Vehicle " + vehicle.to_string() + " ---> " + customer.to_string())

            price_response = command["init_price"]
            if FLAGS.enable_pricing:
                # For DQN
                if vehicle.state.agent_type == agent_codes.dummy_agent:
                    price_response = dummy_agent.get_price_decision(
                        vehicle, command["init_price"], customer.get_request())

                elif vehicle.state.agent_type == agent_codes.dqn_agent:
                    price_response = dqn_agent.get_price_decision(
                        vehicle, command["init_price"], customer.get_request())
                # price_response = initial_price

                # print("Diff: ", (price_response-initial_price))

                # Now, customer needs to calculate reward and accpet or reject
                if customer.accpet_reject_ride(
                        price_response, vehicle.state,
                        triptime):  # If customer accepts
                    vehicle.head_for_customer(customer.get_origin(), triptime,
                                              customer.get_id(),
                                              command["distance"])
                    customer.wait_for_vehicle(triptime)
                    v = VehicleRepository.get(vid)
                    v.state.current_capacity += 1
                    accepted_commands[reject_count] = command
                    # print(command)
                    # print(accepted_commands)
                    reject_count += 1
                    # print(len(accepted_commands))

                else:  # base case: request drops
                    rejected_flag = 1
                    customer.go_to_nxt_timestep = 1
                    rejected_requests.append(customer.get_request())

            else:
                customer.accepted_price = command["init_price"]
                vehicle.head_for_customer(customer.get_origin(), triptime,
                                          customer.get_id(),
                                          command["distance"])
                customer.wait_for_vehicle(triptime)
                v = VehicleRepository.get(vid)
                v.state.current_capacity += 1
                accepted_commands = commands
                # vehicle.state.status = status_codes.V_ASSIGNED

            if FLAGS.enable_pooling:
                if vid not in vehicle_list and not rejected_flag:
                    vehicle_list.append(vid)

        if FLAGS.enable_pooling:
            for vi in vehicle_list:
                vehicle = VehicleRepository.get(vi)
                # vehicle.print_vehicle()
                vehicle.change_to_assigned()

        return rejected_requests, accepted_commands
Пример #15
0
    def init_price(self, match_commands, charging_commands):
        m_commands = []
        for m in match_commands:
            vehicle = VehicleRepository.get(m["vehicle_id"])
            # vehicle.state.status = status_codes.V_ASSIGNED
            if vehicle is None:
                print("Invalid Vehicle id")
                continue
            customer = CustomerRepository.get(m["customer_id"])
            if customer is None:
                print("Invalid Customer id")
                continue

            triptime = m["duration"]

            # if FLAGS.enable_pricing:
            dist_for_pickup = m["distance"]
            dist_till_dropoff = great_circle_distance(
                customer.get_origin()[0],
                customer.get_origin()[1],
                customer.get_destination()[0],
                customer.get_destination()[1])
            total_trip_dist = dist_for_pickup + dist_till_dropoff
            [travel_price, wait_price] = vehicle.get_price_rates()
            # v_cap = vehicle.state.current_capacity
            initial_price = calculate_price(total_trip_dist, triptime,
                                            vehicle.state.mileage,
                                            travel_price, wait_price,
                                            vehicle.state.gas_price,
                                            vehicle.state.driver_base_per_trip)
            m["init_price"] = initial_price
            m_commands.append(m)

        # matching_charging_station
        c_commands = []  # charging command
        for c in charging_commands:
            vehicle = VehicleRepository.get(c["vehicle_id"])
            if vehicle is None:
                print("Invalid Vehicle id")
                continue
            charging_pile = ChargingRepository.get_charging_station(
                c["customer_id"])

            # charging_pile = df_charging_piles["customer_id"] # return to that row
            if charging_pile is None:
                print("Invalid Customer id")
                continue
            charging_incentives = 0  # charging_pile.get_incentive()

            triptime = c["duration"]
            # if FLAGS.enable_pricing:
            dist_to_cs = c["distance"]
            total_trip_dist = dist_to_cs
            [travel_price, wait_price] = vehicle.get_price_rates()
            # v_cap = vehicle.state.current_capacity
            initial_price = calculate_price(total_trip_dist, triptime,
                                            vehicle.state.mile_of_range,
                                            travel_price, wait_price,
                                            vehicle.state.full_charge_price,
                                            vehicle.state.driver_base_per_trip)
            c["init_price"] = initial_price + charging_incentives
            c_commands.append(c)

        return m_commands, c_commands
Пример #16
0
    def match_vehicles(self, commands, dqn_agent, dummy_agent):
        # print("M: ", commands)
        vehicle_list = []
        rejected_requests = []
        accepted_commands = []
        num_accepted = 0
        # reject_count = 0
        vehicle_accepted_cust = defaultdict(list)
        # od_accepted_pairs = []
        # Comamnd is a dictionary created in dummy_agent
        # print("########################################################")
        for command in commands:
            rejected_flag = 0
            # print(command["vehicle_id"], command["customer_id"])
            vehicle = VehicleRepository.get(command["vehicle_id"])
            vid = command["vehicle_id"]
            # print("V_Loc: ", vehicle.get_location())
            # vehicle.state.status = status_codes.V_ASSIGNED
            if vehicle is None:
                self.logger.warning("Invalid Vehicle id")
                continue
            # print("Vid: ", vid, "Plan: ", vehicle.current_plan)
            # vehicle_cust_price_time = dict()

            if (vehicle.state.status == status_codes.V_OCCUPIED
                ) & vehicle.state.accept_new_request:
                # print(vid, "Update", vehicle.current_plan, vehicle.get_destination())
                if (len(vehicle.current_plan)
                        == 0) & (vehicle.state.destination_lat is not None):
                    # print(vid, "Dest: ", vehicle.get_destination())
                    vehicle.current_plan = [vehicle.get_destination()]
                    vehicle.current_plan_routes = [
                        (vehicle.get_route(),
                         vehicle.state.time_to_destination)
                    ]

                elif (len(vehicle.current_plan)
                      == 0) & (vehicle.state.destination_lat is None):
                    vehicle.change_to_idle()
                    vehicle.reset_plan()

                elif (len(vehicle.current_plan) != 0) & (
                        vehicle.get_destination() != vehicle.current_plan[0]):
                    # print(vid, ": ", vehicle.get_destination(), vehicle.current_plan[0])
                    if vehicle.state.destination_lat is not None:
                        plan = [vehicle.get_destination()]
                        plan.extend(vehicle.current_plan)
                        vehicle.current_plan = np.copy(plan).tolist()

                        if len(vehicle.get_route()) == 0:
                            # print(vid, "Empty Route!!", vehicle.get_destination())
                            # print(vehicle.current_plan)
                            # print(vehicle.pickup_flags)
                            # print(vehicle.ordered_pickups_dropoffs_ids)
                            # print(vehicle.current_plan_routes)

                            od_routes = self.routing_engine.route_time([
                                (vehicle.get_location(),
                                 vehicle.get_destination())
                            ])
                            routes = [od_routes[0]]
                        else:

                            routes = [[
                                vehicle.get_route(),
                                vehicle.state.time_to_destination
                            ]]

                        routes.extend(vehicle.current_plan_routes)
                        vehicle.current_plan_routes = np.copy(routes).tolist()

                        # if len(vehicle.get_route()) == 0:
                        #     print("R: ", vehicle.current_plan_routes)

                if len(vehicle.current_plan) != len(vehicle.current_plan_routes) != len(\
                        vehicle.ordered_pickups_dropoffs_ids) != len(vehicle.pickup_flags):
                    print("ERROR!")

            prev_cost = 0
            # For each vehicle
            # Need to calculate route (and order customer list) before heading to customer
            for index in range(len(command["customer_id"])):
                customer = CustomerRepository.get(
                    command["customer_id"][index])
                if customer is None:
                    self.logger.warning("Invalid Customer id")
                    continue

                prev_plan = np.copy(vehicle.current_plan)
                prev_flags = np.copy(vehicle.pickup_flags)
                prev_ids = np.copy(vehicle.ordered_pickups_dropoffs_ids)
                prev_routes = np.copy(vehicle.current_plan_routes)

                vehicle_accepted_cust[vid].append(
                    command["customer_id"][index])

                insertion_cost, waiting_time = self.generate_plan(
                    vehicle, vehicle_accepted_cust[vid], customer)

                if len(vehicle_accepted_cust[vid]) > 1:
                    # print("prev: ", prev_cost, insertion_cost)
                    insertion_cost = abs(insertion_cost - prev_cost)

                if insertion_cost == float(0):
                    insertion_cost = command["init_price"][index]
                # print("L: ", len(vehicle_accepted_cust[vid]))
                # print("C: ", insertion_cost)

                [travel_price, wait_price] = vehicle.get_price_rates()

                init_price = calculate_price(
                    insertion_cost, waiting_time, vehicle.state.mileage,
                    travel_price, wait_price, vehicle.state.gas_price,
                    vehicle.state.driver_base_per_trip)

                # print("P: ", init_price, command["init_price"][index])
                command["duration"][index] = waiting_time

                command["init_price"][index] = init_price

                # print("A: ", command)

                if FLAGS.enable_pricing:
                    # For DQN
                    if vehicle.state.agent_type == agent_codes.dummy_agent:
                        price_response = dummy_agent.get_price_decision(
                            vehicle, init_price, customer.get_request())

                    elif vehicle.state.agent_type == agent_codes.dqn_agent:
                        price_response = dqn_agent.get_price_decision(
                            vehicle, init_price, customer.get_request())
                    # price_response = initial_price

                    # print("Diff: ", (price_response-initial_price))

                    # Now, customer needs to calculate reward and accept or reject
                    if customer.accpet_reject_ride(
                            price_response, vehicle.state,
                            waiting_time):  # If customer accepts
                        num_accepted += 1
                        # if not FLAGS.enable_pooling:
                        #     vehicle.head_for_customer(customer.get_origin(), waiting_time, customer.get_id(), command["distance"][index])
                        vehicle.accepted_customers.append(
                            command["customer_id"][index])
                        # vehicle.accepted_customers.append([customer, triptime, price_response, command["distance"][index]])

                        customer.wait_for_vehicle(waiting_time)
                        prev_cost = insertion_cost

                        # vehicle_accepted_cust[vid].append(command["customer_id"][index])
                        # vehicle.state.current_capacity += 1
                        accepted_commands.append(command)
                        # print("Accepted, cust: ", customer.get_id(), " ", vehicle.current_plan)

                    else:
                        # rejected_flag = 1
                        # reject_count += 1
                        customer.go_to_nxt_timestep = 1
                        rejected_requests.append(customer.get_request())
                        # if len(prev_plan) == 0:
                        #     print("Should be Empty!")
                        vehicle.current_plan = prev_plan.tolist()
                        vehicle.pickup_flags = prev_flags.tolist()
                        vehicle.ordered_pickups_dropoffs_ids = prev_ids.tolist(
                        )
                        vehicle.current_plan_routes = prev_routes.tolist()
                        # print("B: ", vehicle.accepted_customers, vehicle_accepted_cust[vid])
                        # print("Reject: ", vid, " ", customer.get_id())
                        vehicle_accepted_cust[vid].pop()
                        # vehicle.accepted_customers.pop(0)
                        # print("A: ", vehicle_accepted_cust[vid], len(vehicle_accepted_cust[vid]), vehicle.accepted_customers)
                        # print("Rejected, cust: ", customer.get_id(), " ", vehicle.current_plan)

                else:
                    customer.accepted_price = init_price
                    # if not FLAGS.enable_pooling:
                    #     vehicle.head_for_customer(customer.get_origin(), triptime, customer.get_id(), command["distance"][index])
                    vehicle.accepted_customers.append(
                        command["customer_id"][index])
                    # vehicle.accepted_customers.append([customer, triptime, price_response, command["distance"][index]])
                    customer.wait_for_vehicle(waiting_time)
                    prev_cost = insertion_cost
                    accepted_commands = commands
                    # od_accepted_pairs = pairs
                    # vehicle.state.status = status_codes.V_ASSIGNED

            if FLAGS.enable_pooling:
                # print("vid: ", vehicle.get_id(), "Accepted: ", len(vehicle_accepted_cust[vid]))
                vehicle.tmp_capacity = vehicle.state.current_capacity
                # if vid not in vehicle_list and len(vehicle_accepted_cust[vid]) != 0:
                #     vehicle_list.append(vid)

                if len(vehicle.current_plan) == 0:
                    # print(vid, "EMPTYYYYYY!")
                    continue

                else:
                    route, triptime = vehicle.current_plan_routes.pop(0)
                    vehicle.nxt_stop = vehicle.current_plan.pop(0)
                    if len(route) == 0:
                        # print("B: ", triptime, len(vehicle.current_plan), len(vehicle.ordered_pickups_dropoffs_ids),
                        #       len(vehicle.current_plan_routes))
                        r2 = self.routing_engine.route_time([
                            (vehicle.get_location(), vehicle.nxt_stop)
                        ])
                        route, triptime = r2[0]
                        # print("Updated: ", triptime, len(route))
                    # print("Loc: ", vehicle.get_location(), "Nxt: ", vehicle.nxt_stop)
                    cust_id = vehicle.ordered_pickups_dropoffs_ids[0]
                    if triptime == 0.0:
                        # vehicle.current_plan.pop(0)
                        pick_drop = vehicle.pickup_flags.pop(0)
                        cust_id = vehicle.ordered_pickups_dropoffs_ids.pop(0)
                        # print("routes: ", routes)

                        vehicle.state.assigned_customer_id = cust_id
                        if pick_drop == 1:
                            vehicle.state.lat, vehicle.state.lon = CustomerRepository.get(
                                cust_id).get_origin()
                            vehicle.pickup(CustomerRepository.get(cust_id))
                        else:
                            vehicle.state.lat, vehicle.state.lon = CustomerRepository.get(
                                cust_id).get_destination()
                            vehicle.dropoff(CustomerRepository.get(cust_id))
                    else:
                        vehicle.head_for_customer(triptime, cust_id, route)
                        # vehicle.nxt_stop = vehicle.current_plan[0]
                        vehicle.change_to_assigned()

        return rejected_requests, accepted_commands, num_accepted
Пример #17
0
    def assign_nearest_vehicle_RideShare(self, request_ids, all_target_ridx,
                                         requestAssigned, vehicle_ids,
                                         vehicle_idx, T, TRR, TDD, dist, d_RR,
                                         d_DD):
        assignments = []
        # for ri, rid in enumerate(request_ids):
        # vi = T[ri,vehicle_idx].argmin()
        # tt = T[ri,vehicle_idx][vi]
        # for rid in request_ids:
        # print(len(request_ids), type(request_ids))
        # print(len(all_target_ridx), len(vehicle_ids), len(vehicle_idx))

        # for ri, rid in enumerate(request_ids):
        for rid in request_ids:
            ri = all_target_ridx[rid]
            # For this request ri, pick the min from all candidate vehicles
            vi = T[ri, vehicle_idx].argmin()
            tt = T[ri, vehicle_idx][vi]

            # print("D: ", dist, "shape: ", dist.shape)
            # print("Ri:", ri,  "Vi: ", vehicle_idx, len(vehicle_idx))
            # print("Filtered: ", dist[ri, vehicle_idx])
            # print(type(dist))
            di = dist[ri, vehicle_idx].argmin()
            dd = dist[ri, vehicle_idx][di]
            # else:
            # dd = dist

            # print(T[ri])
            # print(vehicle_idx)
            # print(T[ri,vehicle_idx])
            # print("RTT","Ri",ri,"Vi",vi,vehicle_ids,tt)
            if tt > self.reject_wait_time:
                continue

            vid = vehicle_ids[vi]
            vi = vehicle_idx[vi]

            # print("RTT","VID",vid,"Vix",vi)

            vehicle = VehicleRepository.get(vid)
            if (vehicle.reachedCapacity() == False):
                if (len(requestAssigned[vid]) == 0):
                    requestAssigned[vid].append(rid)
                    # vehicle.state.current_capacity += 1
                    # print("1: ", dd)
                    assignments.append((vid, rid, tt, dd))
                    # print("TT",T[:, vi])
                    # print("TRR",TRR[:, ri])
                    T[:, vi] = T[:, vi] + TRR[:, ri]
                    dist[:, vi] = d_RR[:, ri]
                    # T[ri, vi] = T[ri, vi] + tt
                    # print("TTA",T[:, vi])
                else:
                    ll = list(requestAssigned[vid])
                    # print("LL",ll)
                    ll.append(rid)
                    # print("RS",requestAssigned[vid])
                    ll_x = [all_target_ridx[r] for r in ll]
                    maxdd = TDD[ll_x][:, ll_x].max()
                    # extra_d = d_DD[ll_x][:, ll_x].max()
                    # print("LLAfter",ll, "Max Time",maxdd,flush=True)
                    if (maxdd <= self.reject_wait_time):
                        requestAssigned[vid].append(rid)
                        # vehicle.increaseCapacity()
                        assignments.append((vid, rid, tt, dd))
                        # print("Nxt: ", dd)
                        # print("TT",T[:, vi])
                        T[:, vi] = T[:, vi] + TRR[:, ri]
                        dist[:, vi] = d_RR[:, ri]
                        # T[ri, vi] = T[ri, vi] + tt
                        # print("TTA",T[:, vi])
            else:
                T[:, vi] = float('inf')
                dist[:, vi] = float('inf')
        return assignments