fixed best path algo

This commit is contained in:
Hendrik Schutter 2019-05-08 00:42:24 +02:00
parent 133c386a5d
commit e336a1d4fc
2 changed files with 17 additions and 7 deletions

Binary file not shown.

View File

@ -28,10 +28,14 @@ int main() {
pthread_join(startThread, NULL); pthread_join(startThread, NULL);
printf("\nBest Path: ");
for(int i = 0; i < data->bestpath->depth; i++){ printf("pathdepth: %i\n", data->bestpath->depth);
printf("%i --> ", data->bestpath->predecessorsBestPath[i]);
printf("\nBest Path: %i", data->bestpath->predecessorsBestPath[0]);
for(int i = 1; i <= data->bestpath->depth; i++){
printf(" --> %i", data->bestpath->predecessorsBestPath[i]);
} }
printf("\nWith cost of: %i\n", data->bestpath->lowestCost); printf("\nWith cost of: %i\n", data->bestpath->lowestCost);
@ -57,22 +61,24 @@ void *Search(void* arguments) {
if(data->bestpath->lowestCost == -1) { if(data->bestpath->lowestCost == -1) {
//first solution found //first solution found
printf("first solution found\n"); printf("first solution found! %i\n", data->depth);
data->bestpath->lowestCost = data->cost; data->bestpath->lowestCost = data->cost;
data->bestpath->depth = data->depth; data->bestpath->depth = data->depth;
for(int j = 0; j < data->depth; j++) { for(int j = 0; j < data->depth; j++) {
data->bestpath->predecessorsBestPath[j] = data->predecessors[j]; data->bestpath->predecessorsBestPath[j] = data->predecessors[j];
} }
data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id;
} else { } else {
if(data->bestpath->lowestCost > data->cost) { if(data->bestpath->lowestCost > data->cost) {
//new best solution found //new best solution found
printf("new best solution found\n"); printf("new best solution found! %i\n", data->depth);
data->bestpath->lowestCost = data->cost; data->bestpath->lowestCost = data->cost;
data->bestpath->depth = data->depth; data->bestpath->depth = data->depth;
for(int j = 0; j < data->depth; j++) { for(int j = 0; j < data->depth; j++) {
data->bestpath->predecessorsBestPath[j] = data->predecessors[j]; data->bestpath->predecessorsBestPath[j] = data->predecessors[j];
} }
data->bestpath->predecessorsBestPath[(data->bestpath->depth)] = data->node->id;
} }
} }
@ -95,12 +101,16 @@ void *Search(void* arguments) {
nextdata->node = (data->node->nodesnext[i]); nextdata->node = (data->node->nodesnext[i]);
nextdata->bestpath = data->bestpath; nextdata->bestpath = data->bestpath;
nextdata->cost = data->node->nodescost[i] + data->cost; nextdata->cost = data->node->nodescost[i] + data->cost;
nextdata->depth++; nextdata->depth = data->depth + 1;
for(int j = 0; j < data->depth; j++) { for(int j = 0; j < data->depth; j++) {
nextdata->predecessors[j] = data->predecessors[j]; nextdata->predecessors[j] = data->predecessors[j];
printf("data->predecessors: %i\n", data->predecessors[j]);
} }
nextdata->predecessors[(nextdata->depth)+1] = data->node->id;
nextdata->predecessors[((nextdata->depth) - 1)] = data->node->id;
pthread_create(&threads[i], NULL, Search, nextdata); pthread_create(&threads[i], NULL, Search, nextdata);
} }