Add edge tagging
This commit is contained in:
parent
004ee52ae4
commit
4887235d61
@ -71,6 +71,7 @@ int main(int argc, char *argv[]) {
|
|||||||
char *v = strtok(NULL, "");
|
char *v = strtok(NULL, "");
|
||||||
edges[i-1].u = strtol(u, NULL, 10);
|
edges[i-1].u = strtol(u, NULL, 10);
|
||||||
edges[i-1].v = strtol(v, NULL, 10);
|
edges[i-1].v = strtol(v, NULL, 10);
|
||||||
|
edges[i-1].chosen = false;
|
||||||
|
|
||||||
if(num_nodes < edges[i-1].u)
|
if(num_nodes < edges[i-1].u)
|
||||||
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
|
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];
|
int unique_nodes[num_nodes];
|
||||||
bool chosen_edges[num_edges];
|
|
||||||
|
|
||||||
/// Filling the node array with the nodes
|
/// Filling the node array with the nodes
|
||||||
for(int i=0; i < num_nodes; i++) {
|
for(int i=0; i < num_nodes; i++) {
|
||||||
unique_nodes[i] = 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
|
/// Allocate resources for circular buffer
|
||||||
int wr_pos = 0;
|
int wr_pos = 0;
|
||||||
buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
|
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);
|
use_sem = open_sem(USE_SEM_NAME, 0, 1);
|
||||||
mutex = open_sem(MUTEX_NAME, 1, 1);
|
mutex = open_sem(MUTEX_NAME, 1, 1);
|
||||||
|
|
||||||
int best_solution = num_edges - 1; ///< Set a basic best solution
|
/// Declaring buffer arrays and variables
|
||||||
|
int current_best = num_edges - 1; ///< Set a basic best solution
|
||||||
/// Declaring buffer arrays
|
|
||||||
int buf_nodes[num_nodes];
|
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
|
/// Copy the original into a buffer array and shuffle it
|
||||||
memcpy(buf_nodes, unique_nodes, sizeof(unique_nodes));
|
memcpy(buf_nodes, unique_nodes, sizeof(unique_nodes));
|
||||||
shuffle(buf_nodes, num_nodes);
|
shuffle(buf_nodes, num_nodes);
|
||||||
|
|
||||||
/// Reset the chosen status of the array
|
/// Reset the chosen status of the edges
|
||||||
memcpy(buf_chosen_edges, chosen_edges, sizeof(chosen_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);
|
exit(EXIT_SUCCESS);
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
/// Struct for the edge
|
/// Struct for the edge
|
||||||
typedef struct edge {
|
typedef struct edge {
|
||||||
|
bool chosen;
|
||||||
int u, v;
|
int u, v;
|
||||||
} edge_t;
|
} edge_t;
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ int main(int argc, char *argv[]) {
|
|||||||
int free_space = 0;
|
int free_space = 0;
|
||||||
int use_space = 0;
|
int use_space = 0;
|
||||||
int r_pos = 0; ///< Position of the circular buffer to read from
|
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;
|
fb_arc_set_t fb_arc;
|
||||||
while(terminate == false) {
|
while(terminate == false) {
|
||||||
@ -82,8 +82,8 @@ int main(int argc, char *argv[]) {
|
|||||||
signal_handler(SIGINT);
|
signal_handler(SIGINT);
|
||||||
} else if(fb_arc.edge_num < circ_buffer->best_arc_len) {
|
} else if(fb_arc.edge_num < circ_buffer->best_arc_len) {
|
||||||
/// Save ther best feedback arc length and print a solution
|
/// Save ther best feedback arc length and print a solution
|
||||||
circ_buffer->best_arc_len = fb_arc.edge_num;
|
circ_buffer -> best_arc_len = fb_arc.edge_num;
|
||||||
printf("Solution with %d edges: ", circ_buffer->best_arc_len);
|
printf("Solution with %d edges: ", circ_buffer -> best_arc_len);
|
||||||
|
|
||||||
for(int i=0; i < fb_arc.edge_num; i++) {
|
for(int i=0; i < fb_arc.edge_num; i++) {
|
||||||
printf("%d-%d ", fb_arc.edges[i].u, fb_arc.edges[i].v);
|
printf("%d-%d ", fb_arc.edges[i].u, fb_arc.edges[i].v);
|
||||||
|
Reference in New Issue
Block a user