Added code for replacing bestSolution so far with new one

This commit is contained in:
zenon 2018-06-07 16:35:01 +02:00
parent 699d1b9ed4
commit 1fbd1bf2b8

View File

@ -13,6 +13,8 @@ struct circ_buf *shared;
struct sigaction sa; struct sigaction sa;
static int bestSolution[MAX_ITEMS] = {-1};
static volatile sig_atomic_t quit = 0; static volatile sig_atomic_t quit = 0;
static void sig_handler(int signum) static void sig_handler(int signum)
@ -53,7 +55,44 @@ void getSolution(sem_t sUsedSpace, sem_t sFreeSpace, int *solution)
} }
} }
// Code for testing wether new solution is better than old // Code for testing if new solution is better than old
int bestSolutionLength = 0;
int newSolutionLength = 0;
for (int i = 0; i < MAX_ITEMS; i++) {
if ((bestSolution[i] == -1) && (i != 0)) {
bestSolutionLength = i+1;
break;
}
}
for (int i = 0; i < MAX_ITEMS; i++) {
if ((solution[i] == -1) && (i != 0)) {
newSolutionLength = i+1;
break;
}
}
if (newSolutionLength == 0) {
printf("[%s] The graph is acyclic!\n", pname);
return;
}
if (newSolutionLength < bestSolutionLength) {
// Set all entries of bestSolution to -1
for (int i = 0; i < bestSolutionLength; i++) {
bestSolution[i] = -1;
}
// Copy all entries from solution to bestSolution
for (int i = 0; i < newSolutionLength; i++) {
bestSolution[i] = solution[i];
}
printf("[%s] Solution with %d edges:", pname, newSolutionLength/2);
for (int i = 0; i < newSolutionLength; i += 2) {
printf(" %d-%d", bestSolution[i], bestSolution[i+1]);
}
printf("\n");
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])