|
|
@@ -1,11 +1,14 @@
|
|
|
package com.deliveryproject.easydelivery;
|
|
|
|
|
|
+import com.deliveryproject.easydelivery.Models.Coordinate;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Intersection;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Root;
|
|
|
import com.deliveryproject.easydelivery.Models.OSMRClass.Step;
|
|
|
+import com.deliveryproject.easydelivery.Utils.AntColony;
|
|
|
+import com.deliveryproject.easydelivery.Utils.SimulatedAnnealing;
|
|
|
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;
|
|
|
@@ -52,14 +55,15 @@ public class MainController {
|
|
|
location.add(intersection.location.get(0));
|
|
|
coordinates.add(location);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
System.out.println(coordinates);
|
|
|
return coordinates;
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/optimize-delivery-route")
|
|
|
+ @RequestMapping("/optimize-delivery-route")
|
|
|
@ResponseBody
|
|
|
- public List<Coordinate> optimizeDeliveryRoute(List<Coordinate> coordinates) {
|
|
|
+ public List<Coordinate> optimizeDeliveryRoute(@RequestBody List<Coordinate> coordinates) {
|
|
|
|
|
|
List<Integer> optimizedRoute = new ArrayList<>();
|
|
|
List<Coordinate> optimizedRouteCoordinates = new ArrayList<>();
|
|
|
@@ -92,6 +96,34 @@ public class MainController {
|
|
|
return optimizedRouteCoordinates;
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping("/intersections-between-multiple-coordinates/{algorithm}")
|
|
|
+ @ResponseBody
|
|
|
+ public List<Coordinate> getIntersectionsBetweenCoordinates(@PathVariable("algorithm") String algorithm, @RequestBody List<Coordinate> coordinates) throws IOException {
|
|
|
+ List<Coordinate> optimizedRouteCoordinates = null;
|
|
|
+ if (algorithm.equals("Brute-Force")) {
|
|
|
+ optimizedRouteCoordinates = optimizeDeliveryRoute(coordinates);
|
|
|
+ System.out.println("Brute-Force");
|
|
|
+ } else if (algorithm.equals("Ant")){
|
|
|
+ optimizedRouteCoordinates = optimizeDeliveryRouteACO(coordinates);
|
|
|
+ System.out.println("ANT");
|
|
|
+ } else if (algorithm.equals("Annealing")){
|
|
|
+ optimizedRouteCoordinates = optimizeDeliveryRouteAnnealing(coordinates);
|
|
|
+ System.out.println("Annealing");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nodes;
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/optimize-delivery-route-aco")
|
|
|
@ResponseBody
|
|
|
public List<Coordinate> optimizeDeliveryRouteACO(@RequestBody List<Coordinate> coordinates) throws IOException {
|
|
|
@@ -111,10 +143,10 @@ public class MainController {
|
|
|
if (i == j) {
|
|
|
distanceMatrix[i][j] = 0.0;
|
|
|
} else {
|
|
|
- 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 x1 = coordinates.get(i).getLat();
|
|
|
+ double y1 = coordinates.get(i).getLon();
|
|
|
+ double x2 = coordinates.get(j).getLat();
|
|
|
+ double y2 = coordinates.get(j).getLon();
|
|
|
|
|
|
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
|
|
|
distanceMatrix[i][j] = distance;
|
|
|
@@ -144,10 +176,10 @@ public class MainController {
|
|
|
|
|
|
// Get the nodes between the current and next coordinates
|
|
|
ArrayList<ArrayList<Double>> nodesBetweenCoordinates = getNodesBetweenTwoCoordinates(
|
|
|
- currentCoordinate.getX(),
|
|
|
- currentCoordinate.getY(),
|
|
|
- nextCoordinate.getX(),
|
|
|
- nextCoordinate.getY()
|
|
|
+ currentCoordinate.getLat(),
|
|
|
+ currentCoordinate.getLon(),
|
|
|
+ nextCoordinate.getLat(),
|
|
|
+ nextCoordinate.getLon()
|
|
|
);
|
|
|
|
|
|
// Convert the nodes to Coordinate objects and add them to the optimizedRoute
|
|
|
@@ -167,7 +199,7 @@ public class MainController {
|
|
|
private double calculateDistance(Coordinate c1, Coordinate c2) {
|
|
|
ArrayList<ArrayList<Double>> nodes;
|
|
|
try {
|
|
|
- nodes = getNodesBetweenTwoCoordinates(c1.x, c1.y, c2.x, c2.y);
|
|
|
+ nodes = getNodesBetweenTwoCoordinates(c1.lon, c1.lat, c2.lon, c2.lat);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return Double.MAX_VALUE;
|
|
|
@@ -187,6 +219,31 @@ public class MainController {
|
|
|
return sumDistance;
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("/optimize-delivery-route-annealing")
|
|
|
+ @ResponseBody
|
|
|
+ public List<Coordinate> optimizeDeliveryRouteAnnealing(List<Coordinate> coordinates) {
|
|
|
+ SimulatedAnnealing annealing = new SimulatedAnnealing();
|
|
|
+
|
|
|
+ List<Coordinate> currentSolution = new ArrayList<>(coordinates);
|
|
|
+
|
|
|
+ double initialTemperature = 500.0; // Initial temperature
|
|
|
+ double coolingRate = 0.85; // Cooling rate
|
|
|
+ int numIterations = 1000; // Number of iterations
|
|
|
+ currentSolution = annealing.optimize(currentSolution, initialTemperature, coolingRate, numIterations);
|
|
|
+
|
|
|
+ return currentSolution;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|