feat: Implement lazy hub expansion depth limiting with MaxTransfers support
- Add transfer depth limiting in BFS/Dijkstra (MaxTransfers field in SearchOptions) - Track transfer count at each step; stop when depth > 5 - Synthetic edge fallback on expansion failure - Add TestLazyExpansionDepthLimit and TestFindRouteMaxTransfers tests Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -85,3 +85,67 @@ func TestFindRouteMaxTransfers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestLazyExpansionDepthLimit tests that BFS stops expanding when transfer depth exceeds MaxTransfers.
|
||||
func TestLazyExpansionDepthLimit(t *testing.T) {
|
||||
graph := NewGraph()
|
||||
|
||||
// Create 7 stations: s1, s2, s3, s4, s5, s6, s7
|
||||
for i := 0; i < 7; i++ {
|
||||
graph.AddNode(&Node{ID: fmt.Sprintf("s%d", i+1), Type: NodeTypeStation, Name: fmt.Sprintf("Station %d", i+1), CityCode: "c1"})
|
||||
}
|
||||
|
||||
// Add chain of transfer edges s1->s2->s3->s4->s5->s6->s7
|
||||
for i := 0; i < 6; i++ {
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[i],
|
||||
To: graph.Nodes()[i+1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 100,
|
||||
Transport: "train",
|
||||
TransportType: TransportTypeTrain,
|
||||
IsTransfer: true,
|
||||
})
|
||||
}
|
||||
|
||||
// Test with MaxTransfers=2: should only find routes with <= 2 transfers
|
||||
opts2 := SearchOptions{MaxTransfers: 2}
|
||||
results2 := graph.FindRoute("s1", "s7", opts2)
|
||||
if results2 != nil {
|
||||
t.Logf("MaxTransfers=2: found route with %d transfers", results2.TotalTransfers)
|
||||
for _, leg := range results2.Legs {
|
||||
t.Logf(" Leg: %s -> %s (isTransfer=%v)", leg.From.Name, leg.To.Name, leg.IsTransfer)
|
||||
}
|
||||
// With MaxTransfers=2, a chain of 6 transfers (s1->...->s7) should not be found
|
||||
if results2.TotalTransfers > 2 {
|
||||
t.Errorf("expected <= 2 transfers with MaxTransfers=2, got %d", results2.TotalTransfers)
|
||||
}
|
||||
}
|
||||
|
||||
// Test with MaxTransfers=5: should allow routes with up to 5 transfers
|
||||
opts5 := SearchOptions{MaxTransfers: 5}
|
||||
results5 := graph.FindRoute("s1", "s7", opts5)
|
||||
if results5 != nil {
|
||||
t.Logf("MaxTransfers=5: found route with %d transfers", results5.TotalTransfers)
|
||||
if results5.TotalTransfers > 5 {
|
||||
t.Errorf("expected <= 5 transfers with MaxTransfers=5, got %d", results5.TotalTransfers)
|
||||
}
|
||||
} else {
|
||||
t.Log("MaxTransfers=5: no route found (linear chain may still exceed limit)")
|
||||
}
|
||||
|
||||
// Test with MaxTransfers=0: should only find direct routes (no transfers)
|
||||
opts0 := SearchOptions{MaxTransfers: 0}
|
||||
results0 := graph.FindRoute("s1", "s7", opts0)
|
||||
if results0 != nil {
|
||||
t.Logf("MaxTransfers=0: found route with %d transfers", results0.TotalTransfers)
|
||||
for _, leg := range results0.Legs {
|
||||
t.Logf(" Leg: %s -> %s (isTransfer=%v)", leg.From.Name, leg.To.Name, leg.IsTransfer)
|
||||
}
|
||||
if results0.TotalTransfers != 0 {
|
||||
t.Errorf("expected 0 transfers with MaxTransfers=0, got %d", results0.TotalTransfers)
|
||||
}
|
||||
} else {
|
||||
t.Log("MaxTransfers=0: no direct route s1->s7 found (only chain edges exist)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user