diff --git a/fb_arc_set/generator.c b/fb_arc_set/generator.c index 8009e5b..34b0b2a 100644 --- a/fb_arc_set/generator.c +++ b/fb_arc_set/generator.c @@ -71,6 +71,7 @@ int main(int argc, char *argv[]) { char *v = strtok(NULL, ""); edges[i-1].u = strtol(u, NULL, 10); edges[i-1].v = strtol(v, NULL, 10); + edges[i-1].chosen = false; if(num_nodes < edges[i-1].u) num_nodes = edges[i-1].u; @@ -80,20 +81,14 @@ int main(int argc, char *argv[]) { } num_nodes++; ///< Our first node is labeled 0, so we need to add 1 more - /// Declare arrays for unique nodes and for the chosen edges + /// Declare arrays for unique nodes int unique_nodes[num_nodes]; - bool chosen_edges[num_edges]; /// Filling the node array with the nodes for(int i=0; i < num_nodes; i++) { unique_nodes[i] = i; } - /// Marking all edges as not chosen - for(int i=0; i < num_edges; i++) { - chosen_edges[i] = false; - } - /// Allocate resources for circular buffer int wr_pos = 0; buffer_shmfd = create_shmfd(BUFF_SHM_NAME); @@ -104,19 +99,40 @@ int main(int argc, char *argv[]) { use_sem = open_sem(USE_SEM_NAME, 0, 1); mutex = open_sem(MUTEX_NAME, 1, 1); - int best_solution = num_edges - 1; ///< Set a basic best solution - - /// Declaring buffer arrays + /// Declaring buffer arrays and variables + int current_best = num_edges - 1; ///< Set a basic best solution int buf_nodes[num_nodes]; - int buf_chosen_edges[num_edges]; + edge_t buf_edges[num_edges]; + int new_best = 0; - while(terminate != true && circ_buffer->finished != true) { ///< Loop until told to stop or until an acyclic solution is discovered + while(terminate != true && circ_buffer -> finished != true) { ///< Loop until told to stop or until an acyclic solution is discovered /// Copy the original into a buffer array and shuffle it memcpy(buf_nodes, unique_nodes, sizeof(unique_nodes)); shuffle(buf_nodes, num_nodes); - /// Reset the chosen status of the array - memcpy(buf_chosen_edges, chosen_edges, sizeof(chosen_edges)); + /// Reset the chosen status of the edges + memcpy(buf_edges, edges, sizeof(edges)); + + new_best = 0; ///< Reset the new_besy counter + for(int i=0; i < num_edges; i++) { + if(new_best >= SET_MAX_SIZE || new_best >= num_edges) + break; ///< Skip if the solution is faulty + + for(int j=0; j < num_nodes; j++) { + if(buf_nodes[j] == edges[i].u) + break; ///< The first node comes before the second, break + else if(buf_nodes[j] == buf_edges[i].v) { + buf_edges[i].chosen = true; ///< Found a correct edge, mark it as chosen + new_best++; + break; + } + } + } + + /// Construct the new feedback arc set + if(new_best < current_best && new_best <= SET_MAX_SIZE) { + current_best = new_best; + } } exit(EXIT_SUCCESS); diff --git a/fb_arc_set/shared/structs.c b/fb_arc_set/shared/structs.c index e869065..bbd27e1 100644 --- a/fb_arc_set/shared/structs.c +++ b/fb_arc_set/shared/structs.c @@ -6,6 +6,7 @@ /// Struct for the edge typedef struct edge { + bool chosen; int u, v; } edge_t; diff --git a/fb_arc_set/supervisor.c b/fb_arc_set/supervisor.c index 7fc65a7..09941bd 100644 --- a/fb_arc_set/supervisor.c +++ b/fb_arc_set/supervisor.c @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) { int free_space = 0; int use_space = 0; int r_pos = 0; ///< Position of the circular buffer to read from - circ_buffer->best_arc_len = __INT8_MAX__; + circ_buffer -> best_arc_len = __INT8_MAX__; fb_arc_set_t fb_arc; while(terminate == false) { @@ -82,8 +82,8 @@ int main(int argc, char *argv[]) { signal_handler(SIGINT); } else if(fb_arc.edge_num < circ_buffer->best_arc_len) { /// Save ther best feedback arc length and print a solution - circ_buffer->best_arc_len = fb_arc.edge_num; - printf("Solution with %d edges: ", circ_buffer->best_arc_len); + circ_buffer -> best_arc_len = fb_arc.edge_num; + printf("Solution with %d edges: ", circ_buffer -> best_arc_len); for(int i=0; i < fb_arc.edge_num; i++) { printf("%d-%d ", fb_arc.edges[i].u, fb_arc.edges[i].v);