public List<Arc> chooseArcs(Solution solution, Vertex current) { List<Arc> nextArcs; List<Arc> currentOutgoing = current.getOutgoingArcs(); List<Arc> currentOutgoingStillInAreaNotVisited = new LinkedList<Arc>(); for (Arc arc : currentOutgoing) { Vertex endVertex = arc.getEnd(); if (GeographicDistances.distance( endVertex, new Vertex(solution.getLatStart(), solution.getLngStart(), -1)) < maxDistanceFromStart) { if (!arc.isVisited()) { currentOutgoingStillInAreaNotVisited.add(arc); } } } // The set of outgoing arcs contains arcs that are not visited // Choose the best arc among the set if (!currentOutgoingStillInAreaNotVisited.isEmpty()) { Collections.sort(currentOutgoingStillInAreaNotVisited, comparator); nextArcs = Collections.singletonList(currentOutgoingStillInAreaNotVisited.get(0)); } // Else all the arcs are visited // Go to the nearest unvisited arc in the area // TODO - is there a better choice? else { nextArcs = pathToNearestUnvisitedArcInArea(current, solution); } int newTotal = solution.getTotalTime(); for (Arc arc : nextArcs) newTotal += arc.getDuration(); if (newTotal > maxTime || nextArcs.isEmpty()) return null; else return nextArcs; }