def create_tt_tensor(engine, reachable_map): origin_destins_list = [] for x, y in state_space: origin = convert_xy_to_lonlat(x, y)[::-1] destins = [ convert_xy_to_lonlat(x + ax, y + ay)[::-1] for ax, ay in action_space ] origin_destins_list.append((origin, destins)) tt_list = engine.eta_one_to_many(origin_destins_list) a_size = MAX_MOVE * 2 + 1 tt_tensor = np.full((MAP_WIDTH, MAP_HEIGHT, a_size, a_size), np.inf) for (x, y), tt in zip(state_space, tt_list): tt_tensor[x, y] = np.array(tt).reshape((a_size, a_size)) for ax, ay in action_space: x_, y_ = x + ax, y + ay axi, ayi = ax + MAX_MOVE, ay + MAX_MOVE if x_ < 0 or x_ >= MAP_WIDTH or y_ < 0 or y_ >= MAP_HEIGHT or reachable_map[ x_, y_] == 0: tt_tensor[x, y, axi, ayi] = float('inf') # if reachable_map[x, y] == 1: # tt_tensor[x, y, MAX_MOVE, MAX_MOVE] = 0 tt_tensor[np.isnan(tt_tensor)] = float('inf') return tt_tensor
def create_routes(engine, reachable_map): routes = {} for x, y in state_space: print(x, y) origin = convert_xy_to_lonlat(x, y)[::-1] od_list = [(origin, convert_xy_to_lonlat(x + ax, y + ay)[::-1]) for ax, ay in action_space] tr_list, _ = zip(*engine.route(od_list, decode=False)) routes[(x, y)] = {} for a, tr in zip(action_space, tr_list): routes[(x, y)][a] = tr return routes
def convert_action_to_destination(self, vehicle_state, a): cache_key = None target = None ax, ay = a x, y = mesh.convert_lonlat_to_xy(vehicle_state.lon, vehicle_state.lat) lon, lat = mesh.convert_xy_to_lonlat(x + ax, y + ay) if lon == vehicle_state.lon and lat == vehicle_state.lat: pass elif FLAGS.use_osrm and mesh.convert_xy_to_lonlat(x, y) == (lon, lat): cache_key = ((x, y), (ax, ay)) else: target = (lat, lon) return target, cache_key
def get_route_cache(self, l, a): if l in self.route_cache: if a in self.route_cache[l]: trajectory, triptime = self.route_cache[l][a] return trajectory[:], triptime else: self.route_cache[l] = {} x, y = l ax, ay = a origin = convert_xy_to_lonlat(x, y) destin = convert_xy_to_lonlat(x + ax, y + ay) self.route_cache[l][a] = self.route([(origin, destin)])[0] trajectory, triptime = self.route_cache[l][a] return trajectory[:], triptime
def convert_action_to_destination(self, vehicle_state, a): cache_key = None target = None ax, ay = a # Action from action space matrix x, y = mesh.convert_lonlat_to_xy(vehicle_state.lon, vehicle_state.lat) lon, lat = mesh.convert_xy_to_lonlat(x + ax, y + ay) if lon == vehicle_state.lon and lat == vehicle_state.lat: pass elif FLAGS.use_osrm and mesh.convert_xy_to_lonlat(x, y) == (lon, lat): cache_key = ( (x, y), (ax, ay) ) # Create cache key with location associated with action else: target = (lat, lon) return target, cache_key
def sample_initial_locations(self, t): locations = [mesh.convert_xy_to_lonlat(x, y)[::-1] for x in range(MAP_WIDTH) for y in range(MAP_HEIGHT)] p = demand_loader.DemandLoader.load_demand_profile(t) p = p.flatten() / p.sum() vehicle_locations = [locations[i] for i in np.random.choice(len(locations), size=FLAGS.vehicles, p=p)] # print("Num: ", len(vehicle_locations)) return vehicle_locations
def get_charging_piles(): chargingpiles = csv.DictReader(open('data/processed_cs.csv')) list_ev_chargers = pd.DataFrame(chargingpiles) r_latlon = list_ev_chargers[["Longitude", "Latitude"]].apply(pd.to_numeric) # print(list_ev_chargers.iloc[[2]].ZIP) c_latlon = pd.DataFrame() for r_id, row in r_latlon.iterrows(): try: x, y = convert_lonlat_to_xy(row.Longitude, row.Latitude) c_lon, c_lat = convert_xy_to_lonlat(x, y) for _ in range(int(list_ev_chargers.iloc[[r_id]].EV_Level2)): c_latlon = c_latlon.append([[ c_lon, c_lat, status_codes.SP_LEVEL2, status_codes.CP_AVAILABLE, 0.0 ]]) for _ in range(int(list_ev_chargers.iloc[[r_id]].EV_DC_Fast)): c_latlon = c_latlon.append([[ c_lon, c_lat, status_codes.SP_DCFC, status_codes.CP_AVAILABLE, 0.0 ]]) except: ValueError c_latlon.columns = ["c_lon", "c_lat", "type", "flag", "incentive"] c_latlon.index = [i for i in range(len(c_latlon.index))] return c_latlon
def create_reachable_map(engine): lon0, lat0 = convert_xy_to_lonlat(0, 0) lon1, lat1 = convert_xy_to_lonlat(1, 1) d_max = great_circle_distance(lat0, lon0, lat1, lon1) / 2.0 points = [] for x, y in state_space: lon, lat = convert_xy_to_lonlat(x, y) points.append((lat, lon)) nearest_roads = engine.nearest_road(points) reachable_map = np.zeros((MAP_WIDTH, MAP_HEIGHT), dtype=np.float32) for (x, y), (latlon, d) in zip(state_space, nearest_roads): if d < d_max: reachable_map[x, y] = 1 return reachable_map
def propose_price(self, vehicle, price, request): if len(vehicle.q_action_dict) == 0: # print("NOT DISPATCHED BEFORE!") return price else: # print(self.q_action_dict) r_lon, r_lat = request.origin_lon, request.origin_lat r_x, r_y = mesh.convert_lonlat_to_xy(r_lon, r_lat) # print("ID: ", self.state.id) # print("Request: ", r_x, r_y) sorted_q = { k: v for k, v in sorted(vehicle.q_action_dict.items(), key=lambda item: item[1], reverse=True) } # print("Sorted: ", sorted_q) # print("Epsilon: ", self.epsilon, len(self.q_action_dict)) filtered_q = list(islice(sorted_q, vehicle.epsilon)) # filtered_q = dict(filtered_q) # print("Filtered: ", filtered_q) if (r_x, r_y) in filtered_q: # print("Here!") return price if (r_x, r_y) in vehicle.q_action_dict.keys(): # print("Exists!") # req_q = self.q_action_dict.get((r_x,r_y)) rank = 0 index = 0 for (kx, ky), v in sorted_q.items(): # print((kx,ky), (r_x, r_y)) if (kx, ky) == (r_x, r_y): rank = index index += 1 else: # print("Does not exist!") dist_list = {} for (kx, ky), v in vehicle.q_action_dict.items(): k_lon, k_lat = mesh.convert_xy_to_lonlat(kx, ky) dist = great_circle_distance(r_lat, r_lon, k_lat, k_lon) dist_list[(kx, ky)] = dist # print("D: ", dist_list) min_dist = np.min(list(dist_list.values())) (min_x, min_y) = list(dist_list.keys())[list( dist_list.values()).index(min_dist)] req_q = vehicle.q_action_dict.get((min_x, min_y)) # print(min_dist, (min_x,min_y), req_q) rank = 0 index = 0 for (kx, ky), v in sorted_q.items(): if (kx, ky) == (min_x, min_y): rank = index index += 1 # print("Rank: ", rank, len(self.q_action_dict)) rank = 1 - (rank / len(vehicle.q_action_dict)) # print("Rank_: ", rank, (self.state.driver_base_per_trip/100)) return price + (rank * 0.5 * (vehicle.state.driver_base_per_trip / 100))