diff --git a/src/algorithm.c b/src/algorithm.c index ce05418..333c82d 100644 --- a/src/algorithm.c +++ b/src/algorithm.c @@ -758,6 +758,7 @@ void move_colle(creneau* edt, int len_edt, int id_src, int id_dest) { } void swap_colle(creneau* edt, int len_edt, int id_src, int id_dest) { + // BROKEN if((id_src < len_edt && id_dest < len_edt) == false) { printf("Bad\n"); exit(1); @@ -767,11 +768,11 @@ void swap_colle(creneau* edt, int len_edt, int id_src, int id_dest) { edt[id_dest].group = edt[id_src].group; //printf("%d %d\n", edt[id_src].group, dest_grp); - //edt[id_src].group = edt[id_src].group; + edt[id_src].group = edt[id_src].group; } -int mem_id(creneau* edt, int len_edt, int grp, char* colleur) { - for(int i = 0; i < len_edt; i++) { +int mem_id(creneau* edt, int len_edt, int grp, char* colleur, int offset) { + for(int i = offset; i < len_edt; i++) { if(edt[i].group == grp && str_equal(edt[i].name, colleur)) { return i; } @@ -1146,6 +1147,147 @@ topic get_mat_from_id(colleur* dudes, int n_dudes, int id) { return NOTHING; } +int** generate_matrix(int lines, int columns, int def) { + int** mat = malloc(sizeof(int*)*lines); + for(int i = 0; i < lines; i++) { + mat[i] = malloc(sizeof(int)*columns); + for(int j = 0; j < columns; j++) { + mat[i][j] = def; + } + } + return mat; +} + +void destroy_matrix(int** m, int li) { + for(int i = 0; i < li; i++) { + free(m[i]); + } + free(m); +} + +bool retreive_indexes(creneau* edt, int len_edt, int* p1, int* p2, int g1, int g2, char* n1, char* n2, int n_weeks) { + int* indexes_1 = malloc(sizeof(int)*n_weeks); + int* indexes_2 = malloc(sizeof(int)*n_weeks); + int ptr_1 = 0; + int ptr_2 = 0; + + for(int i = 0; i < len_edt; i++) { + //printf("%d\n", i); + if(edt[i].group == g1 && str_equal(edt[i].name, n1)) { + indexes_1[ptr_1] = i; + ptr_1++; + } else if(edt[i].group == g2 && str_equal(edt[i].name, n2)) { + indexes_2[ptr_2] = i; + ptr_2++; + } + } + + //print_arr(indexes_1, ptr_1); + //print_arr(indexes_2, ptr_2); + + int j = 0; + for(int i = 0; i < ptr_1; i++) { + if(j >= ptr_2) { + free(indexes_1); + free(indexes_2); + return false; + } else { + //printf("{%d %d}\n", i, j); + int friday_1 = get_next_friday(edt, len_edt, edt[indexes_1[i]].date); + int friday_2 = get_next_friday(edt, len_edt, edt[indexes_2[j]].date); + if(friday_1 == friday_2) { + *p1 = indexes_1[i]; + *p2 = indexes_2[j]; + + free(indexes_1); + free(indexes_2); + return true; + } else { + if(friday_1 > friday_2) { + i--; + j++; + } + } + } + } + + free(indexes_1); + free(indexes_2); + return false; +} + +void occurencies(creneau* edt, int len_edt, colleur* dudes, int n_groups, int n_colleurs, int n_weeks) { + //printf("{%d}\n", n_groups); + int max_occ = 1; + int** res = generate_matrix(n_groups, n_colleurs, 0); + + for(int c = 0; c < len_edt; c++) { + if(edt[c].group != 0) { + res[edt[c].group-1][get_colleur_id(dudes, n_colleurs, edt[c].name)] += 1; + } + } + + /*for(int k = 0; k < n_groups; k++) { + printf("Group %d : ", k+1); + print_arr(res[k], n_colleurs); + } + printf("\n"); // caused a minor panic */ + + bool halt = false; + for(int it = 0; it < 3; it++) { + for(int grp = 0; grp < n_groups; grp++) { + for(int dud = 0; dud < n_colleurs; dud++) { + if(res[grp][dud] > max_occ) { + for(int grp2 = 0; grp2 < n_groups*(1-halt); grp2++) { + if(grp2 != grp) { + for(int dud2 = 0; dud2 < n_colleurs*(1-halt); dud2++) { + if(dud2 != dud && res[grp2][dud2] > max_occ && res[grp][dud2] < max_occ && res[grp2][dud] < max_occ && get_mat_from_id(dudes, n_colleurs, dud) == get_mat_from_id(dudes, n_colleurs, dud2)) { + + int id_src; + int id_dest; + if(retreive_indexes(edt, len_edt, &id_src, &id_dest, grp+1, grp2+1, get_name_from_id(dudes, n_colleurs, dud), get_name_from_id(dudes, n_colleurs, dud2), n_weeks)) {; + // RESOLVED : both colles need to be the same week + + if(id_src < 0 || id_dest < 0 || id_src >= len_edt || id_dest >= len_edt) { + printf("Uh oh (%d %d)\n", id_src, id_dest); + exit(1); + } + + res[grp][dud] -= 1; + res[grp2][dud2] -= 1; + res[grp2][dud] += 1; + res[grp][dud2] += 1; + + //printf("%d %d | ", edt[id_src].group, edt[id_dest].group); + edt[id_src].group += edt[id_dest].group; + edt[id_dest].group = edt[id_src].group - edt[id_dest].group; + edt[id_src].group -= edt[id_dest].group; + //printf("%d %d\n", edt[id_src].group, edt[id_dest].group); + + //printf("[%d %d - %d] <==> [%d %d - %d]\n", grp+1, id_src, edt[id_src].group, grp2+1, id_dest, edt[id_dest].group); + halt = true; + dud--; + } + } + } + } + } + } + halt = false; + } + } + } + + /* + for(int k = 0; k < n_groups; k++) { + printf("Group %d : ", k+1); + print_arr(res[k], n_colleurs); + } + printf("\n");*/ + + destroy_matrix(res, n_groups); +} + void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_groups, int n_weeks, int n_sim, char* outname) { int start = time(NULL); int* weeks_len = malloc(sizeof(int)*n_weeks); @@ -1192,6 +1334,7 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group //add_colles_for_group_MP2I(weeks_len, edt, len_edt, chads, len_chads, n_weeks, i+1, (topic)(2+i%2), i%4, i%6, &skipped); add_colles_for_group_MP2I(weeks_len, edt, len_edt, chads, len_chads, n_weeks, i+1, (topic)(2+i%2), i%3, -20, &skipped); } + occurencies(edt, len_edt, chads, n_groups, len_chads, n_weeks); for(int i = 0; i < n_groups; i++) { //printf("Score for group %d : %d\n", i+1, score(edt, len_edt, i+1)); temp = score(edt, len_edt, i+1, n_weeks); diff --git a/src/algorithm.h b/src/algorithm.h index a79bc72..69dae0a 100644 --- a/src/algorithm.h +++ b/src/algorithm.h @@ -46,7 +46,7 @@ void move_colle(creneau* edt, int len_edt, int id_src, int id_dest); void swap_colle(creneau* edt, int len_edt, int id_src, int id_dest); -int mem_id(creneau* edt, int len_edt, int grp, char* colleur); +int mem_id(creneau* edt, int len_edt, int grp, char* colleur, int offset); bool is_overlap(creneau *edt, int len_edt, int id); @@ -66,6 +66,14 @@ char* get_name_from_id(colleur* dudes, int n_dudes, int id); topic get_mat_from_id(colleur* dudes, int n_dudes, int id); +int** generate_matrix(int lines, int columns, int def); + +void destroy_matrix(int** m, int li); + +bool retreive_indexes(creneau* edt, int len_edt, int* p1, int* p2, int g1, int g2, char* n1, char* n2, int n_weeks); + +void occurencies(creneau* edt, int len_edt, colleur* dudes, int n_groups, int n_colleurs, int n_weeks); + void aux_2(creneau *edt, int len_edt, colleur *chads, int len_chads, int n_groups, int n_weeks, int n_sim, char *outname); #endif