get best path

This commit is contained in:
Hendrik Schutter 2019-05-08 00:18:30 +02:00
parent e836dfc4e2
commit 133c386a5d
4 changed files with 189 additions and 7 deletions

Binary file not shown.

View File

@ -10,14 +10,31 @@ void *Search(void* arguments);
int main() {
printf("hello world!\n");
pthread_t startThread;
THREAD_DATA_t* data;
data = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
data->node = getStartNode();
data->cost = 0;
data->depth = 0;
data->predecessors[0] = data->node->id;
data->bestpath = (BESTPATH_t*) malloc(sizeof(BESTPATH_t));
data->bestpath->lowestCost = -1;
data->bestpath->depth = 0;
pthread_create(&startThread, NULL, Search, data);
pthread_join(startThread, NULL);
printf("\nBest Path: ");
for(int i = 0; i < data->bestpath->depth; i++){
printf("%i --> ", data->bestpath->predecessorsBestPath[i]);
}
printf("\nWith cost of: %i\n", data->bestpath->lowestCost);
for(;;) {
}
@ -34,7 +51,34 @@ void *Search(void* arguments) {
printf("Cost to this node: %i\n", data->cost);
if(data->node->id == 8) {
printf("End found! Cost: %i\n", data->cost);
printf("!!!End found! Cost: %i\n", data->cost);
//critical section start
if(data->bestpath->lowestCost == -1) {
//first solution found
printf("first solution found\n");
data->bestpath->lowestCost = data->cost;
data->bestpath->depth = data->depth;
for(int j = 0; j < data->depth; j++) {
data->bestpath->predecessorsBestPath[j] = data->predecessors[j];
}
} else {
if(data->bestpath->lowestCost > data->cost) {
//new best solution found
printf("new best solution found\n");
data->bestpath->lowestCost = data->cost;
data->bestpath->depth = data->depth;
for(int j = 0; j < data->depth; j++) {
data->bestpath->predecessorsBestPath[j] = data->predecessors[j];
}
}
}
//critical section end
return NULL;
}
@ -49,9 +93,21 @@ void *Search(void* arguments) {
THREAD_DATA_t* nextdata;
nextdata = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
nextdata->node = (data->node->nodesnext[i]);
nextdata->bestpath = data->bestpath;
nextdata->cost = data->node->nodescost[i] + data->cost;
nextdata->depth++;
for(int j = 0; j < data->depth; j++) {
nextdata->predecessors[j] = data->predecessors[j];
}
nextdata->predecessors[(nextdata->depth)+1] = data->node->id;
pthread_create(&threads[i], NULL, Search, nextdata);
}
for(int i = 0; i < data->node->nodescount; i++) {
pthread_join(threads[i],NULL);
}
return NULL;
}

113
src/routing4SITA.c.orig Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "routing4SITA.h"
void *Search(void* arguments);
int main() {
printf("hello world!\n");
pthread_t startThread;
THREAD_DATA_t* data;
data = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
data->node = getStartNode();
data->cost = 0;
data->depth = 0;
data->predecessors[0] = data->node->id;
data->bestpath = (BESTPATH_t*) malloc(sizeof(BESTPATH_t));
data->bestpath->lowestCost = -1;
pthread_create(&startThread, NULL, Search, data);
for(;;) {
}
return 0;
}
void *Search(void* arguments) {
THREAD_DATA_t *data = (THREAD_DATA_t*) arguments;
printf("Number: %i\n", data->node->id);
printf("Cost to this node: %i\n", data->cost);
if(data->node->id == 8) {
printf("End found! Cost: %i\n", data->cost);
//critical section
if(data->bestpath->lowestCost == -1){
//first solution found
data->bestpath->lowestCost = data->cost;
for(int j = 0; j < data->depth; j++){
nextdata->predecessors[j] = data->predecessors[j];
}
}else{
if(data->bestpath->lowestCost > data->cost){
//new best solution found
data->bestpath->lowestCost = data->cost;
for(int j = 0; j < data->depth; j++){
nextdata->predecessors[j] = data->predecessors[j];
}
}
}
return NULL;
}
if(data->node->nodescount == 0) {
printf("No next nodes\n");
return NULL;
}
pthread_t threads[data->node->nodescount];
for(int i = 0; i < data->node->nodescount; i++) {
THREAD_DATA_t* nextdata;
nextdata = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
nextdata->node = (data->node->nodesnext[i]);
nextdata->cost = data->node->nodescost[i] + data->cost;
nextdata->depth++;
for(int j = 0; j < data->depth; j++){
nextdata->predecessors[j] = data->predecessors[j];
}
nextdata->predecessors[nextdata->depth] = data->node->id;
pthread_create(&threads[i], NULL, Search, nextdata);
}
return NULL;
}

View File

@ -2,23 +2,36 @@
#define ROUTING4SITA_H
#define MAX_CHILDREN 3
#define MAX_DEPTH 4
typedef struct node NODE_t;
typedef struct threadData THREAD_DATA_t;
typedef struct bestPath BESTPATH_t;
extern NODE_t* getStartNode();
struct node {
int id;
int nodescount;
NODE_t* nodesnext[MAX_CHILDREN];
int nodescost[MAX_CHILDREN];
int id; //identifier for this node
int nodescount; //how many children nodes
NODE_t* nodesnext[MAX_CHILDREN]; //array of children nodes
int nodescost[MAX_CHILDREN]; //array of cost per children node
};
struct threadData {
NODE_t* node;
int cost;
int depth; //depth of the next node, e.g. start --> 0
NODE_t* node; //next node to go
int cost; //cost for the next node
int predecessors[MAX_DEPTH]; // array with all predecessor
BESTPATH_t* bestpath;
};
struct bestPath {
int lowestCost;
int predecessorsBestPath[MAX_DEPTH]; // array with all predecessor of the best path
int depth; //depth of the best path, aka. how many nodes
};
#endif /* ROUTING4SITA_H */