Skip to content

Commit

Permalink
Testing for better get nearest points algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
BaileyS03 committed May 30, 2024
1 parent d5fe181 commit 337cf67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions application/app/views/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,22 @@ def post(self):

start_point = to_shape(trip.start_location)
passengers = trip.trip_requests
# Sort passengers by the Haversine distance from the start point
sorted_passengers = sorted(
passengers,
key=lambda p: haversine(
start_point.x, start_point.y,
to_shape(p.pickup_location).x, to_shape(p.pickup_location).y
# Sorting passengers based on the nearest neighbor heuristic
sorted_passengers = []
current_point = start_point
remaining_passengers = passengers.copy()

while remaining_passengers:
next_passenger = min(
remaining_passengers,
key=lambda p: haversine(
current_point.x, current_point.y,
to_shape(p.pickup_location).x, to_shape(p.pickup_location).y
)
)
)
sorted_passengers.append(next_passenger)
current_point = to_shape(next_passenger.pickup_location)
remaining_passengers.remove(next_passenger)

# Construct the response
response = {
Expand Down
4 changes: 2 additions & 2 deletions endpoint.http
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ POST {{baseUrl}}/trips/get/all
Content-Type: application/json

{
"username": "test1"
"username": "jDoe12"
}

### Get all PENDING trips for a user
Expand Down Expand Up @@ -280,7 +280,7 @@ POST {{baseUrl}}/trip/get_route_positions
Content-Type: application/json

{
"trip_id": "40a7b51b-ec8b-4c09-86ac-2d0a2157bae3",
"trip_id": "b80739e6-0e82-4106-bb87-f40bf4667fbc",
"username": "jDoe12"
}

Expand Down

0 comments on commit 337cf67

Please sign in to comment.