Skip to content

Commit

Permalink
Merge pull request #62 from PLD-Agile/53-BE-Add-time-window-constraints
Browse files Browse the repository at this point in the history
Add time constraints to the algo and the tests
  • Loading branch information
cfstcyr authored Nov 13, 2023
2 parents 72b268b + 38786c5 commit fc3accb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
67 changes: 60 additions & 7 deletions src/services/tour/tests/test_tour_computing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_compute_shortest_path_graph(tour_service):
DeliveryLocation(
Segment(-1, "", Intersection(0, 0, i), Intersection(0, 0, i), 0), 0
),
0,
8,
)
for i in [1, 2, 4]
]
Expand All @@ -62,9 +62,9 @@ def test_compute_shortest_path_graph(tour_service):
def test_solve_tsp_should_return_solution(tour_service):
# Create a sample complete directed graph
G = nx.DiGraph()
G.add_node(0)
G.add_node(1)
G.add_node(2)
G.add_node(0, timewindow=8)
G.add_node(1, timewindow=8)
G.add_node(2, timewindow=8)

G.add_edge(0, 1, length=1.0, path=[0, 23, 56, 1])
G.add_edge(1, 0, length=2.0, path=[1, 12, 16, 0])
Expand All @@ -82,9 +82,9 @@ def test_solve_tsp_should_return_solution(tour_service):
def test_solve_tsp_should_return_empty_solution_if_cul_de_sac(tour_service):
# Create a sample complete directed graph
G = nx.DiGraph()
G.add_node(0)
G.add_node(1)
G.add_node(2)
G.add_node(0, timewindow=8)
G.add_node(1, timewindow=8)
G.add_node(2, timewindow=8)

G.add_edge(0, 1, length=1.0, path=[0, 23, 56, 1])
G.add_edge(1, 0, length=2.0, path=[1, 12, 16, 0])
Expand All @@ -96,3 +96,56 @@ def test_solve_tsp_should_return_empty_solution_if_cul_de_sac(tour_service):
# Check if the above graph is a valid NetworkX DiGraph
assert isinstance(G, nx.DiGraph)
assert path == []


# Test for time window constraints
def test_solve_tsp_should_fail_if_delivery_not_in_time_window(tour_service):
# Create a sample complete directed graph
G = nx.DiGraph()
G.add_node(0, timewindow=8)
G.add_node(1, timewindow=8)
G.add_node(2, timewindow=10)
G.add_node(3, timewindow=11)

G.add_edge(0, 1, length=1.0, path=[0, 23, 56, 1])
G.add_edge(1, 0, length=2.0, path=[1, 12, 16, 0])
G.add_edge(0, 2, length=3.0, path=[0, 5, 33, 2])
G.add_edge(2, 0, length=4.0, path=[2, 42, 27, 0])
G.add_edge(1, 2, length=500000000000000000.0, path=[1, 7, 6, 2])
G.add_edge(1, 3, length=5.0, path=[1, 9, 54, 2, 3])
G.add_edge(3, 1, length=6.0, path=[3, 54, 9, 1])
G.add_edge(3, 2, length=7.0, path=[3, 4, 19, 2])
G.add_edge(2, 3, length=8.0, path=[2, 4, 7, 3])
G.add_edge(3, 0, length=555.0, path=[3, 2, 99, 33, 0])

path = tour_service.solve_tsp(G)

# Check if the above graph is a valid NetworkX DiGraph
assert isinstance(G, nx.DiGraph)
assert path == []


def test_solve_tsp_should_pass_if_delivery_in_time_window(tour_service):
# Create a sample complete directed graph
G = nx.DiGraph()
G.add_node(0, timewindow=8)
G.add_node(1, timewindow=8)
G.add_node(2, timewindow=10)
G.add_node(3, timewindow=11)

G.add_edge(0, 1, length=1.0, path=[0, 23, 56, 1])
G.add_edge(1, 0, length=2.0, path=[1, 12, 16, 0])
G.add_edge(0, 2, length=3.0, path=[0, 5, 33, 2])
G.add_edge(2, 0, length=4.0, path=[2, 42, 27, 0])
G.add_edge(1, 2, length=50.0, path=[1, 7, 6, 2])
G.add_edge(1, 3, length=5.0, path=[1, 9, 54, 2, 3])
G.add_edge(3, 1, length=6.0, path=[3, 54, 9, 1])
G.add_edge(3, 2, length=7.0, path=[3, 4, 19, 2])
G.add_edge(2, 3, length=8.0, path=[2, 4, 7, 3])
G.add_edge(3, 0, length=555.0, path=[3, 2, 99, 33, 0])

