Skip to content

Commit

Permalink
ddd
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed Sep 24, 2024
1 parent 0218660 commit 7193355
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/main/kotlin/com/ggsdh/backend/trip/application/AILaneService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,45 @@ class AILaneService(
fun buildRoute(laneMappings: List<LaneMapping>): List<LaneMapping> {
if (laneMappings.isEmpty()) return emptyList()

val remaining = laneMappings.toMutableList()
val route = mutableListOf<LaneMapping>()
val routeAll = mutableListOf<LaneMapping>()
laneMappings.groupBy { it.day }.forEach { (_, dayLaneMappings) ->
val remaining = dayLaneMappings.toMutableList()
val route = mutableListOf<LaneMapping>()
// Start from the first laneMapping
var current = remaining.removeAt(0)
route.add(current)

while (remaining.isNotEmpty()) {
val currentPosition = Position(
current.tourArea?.latitude ?: 0.0,
current.tourArea?.longitude ?: 0.0
)

// Start from the first laneMapping
var current = remaining.removeAt(0)
route.add(current)
// Find the closest unvisited laneMapping
val next = remaining.minByOrNull { lm ->
val position = Position(
lm.tourArea?.latitude ?: 0.0,
lm.tourArea?.longitude ?: 0.0
)
distance(currentPosition, position)
} ?: break

while (remaining.isNotEmpty()) {
val currentPosition = Position(
current.tourArea?.latitude ?: 0.0,
current.tourArea?.longitude ?: 0.0
)
remaining.remove(next)
route.add(next)
current = next
}

// Find the closest unvisited laneMapping
val next = remaining.minByOrNull { lm ->
val position = Position(
lm.tourArea?.latitude ?: 0.0,
lm.tourArea?.longitude ?: 0.0
)
distance(currentPosition, position)
} ?: break
val newRoute = route.mapIndexed { index, laneMapping ->
laneMapping.sequence = (index+1).toLong()

laneMapping
}

remaining.remove(next)
route.add(next)
current = next
}

// Update the sequence numbers based on the new order
route.forEachIndexed { index, laneMapping ->
laneMapping.sequence = (index + 1).toLong()
routeAll.addAll(newRoute)
}

return route
return routeAll
}

fun generateAILane(
Expand Down

0 comments on commit 7193355

Please sign in to comment.