Skip to content

Commit

Permalink
implemented adapter to make Lucas LKH3 compatible for TSP solving
Browse files Browse the repository at this point in the history
  • Loading branch information
koalamitice committed Jul 23, 2024
1 parent 8a27bf4 commit a93d142
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,41 @@ public String getName() {

@Override
public Mono<Solution<String>> solve(String input, SubRoutineResolver subRoutineResolver) {
var solution = new Solution<String>();

// the following code will transform the TSP problem into a VRP problem
// dummy demand and depot sections are added, every city has a demand of 1.
// this is not an elegant way to solve TSP, but needed to make Lucas' code work.

// read dimension from input:
int dimension = 0;
String[] lines = input.split("\n");
for (String line : lines) {
if (line.contains("DIMENSION")) {
dimension = Integer.parseInt(line.split(":")[1].trim());
break;
}
}
System.out.println("dimension: " + dimension);
int capacity = dimension - 1;

if (input.contains("TYPE : TSP")) {
input = input.replace("TYPE : TSP", "TYPE : CVRP\nCAPACITY : " + capacity);
}

if (!input.contains("DEPOT_SECTION:") && !input.contains("DEMAND_SECTION:")) {
if (input.contains("EOF")) {
input = input.replace("EOF", "");
}
// add depot section dummy:
input = input.concat("DEPOT_SECTION:\n1\n-1\n");
// add dummy for demands:
input = input.concat("DEMAND_SECTION:\n1 0\n");
for (int i = 2; i <= dimension; i++) {
input = input.concat(i + " 1\n");
}
}

var solution = new Solution<String>();
var processResult = context.getBean(
PythonProcessRunner.class,
scriptDir,
Expand All @@ -50,9 +83,6 @@ public Mono<Solution<String>> solve(String input, SubRoutineResolver subRoutineR
.solutionFileName("problem.sol")
.run(getProblemType(), solution.getId(), input);

//TODO: write new wrapper that solves TSP problems with LKH-3
//TODO: change ProcessRunner call to new wrapper

return Mono.just(processResult.applyTo(solution));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public Mono<Solution<String>> solve(
.run(getProblemType(), solution.getId(), input,
new MultiFileProcessResultReader("./.vrp/problem_*.vrp"));

System.out.print(processResult.output());

return getSolutionForCluster(input, solution, processResult, resolver, TSP_SUBROUTINE);
}
}
3 changes: 0 additions & 3 deletions src/test/java/edu/kit/provideq/toolbox/api/TspSolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ void beforeEach() {
void testLkhTspSolver() {
for (String problem : problems) {
var problemDto = ApiTestHelper.createProblem(client, lkhTspSolver, problem, TSP);

System.out.println(problemDto.getSolution());

assertEquals(ProblemState.SOLVED, problemDto.getState());
assertNotNull(problemDto.getSolution());
assertEquals(SolutionStatus.SOLVED, problemDto.getSolution().getStatus());
Expand Down

0 comments on commit a93d142

Please sign in to comment.