|
@@ -1,6 +1,5 @@
|
|
|
package com.deliveryproject.easydelivery;
|
|
package com.deliveryproject.easydelivery;
|
|
|
|
|
|
|
|
-import com.deliveryproject.easydelivery.Models.Coordinate;
|
|
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Intersection;
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Intersection;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Root;
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Root;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Step;
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Step;
|
|
@@ -53,15 +52,13 @@ public class MainController {
|
|
|
location.add(intersection.location.get(0));
|
|
location.add(intersection.location.get(0));
|
|
|
coordinates.add(location);
|
|
coordinates.add(location);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
System.out.println(coordinates);
|
|
System.out.println(coordinates);
|
|
|
return coordinates;
|
|
return coordinates;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- @RequestMapping("/optimize-delivery-route")
|
|
|
|
|
|
|
+ @GetMapping("/optimize-delivery-route")
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
- public List<Coordinate> optimizeDeliveryRoute(@RequestBody List<Coordinate> coordinates) {
|
|
|
|
|
|
|
+ public List<Coordinate> optimizeDeliveryRoute(List<Coordinate> coordinates) {
|
|
|
|
|
|
|
|
List<Integer> optimizedRoute = new ArrayList<>();
|
|
List<Integer> optimizedRoute = new ArrayList<>();
|
|
|
List<Coordinate> optimizedRouteCoordinates = new ArrayList<>();
|
|
List<Coordinate> optimizedRouteCoordinates = new ArrayList<>();
|
|
@@ -93,29 +90,56 @@ public class MainController {
|
|
|
}
|
|
}
|
|
|
return optimizedRouteCoordinates;
|
|
return optimizedRouteCoordinates;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- @RequestMapping("/intersections-between-multiple-coordinates")
|
|
|
|
|
|
|
+ @GetMapping("/optimize-delivery-route-aco")
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
- public List<Coordinate> getIntersectionsBetweenCoordinates(@RequestBody List<Coordinate> coordinates) throws IOException {
|
|
|
|
|
- List<Coordinate> optimizedRouteCoordinates = optimizeDeliveryRoute(coordinates);
|
|
|
|
|
-
|
|
|
|
|
- ArrayList<Coordinate> nodes = new ArrayList<>();
|
|
|
|
|
- for (int i = 0; i < optimizedRouteCoordinates.size(); i++) {
|
|
|
|
|
- ArrayList<ArrayList<Double>> nodesBetweenTwoCoordinates;
|
|
|
|
|
- if (i == optimizedRouteCoordinates.size() - 1) nodesBetweenTwoCoordinates = getNodesBetweenTwoCoordinates(optimizedRouteCoordinates.get(i).lon, optimizedRouteCoordinates.get(i).lat,optimizedRouteCoordinates.get(0).lon, optimizedRouteCoordinates.get(0).lat);
|
|
|
|
|
- else nodesBetweenTwoCoordinates = getNodesBetweenTwoCoordinates(optimizedRouteCoordinates.get(i).lon, optimizedRouteCoordinates.get(i).lat,optimizedRouteCoordinates.get(i+1).lon, optimizedRouteCoordinates.get(i+1).lat);
|
|
|
|
|
- System.out.println("Drop off");
|
|
|
|
|
- for (ArrayList <Double> node: nodesBetweenTwoCoordinates) {
|
|
|
|
|
- nodes.add(new Coordinate(node.get(0), node.get(1)));
|
|
|
|
|
|
|
+ public List<Integer> optimizeDeliveryRouteACO(@RequestBody List<Coordinate> coordinates) {
|
|
|
|
|
+ int numAnts = 10;
|
|
|
|
|
+ int numIterations = 100;
|
|
|
|
|
+ double alpha = 1.0;
|
|
|
|
|
+ double beta = 2.0;
|
|
|
|
|
+ double evaporationRate = 0.5;
|
|
|
|
|
+ double initialPheromone = 0.1;
|
|
|
|
|
+ int numStops = coordinates.size();
|
|
|
|
|
+ AntColony antColony = new AntColony(coordinates.size(), numAnts, alpha, beta, evaporationRate, initialPheromone);
|
|
|
|
|
+
|
|
|
|
|
+ // Populate the distance matrix
|
|
|
|
|
+ double[][] distanceMatrix = new double[numStops][numStops];
|
|
|
|
|
+ for (int i = 0; i < numStops; i++) {
|
|
|
|
|
+ for (int j = 0; j < numStops; j++) {
|
|
|
|
|
+ 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 distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
|
|
|
|
|
+ distanceMatrix[i][j] = distance;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return nodes;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Set the distance matrix in the AntColony instance
|
|
|
|
|
+ antColony.setDistanceMatrix(distanceMatrix);
|
|
|
|
|
+
|
|
|
|
|
+ antColony.initializePheromoneMatrix();
|
|
|
|
|
+
|
|
|
|
|
+ // Ant colony optimization for the specified number of iterations
|
|
|
|
|
+ for (int iteration = 0; iteration < numIterations; iteration++) {
|
|
|
|
|
+ antColony.constructSolutions();
|
|
|
|
|
+ antColony.updatePheromoneMatrix();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Integer> bestSolution = antColony.getBestSolution();
|
|
|
|
|
+
|
|
|
|
|
+ return bestSolution;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private double calculateDistance(Coordinate c1, Coordinate c2) {
|
|
private double calculateDistance(Coordinate c1, Coordinate c2) {
|
|
|
ArrayList<ArrayList<Double>> nodes;
|
|
ArrayList<ArrayList<Double>> nodes;
|
|
|
try {
|
|
try {
|
|
|
- nodes = getNodesBetweenTwoCoordinates(c1.lon, c1.lat, c2.lon, c2.lat);
|
|
|
|
|
|
|
+ nodes = getNodesBetweenTwoCoordinates(c1.x, c1.y, c2.x, c2.y);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
return Double.MAX_VALUE;
|
|
return Double.MAX_VALUE;
|
|
@@ -135,6 +159,16 @@ public class MainController {
|
|
|
return sumDistance;
|
|
return sumDistance;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ class Coordinate {
|
|
|
|
|
+ double x;
|
|
|
|
|
+ double y;
|
|
|
|
|
+ public Coordinate(double x, double y) {
|
|
|
|
|
+ this.x = x;
|
|
|
|
|
+ this.y = y;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|