Skip to content

Commit

Permalink
VRP complete enumeration assignment part
Browse files Browse the repository at this point in the history
  • Loading branch information
gocklkatz committed Jul 18, 2024
1 parent 610e043 commit f223efe
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
56 changes: 46 additions & 10 deletions src/main/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,52 @@ public VehicleRoutingProblem(List<List<Integer>> 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<numberOfElements-1; i++) {
//TODO The "convert to binary" parts are awfully similar
String unpaddedBinaryString1 = Integer.toBinaryString(i);
String paddedBinaryString1 = String.
format("%1$" + length + "s", unpaddedBinaryString1).
replace(' ', '0');

List<Integer> route1 = generateTour(paddedBinaryString1);

int j = numberOfElements - i - 1;
String unpaddedBinaryString2 = Integer.toBinaryString(j);
String paddedBinaryString2 = String.
format("%1$" + length + "s", unpaddedBinaryString2).
replace(' ', '0');

List<Integer> 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<Integer> generateTour(String assignment) {
List<Integer> 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() {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/io/gocklkatz/m12s/vrp/VehicleRoutingProblemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Integer>> 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);
}
}

0 comments on commit f223efe

Please sign in to comment.