|
|
@@ -5,7 +5,7 @@ import com.deliveryproject.easydelivery.Models.OSMRClass.Root;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Step;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
-
|
|
|
+import com.deliveryproject.easydelivery.Models.Coordinate;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
@@ -56,6 +56,7 @@ public class MainController {
|
|
|
System.out.println(coordinates);
|
|
|
return coordinates;
|
|
|
}
|
|
|
+
|
|
|
@GetMapping("/optimize-delivery-route")
|
|
|
@ResponseBody
|
|
|
public List<Coordinate> optimizeDeliveryRoute(List<Coordinate> coordinates) {
|
|
|
@@ -90,9 +91,10 @@ public class MainController {
|
|
|
}
|
|
|
return optimizedRouteCoordinates;
|
|
|
}
|
|
|
+
|
|
|
@GetMapping("/optimize-delivery-route-aco")
|
|
|
@ResponseBody
|
|
|
- public List<Integer> optimizeDeliveryRouteACO(@RequestBody List<Coordinate> coordinates) {
|
|
|
+ public List<Coordinate> optimizeDeliveryRouteACO(@RequestBody List<Coordinate> coordinates) throws IOException {
|
|
|
int numAnts = 10;
|
|
|
int numIterations = 100;
|
|
|
double alpha = 1.0;
|
|
|
@@ -109,10 +111,10 @@ public class MainController {
|
|
|
if (i == j) {
|
|
|
distanceMatrix[i][j] = 0.0;
|
|
|
} else {
|
|
|
- double x1 = coordinates.get(i).x;
|
|
|
- double y1 = coordinates.get(i).y;
|
|
|
- double x2 = coordinates.get(j).x;
|
|
|
- double y2 = coordinates.get(j).y;
|
|
|
+ double x1 = coordinates.get(i).getX();
|
|
|
+ double y1 = coordinates.get(i).getY();
|
|
|
+ double x2 = coordinates.get(j).getX();
|
|
|
+ double y2 = coordinates.get(j).getY();
|
|
|
|
|
|
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
|
|
|
distanceMatrix[i][j] = distance;
|
|
|
@@ -131,9 +133,35 @@ public class MainController {
|
|
|
antColony.updatePheromoneMatrix();
|
|
|
}
|
|
|
|
|
|
- List<Integer> bestSolution = antColony.getBestSolution();
|
|
|
+ // Get the best solution from AntColony and convert it to a list of coordinates
|
|
|
+ List<Coordinate> bestRoute = antColony.getBestSolution(coordinates);
|
|
|
+
|
|
|
+ // Iterate over the bestRoute list
|
|
|
+ List<Coordinate> optimizedRoute = new ArrayList<>();
|
|
|
+ for (int i = 0; i < bestRoute.size() - 1; i++) {
|
|
|
+ Coordinate currentCoordinate = bestRoute.get(i);
|
|
|
+ Coordinate nextCoordinate = bestRoute.get(i + 1);
|
|
|
+
|
|
|
+ // Get the nodes between the current and next coordinates
|
|
|
+ ArrayList<ArrayList<Double>> nodesBetweenCoordinates = getNodesBetweenTwoCoordinates(
|
|
|
+ currentCoordinate.getX(),
|
|
|
+ currentCoordinate.getY(),
|
|
|
+ nextCoordinate.getX(),
|
|
|
+ nextCoordinate.getY()
|
|
|
+ );
|
|
|
+
|
|
|
+ // Convert the nodes to Coordinate objects and add them to the optimizedRoute
|
|
|
+ for (ArrayList<Double> node : nodesBetweenCoordinates) {
|
|
|
+ double lon = node.get(1);
|
|
|
+ double lat = node.get(0);
|
|
|
+ optimizedRoute.add(new Coordinate(lon, lat));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add the last coordinate to the optimizedRoute
|
|
|
+ optimizedRoute.add(bestRoute.get(bestRoute.size() - 1));
|
|
|
|
|
|
- return bestSolution;
|
|
|
+ return optimizedRoute;
|
|
|
}
|
|
|
|
|
|
private double calculateDistance(Coordinate c1, Coordinate c2) {
|
|
|
@@ -159,16 +187,6 @@ public class MainController {
|
|
|
return sumDistance;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- class Coordinate {
|
|
|
- double x;
|
|
|
- double y;
|
|
|
- public Coordinate(double x, double y) {
|
|
|
- this.x = x;
|
|
|
- this.y = y;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
|