wpfat23-1 2 лет назад
Родитель
Сommit
08e2e6f816

+ 27 - 21
src/main/java/com/deliveryproject/easydelivery/AntColony.java

@@ -3,7 +3,7 @@ package com.deliveryproject.easydelivery;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
-
+import com.deliveryproject.easydelivery.Models.Coordinate;
 public class AntColony {
 
     private int numStops;
@@ -60,33 +60,33 @@ public class AntColony {
 
     private List<Integer> constructSolution(int ant) {
         boolean[] visited = new boolean[numStops];
-        int startCity = new Random().nextInt(numStops);
+        int firstStop = new Random().nextInt(numStops);
 
         List<Integer> solution = new ArrayList<>();
-        solution.add(startCity);
-        visited[startCity] = true;
+        solution.add(firstStop);
+        visited[firstStop] = true;
 
         for (int i = 1; i < numStops; i++) {
-            int nextCity = selectNextCity(ant, solution, visited);
-            solution.add(nextCity);
-            visited[nextCity] = true;
+            int nextStop = selectNextStop(ant, solution, visited);
+            solution.add(nextStop);
+            visited[nextStop] = true;
         }
 
         return solution;
     }
 
-    private int selectNextCity(int ant, List<Integer> solution, boolean[] visited) {
-        int currentCity = solution.get(solution.size() - 1);
+    private int selectNextStop(int ant, List<Integer> solution, boolean[] visited) {
+        int currentStop = solution.get(solution.size() - 1);
 
         double totalWeight = 0.0;
         double[] probabilities = new double[numStops];
 
-        for (int city = 0; city < numStops; city++) {
-            if (!visited[city]) {
-                double pheromone = Math.pow(pheromoneMatrix[currentCity][city], alpha);
-                double distance = 1.0 / Math.pow(distanceMatrix[currentCity][city], beta);
-                probabilities[city] = pheromone * distance;
-                totalWeight += probabilities[city];
+        for (int stop = 0; stop < numStops; stop++) {
+            if (!visited[stop]) {
+                double pheromone = Math.pow(pheromoneMatrix[currentStop][stop], alpha);
+                double distance = 1.0 / Math.pow(distanceMatrix[currentStop][stop], beta);
+                probabilities[stop] = pheromone * distance;
+                totalWeight += probabilities[stop];
             }
         }
 
@@ -103,10 +103,10 @@ public class AntColony {
             }
         }
 
-        // Choose an unvisited city randomly
-        for (int city = 0; city < numStops; city++) {
-            if (!visited[city]) {
-                return city;
+        // Choose an unvisited stop randomly
+        for (int stop = 0; stop < numStops; stop++) {
+            if (!visited[stop]) {
+                return stop;
             }
         }
         return -1;
@@ -131,8 +131,14 @@ public class AntColony {
         }
     }
 
-    public List<Integer> getBestSolution() {
-        return bestSolution;
+    public List<Coordinate> getBestSolution(List<Coordinate> coordinates) {
+        List<Coordinate> bestRoute = new ArrayList<>();
+
+        for (int stopIndex : bestSolution) {
+            bestRoute.add(coordinates.get(stopIndex));
+        }
+
+        return bestRoute;
     }
 
     private double calculateSolutionDistance(List<Integer> solution) {

+ 36 - 18
src/main/java/com/deliveryproject/easydelivery/MainController.java

@@ -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;
-        }
-
-    }
 }
 
 

+ 20 - 4
src/main/java/com/deliveryproject/easydelivery/Models/Coordinate.java

@@ -1,10 +1,26 @@
 package com.deliveryproject.easydelivery.Models;
 
 public class Coordinate {
-    public double lat;
-    public double lon;
+    public double x;
+    public double y;
     public Coordinate(double lat, double lon) {
-        this.lat = lat;
-        this.lon = lon;
+        this.x = lat;
+        this.y = lon;
+    }
+
+    public double getX() {
+        return x;
+    }
+
+    public void setX(double x) {
+        this.x = x;
+    }
+
+    public double getY() {
+        return y;
+    }
+
+    public void setY(double y) {
+        this.y = y;
     }
 }