path = tour_service.solve_tsp(G)

# Check if the above graph is a valid NetworkX DiGraph
assert isinstance(G, nx.DiGraph)
assert path == [0, 23, 56, 1, 7, 6, 2, 4, 7, 3, 2, 99, 33, 0]
40 changes: 33 additions & 7 deletions src/services/tour/tour_computing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def compute_tours(
"""Compute tours for a list of tour requests."""
map_graph = self.create_graph_from_map(map)
warehouse = DeliveryRequest(
DeliveryLocation(Segment(-1, "", map.warehouse, map.warehouse, 0), 0), 0
DeliveryLocation(Segment(-1, "", map.warehouse, map.warehouse, 0), 0), 8
)

return [
Expand Down Expand Up @@ -62,15 +62,22 @@ def compute_shortest_path_graph(
) -> nx.DiGraph:
"""Compute the shortest path graph between delivery locations."""
G = nx.DiGraph()

# Add delivery locations as nodes
for delivery in deliveries:
G.add_node(delivery.location.segment.origin.id)
G.add_node(
delivery.location.segment.origin.id, timewindow=delivery.timeWindow
)

# Compute the shortest path distances and paths between delivery locations
for source in deliveries:
for target in deliveries:
if source != target:
# add time windows constraints
if (
target.timeWindow + 1 <= source.timeWindow
and target != deliveries[0]
):
continue
try:
shortest_path_length, shortest_path = nx.single_source_dijkstra(
graph,
Expand All @@ -89,8 +96,6 @@ def compute_shortest_path_graph(

return G

# TODO : solve the tsp algorithm from the shortest path graph, return the route and the length

def solve_tsp(self, shortest_path_graph: nx.Graph) -> List[int]:
shortest_cycle_length = float("inf")
shortest_cycle = []
Expand All @@ -104,13 +109,34 @@ def solve_tsp(self, shortest_path_graph: nx.Graph) -> List[int]:
permuted_points = list(permuted_points)
permuted_points = [warehouse_id] + permuted_points
cycle_length = 0
current_time = 8 * 60 # Starting time at the warehouse (8 a.m.)

for i in range(len(permuted_points) - 1):
source = permuted_points[i]
target = permuted_points[i + 1]
if not shortest_path_graph.has_edge(source, target):
is_valid_tuple = False
break
cycle_length += shortest_path_graph[source][target]["length"]
travel_distance = shortest_path_graph[source][target]["length"]
cycle_length += travel_distance

# Check if the delivery time is within the time window
time_window = shortest_path_graph.nodes[target]["timewindow"] * 60
travel_time = (
travel_distance / 15000
) * 60 # Convert meters to minutes based on speed (15 km/h)
arrival_time = current_time + travel_time

if arrival_time < time_window:
# Courier arrives before the time window, wait until it starts
current_time = time_window + 5 # Add 5 minutes for delivery
elif arrival_time <= time_window + 60:
# Courier arrives within the time window
current_time = arrival_time + 5 # Add 5 minutes for delivery
else:
# Courier arrives after the time window, this tuple is invalid
is_valid_tuple = False
break

if not is_valid_tuple:
continue
Expand All @@ -127,7 +153,7 @@ def solve_tsp(self, shortest_path_graph: nx.Graph) -> List[int]:
shortest_cycle_length = cycle_length
shortest_cycle = permuted_points

# Compute the actual route from the shortest cycle
# Compute the actual route from the shortest cycle
if shortest_cycle == []:
return []
for i in range(len(shortest_cycle) - 1):
Expand Down
6 changes: 3 additions & 3 deletions src/views/main_page/map/map_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,15 @@ def __calculate_arrow(self, line: QLineF, size: float = 0.0002) -> QPolygonF:
direction /= math.sqrt(direction.x() ** 2 + direction.y() ** 2)

# Define origin as the middle of the segment
origin = (line.p1() + line.p2()) / 2 - (direction * size / 2)
origin = (line.p1() + line.p2()) / 2 + (direction * size / 2)

tangent = QPointF(direction.y(), -direction.x())

return QPolygonF(
[
origin + (direction * size) + (tangent * -size / 2),
origin - (direction * size) - (tangent * -size / 2),
origin,
origin + (direction * size) + (tangent * size / 2),
origin - (direction * size) - (tangent * size / 2),
]
)

Expand Down

0 comments on commit fc3accb

Please sign in to comment.