def choose(situation, target, distance, mode, stationFirst, nbCandidates=5): """ Choose the best station around the arrival and then define a trip between the departure and the station. """ city = situation["city"] people = situation["people"] time = tb.convert_time(situation["time"]) # Will convert the positions to (lat, lon) if they are textual addresses departure = geography.convert_to_point(city, situation["departure"]) arrival = geography.convert_to_point(city, situation["arrival"]) # Find the close stations with MongoDB stations = [station for station in query.close_points(city, arrival, number=nbCandidates)] # Get the distances to the stations candidates = geography.compute_distances_manual(arrival, stations, distance) # Sort the stations by distance candidates.sort(key=lambda station: station["duration"]) # Find an appropriate solution through the sorted candidates trip = False for candidate in candidates: # Calculate what time it would be when reaching the candidate station forecast = tb.epoch_to_datetime(time + candidate["duration"]) # Extract features from the forecasted time variables = munging.temporal_features(forecast) features = [variables["hour"], variables["minute"], variables["weekday"]] # Check if the prediction is satisfying settings = tb.read_json("common/settings.json") method = eval(settings["learning"]["method"]) # Make a prediction prediction = method.predict(features, target, city, candidate["_id"]) bias = tb.read_json("common/settings.json")["learning"]["bias"] if target == "bikes": bias *= -1 if prediction + bias >= people: stationPosition = list(reversed(candidate["p"])) if stationFirst is False: trip = tb.reshape(mode, departure, stationPosition) else: trip = tb.reshape(mode, stationPosition, departure) break return trip
def full_trip(situation): ''' We can use the previous functions. ''' # Find the route from the departure to the departure station situationOne = deepcopy(situation) situationOne['arrival'] = situationOne['departure'] firstPath = choose(situationOne, 'bikes', 'walking', 'walking', False) # Find the route from the arrival station to the arrival situationTwo = deepcopy(situation) situationTwo['departure'] = situationTwo['arrival'] secondPath = choose(situationTwo, 'spaces', 'walking', 'walking', True) # Find the route between both stations firstStation = firstPath['points'][1] secondStation = secondPath['points'][0] A = [firstStation['lat'], firstStation['lon']] B = [secondStation['lat'], secondStation['lon']] intermediatePath = tb.reshape('bicycling', A, B) # Generate the routes routes = [ turn_by_turn(firstPath), turn_by_turn(intermediatePath), turn_by_turn(secondPath) ] return routes