def route(self, od_pairs): results = [] for (origin_lat, origin_lon), (dest_lat, dest_lon) in od_pairs: x, y = mesh.convert_lonlat_to_xy(origin_lon, origin_lat) x_, y_ = mesh.convert_lonlat_to_xy(dest_lon, dest_lat) ax, ay = x_ - x, y_ - y axi = x_ - x + MAX_MOVE ayi = y_ - y + MAX_MOVE trajectory = polyline.decode(self.routes[(x, y)][(ax, ay)]) triptime = self.tt_map[x, y, axi, ayi] results.append((trajectory, triptime)) return results
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: s, actions = self.feature_constructor.construct_current_features( x, y) Q = self.q_network.compute_q_values(s) # 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] amax = np.argmax(Q) self.q_cache[(x, y)] = actions, Q, amax # if actions[amax] == (0, 0): # aidx = amax # else: aidx = self.q_network.get_action(Q, amax) a = actions[aidx] offduty = 1 if Q[aidx] < FLAGS.offduty_threshold else 0 return a, offduty
def route_hex(self, od_pairs): results = [] for (origin_lat, origin_lon), (dest_lat, dest_lon) in od_pairs: x, y = mesh.convert_lonlat_to_xy(origin_lon, origin_lat) x_, y_ = mesh.convert_lonlat_to_xy(dest_lon, dest_lat) ax, ay = max(0, min(x_, 52)) - max(0, min(x, 52)), max( 0, min(y_, 68)) - max(0, min(y, 68)) axi = x_ - x + MAX_MOVE ayi = y_ - y + MAX_MOVE trajectory = polyline.decode( self.routes[(x, y)][(ax, ay)]) # Route from origin to destination triptime = self.tt_map[x, y, axi, ayi] results.append((trajectory, triptime)) return results
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 route(self, od_pairs): results = [] for (origin_lat, origin_lon), (dest_lat, dest_lon) in od_pairs: x, y = mesh.convert_lonlat_to_xy(origin_lon, origin_lat) x_, y_ = mesh.convert_lonlat_to_xy(dest_lon, dest_lat) ax, ay = x_ - x, y_ - y axi = x_ - x + MAX_MOVE ayi = y_ - y + MAX_MOVE # print(len(self.routes)) # if ax or ay > MAX_MOVE: # trajectory = None # else: # print((x, y), (ax, ay), (x_, y_)) trajectory = polyline.decode( self.routes[(x, y)][(ax, ay)]) # Route from origin to destination triptime = self.tt_map[x, y, axi, ayi] # print(triptime) results.append((trajectory, triptime)) return results
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 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
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 memorize_experience(self, vehicle_id, vehicle_state, a): t = self.feature_constructor.get_current_time() l = mesh.convert_lonlat_to_xy(vehicle_state.lon, vehicle_state.lat) last_state_action = self.last_state_actions.get(vehicle_id, None) if last_state_action is not None: current_state = (t, l) reward = self.rewards[vehicle_id] if len(self.experience_memory) > settings.MAX_MEMORY_SIZE: self.experience_memory.pop(0) self.experience_memory.append( (last_state_action, current_state, reward)) self.rewards[vehicle_id] = 0 self.last_state_actions[vehicle_id] = (t, l, a)
def route_time(self, od_pairs): results = [] new_od_pairs = [] for (origin_lat, origin_lon), (dest_lat, dest_lon) in od_pairs: x, y = mesh.convert_lonlat_to_xy(origin_lon, origin_lat) x_, y_ = mesh.convert_lonlat_to_xy(dest_lon, dest_lat) ax, ay = x_ - x, y_ - y # print((x, y), (ax, ay), (x_, y_)) # print(MAX_MOVE) if ax > MAX_MOVE and ay > MAX_MOVE: ax = MAX_MOVE ay = MAX_MOVE # print((x + ax, y + ay), (x_, y_)) new_od_pairs.append(((x + ax, y + ay), (x_, y_))) elif ax < (-1 * MAX_MOVE) and ay < (-1 * MAX_MOVE): ax = (-1 * MAX_MOVE) ay = (-1 * MAX_MOVE) # print((x - ax, y - ay), (x_, y_)) new_od_pairs.append(((x + ax, y + ay), (x_, y_))) elif ax > MAX_MOVE and ay < (-1 * MAX_MOVE): ax = MAX_MOVE ay = (-1 * MAX_MOVE) # print((x + ax, y - ay), (x_, y_)) new_od_pairs.append(((x + ax, y + ay), (x_, y_))) elif ay > MAX_MOVE and ax < (-1 * MAX_MOVE): ay = MAX_MOVE ax = (-1 * MAX_MOVE) # print((x - ax, y + ay), (x_, y_)) new_od_pairs.append(((x + ax, y + ay), (x_, y_))) elif ax > MAX_MOVE >= ay: ax = MAX_MOVE # print(len(od_pairs)) new_od_pairs.append(((x + ax, y_), (x_, y_))) # print(len(od_pairs), od_pairs) elif ay > MAX_MOVE >= ax: ay = MAX_MOVE # print(len(od_pairs)) new_od_pairs.append(((x_, y + ay), (x_, y_))) # print(len(od_pairs), od_pairs) elif ax < (-1 * MAX_MOVE) <= ay: ax = (-1 * MAX_MOVE) # print(len(od_pairs)) new_od_pairs.append(((x + ax, y_), (x_, y_))) # print(len(od_pairs), od_pairs) elif ay < (-1 * MAX_MOVE) <= ax: ay = (-1 * MAX_MOVE) # print(len(od_pairs)) new_od_pairs.append(((x_, y + ay), (x_, y_))) # print(len(od_pairs), od_pairs) axi = ax + MAX_MOVE ayi = ay + MAX_MOVE # print((x, y), (ax, ay), (x_, y_)) trajectory = polyline.decode( self.routes[(x, y)][(ax, ay)]) # Route from origin to destination triptime = self.tt_map[x, y, axi, ayi] # print(triptime) extra_res = self.extra_routes(new_od_pairs) for (extra_traject, extra_time) in extra_res: trajectory.extend(extra_traject) triptime += extra_time results.append((trajectory, triptime)) # results.extend(extra_res) return results
def get_coord(self, lon, lat): x, y = mesh.convert_lonlat_to_xy(lon, lat) return (int(x / self.k), int(y / self.k))
def construct_supply_map(self, locations): supply_map = self.construct_initial_map() for lon, lat in locations: x, y = mesh.convert_lonlat_to_xy(lon, lat) supply_map[x, y] += 1.0 return supply_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))