From f223efe9191ea8d6103c47d708b39f90d79e047f Mon Sep 17 00:00:00 2001 From: Stefan Katzensteiner Date: Thu, 18 Jul 2024 07:01:30 +0200 Subject: [PATCH] VRP complete enumeration assignment part --- .../m12s/vrp/VehicleRoutingProblem.java | 56 +++++++++++++++---- .../m12s/vrp/VehicleRoutingProblemTest.java | 20 +++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblem.java b/src/main/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblem.java index fd63781..e83cc63 100644 --- a/src/main/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblem.java +++ b/src/main/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblem.java @@ -15,16 +15,52 @@ public VehicleRoutingProblem(List> distanceMatrix) { public Solution solveCompleteEnumeration(){ - /* - Assignment Problem - 1) All combinations of 011100,100011 - - All possible x = 000000 to 111111 and complement binary 111111 - x except 000000 and 111111 - 2) The power set and its complement - Routing Problem - All permutations of 3,4,5 and 1,2,6 - */ - - return new Solution(new ArrayList<>(), new ArrayList<>()); + Solution bestSolution = new Solution(new ArrayList<>(), new ArrayList<>()); + bestSolution.setObjectiveFunctionValue(Double.MAX_VALUE); + + int length = 6; + int numberOfElements = (int) Math.pow(2,length); + for(int i=1; i route1 = generateTour(paddedBinaryString1); + + int j = numberOfElements - i - 1; + String unpaddedBinaryString2 = Integer.toBinaryString(j); + String paddedBinaryString2 = String. + format("%1$" + length + "s", unpaddedBinaryString2). + replace(' ', '0'); + + List route2 = generateTour(paddedBinaryString2); + + //TODO All permutation for route2 for each permutation of route1 + + Solution solution = new Solution(route1, route2); + solution.setObjectiveFunctionValue(calcZf(solution)); + + if(solution.getObjectiveFunctionValue() < bestSolution.getObjectiveFunctionValue()) { + bestSolution = solution; + } + } + + return bestSolution; + } + + private static List generateTour(String assignment) { + List tour = new ArrayList<>(); + + for(int i=assignment.length()-1; i>=0; i--) { + int active = Integer.parseInt(assignment.substring(i,i+1)); + if(active == 1) { + tour.add(assignment.length()-i); + } + } + + return tour; } public Solution singleSolution() { diff --git a/src/test/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblemTest.java b/src/test/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblemTest.java index 42719b9..1c6ae22 100644 --- a/src/test/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblemTest.java +++ b/src/test/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblemTest.java @@ -26,4 +26,24 @@ void givenCertainMatrix_whenCallingSingleSolution_shouldGiveKnownAnswer() { assertEquals(solution.getObjectiveFunctionValue(), 163); } + + @Test + void givenCertainMatrix_whenCallingSolveCompleteEnumeration_shouldGiveKnownAnswer() { + //https://www.youtube.com/watch?v=A1wsIFDKqBk&t=362s + List> matrix = new ArrayList<>(); + matrix.add(List.of(0, 20, 18, 14, 16, 12, 19)); + matrix.add(List.of(20, 0, 22, 18, 30, 26, 28)); + matrix.add(List.of(18, 22, 0, 32, 20, 22, 21)); + matrix.add(List.of(14, 18, 32, 0, 20, 22, 21)); + matrix.add(List.of(16, 30, 20, 20, 0, 30, 22)); + matrix.add(List.of(12, 26, 22, 22, 30, 0, 26)); + matrix.add(List.of(19, 28, 21, 21, 22, 26, 0)); + + VehicleRoutingProblem vrp = new VehicleRoutingProblem(matrix); + Solution solution = vrp.solveCompleteEnumeration(); + + assertEquals(List.of(2, 5), solution.getRoute1()); + assertEquals(List.of(1, 3, 4, 6), solution.getRoute2()); + assertEquals(solution.getObjectiveFunctionValue(), 151); + } } \ No newline at end of file