Exemplo n.º 1
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()
Exemplo n.º 2
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)))
Exemplo n.º 3
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
Exemplo n.º 4
0
    def dropoff(self, customer):
        # print(self.get_location(), self.state.destination_lat, self.state.destination_lon)
        # assert len(self.onboard_customers) > 0
        # lenC = len(self.onboard_customers)
        # print("At Dropoff!", self.get_location(), " -> ", customer.get_destination())
        # print(self.get_id(), "Dropoff", self.current_plan, self.ordered_pickups_dropoffs_ids)

        self.onboard_customers.remove(customer)
        customer.get_off()
        # print("Vid: ", self.get_id(), "'s Payment:")
        self.earnings += customer.make_payment(self.state.driver_base_per_trip)
        # self.state.travel_dist += great_circle_distance(customer.get_origin()[0], customer.get_origin()[1],
        #                                                     customer.get_destination()[0],
        #                                                     customer.get_destination()[1])

        self.state.current_capacity -= 1
        self.tmp_capacity -= 1

        if (self.state.current_capacity == 0) | (len(self.current_plan) == 0):
            # print(self.get_id(), "IDLE!")
            self.change_to_idle()
            self.state.status = status_codes.V_IDLE
            self.state.price_per_travel_m /= self.num_trip_customers
            self.num_trip_customers = 0
            self.accepted_customers = []
            self.onboard_customers = []
            # latest_cust_id = self.state.assigned_customer_id.pop(0)
            self.reset_plan()

        else:
            self.nxt_stop = self.current_plan.pop(0)
            pick_drop = self.pickup_flags[0]
            # routes = self.routing_engine.route([(self.get_location(), self.nxt_stop)])
            route, triptime = self.current_plan_routes.pop(0)
            # print("Dropoff: ", triptime, route)
            # print(vehicle.get_location(), vehicle.current_plan[0])
            if triptime == 0.0:
                self.pickup_flags.pop(0)
                id = self.ordered_pickups_dropoffs_ids.pop(0)
                # speed = self.compute_speed(route, triptime)
                # self.set_route(route, speed)
                # self.set_destination(route[-1], triptime)
                self.state.lat, self.state.lon = self.nxt_stop
                if pick_drop == 1:
                    if self.get_location() != CustomerRepository.get(id).get_origin():
                        self.state.lat, self.state.lon = CustomerRepository.get(id).get_origin()
                    self.pickup(CustomerRepository.get(id))
                else:
                    if self.get_location() != CustomerRepository.get(id).get_destination():
                        self.state.lat, self.state.lon = CustomerRepository.get(id).get_destination()
                    self.dropoff(CustomerRepository.get(id))
            else:
                # print("Dropoff: ", len(self.current_plan), len(self.ordered_pickups_dropoffs_ids))
                speed = self.compute_speed(route, triptime)
                self.set_route(route, speed)
                self.set_destination(route[-1], triptime)

        self.__log()
Exemplo n.º 5
0
    def pickup(self, customer):
        # print("At Pickup!", self.get_location(), " -> ", customer.get_origin())
        # print(self.get_id(), "Pickup: ", self.current_plan, self.ordered_pickups_dropoffs_ids)
        customer.ride_on()
        # customer_id = customer.get_id()
        # self.__reset_plan() # For now we don't consider routes of occupied trip
        self.onboard_customers.append(customer)
        self.num_trip_customers += 1
        self.state.current_capacity += 1
        self.tmp_capacity += 1

        if len(self.current_plan) == 0:
            print("pickup -> NEVER")
            self.change_to_idle()
            self.state.price_per_travel_m /= self.num_trip_customers
            self.num_trip_customers = 0
            self.accepted_customers = []
            self.onboard_customers = []
            self.reset_plan()

        else:
            self.nxt_stop = self.current_plan.pop(0)
            pick_drop = self.pickup_flags[0]
            # routes = self.routing_engine.route([(self.get_location(), self.nxt_stop)])
            route, triptime = self.current_plan_routes.pop(0)
            # print("Pickup: ", triptime, route)
            # print(vehicle.get_location(), vehicle.current_plan[0])
            # If already there at nxt stop
            if triptime == 0.0:
                self.pickup_flags.pop(0)
                id = self.ordered_pickups_dropoffs_ids.pop(0)
                self.state.lat, self.state.lon = self.nxt_stop
                if pick_drop == 1:
                    if self.get_location() != CustomerRepository.get(id).get_origin():
                        self.state.lat, self.state.lon = CustomerRepository.get(id).get_origin()
                    self.pickup(CustomerRepository.get(id))
                else:
                    if self.get_location() != CustomerRepository.get(id).get_destination():
                        self.state.lat, self.state.lon = CustomerRepository.get(id).get_destination()
                    self.dropoff(CustomerRepository.get(id))
            else:
                speed = self.compute_speed(route, triptime)
                self.set_route(route, speed)
                self.set_destination(route[-1], triptime)

            # self.state.assigned_customer_id = customer_id
            # triptime = customer.get_trip_duration()
            # self.__set_destination(customer.get_destination(), triptime)
            self.__set_pickup_time(triptime)
            self.change_to_occupied()
        # self.state.current_capacity += 1
        self.__log()
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
0
 def get_customers(self):
     return CustomerRepository.get_all()
Exemplo n.º 9
0
 def get_new_requests(self):
     return CustomerRepository.get_new_requests()
Exemplo n.º 10
0
 def __populate_new_customers(self):
     new_customers = self.demand_generator.generate(self.__t, self.__dt)
     CustomerRepository.update_customers(new_customers)
Exemplo n.º 11
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
Exemplo n.º 12
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