diff --git a/algorithm.c b/algorithm.c index 89f41eb..f064129 100644 --- a/algorithm.c +++ b/algorithm.c @@ -718,7 +718,7 @@ void remove_colle(creneau* edt, int id_edt) { edt[id_edt].mat = NOTHING; } -bool is_overlap(creneau* edt, int len_edt, int id) { +bool is_overlap(creneau* edt, int len_edt, int id) { // detect if a colleur has 2 colles at the same time int k = 1; while(id+k < len_edt && is_equal_date(edt[id].date, edt[id+k].date)) { if(str_equal(edt[id].name, edt[id+k].name)) { @@ -736,6 +736,17 @@ bool is_overlap(creneau* edt, int len_edt, int id) { return false; } +bool is_overlap_creneau(creneau* edt, int len_edt, int id, int grp) { // detect if a group has 2 overlapping colles + int k = 1; + while((id+k < len_edt && edt[id+k].date.hour - edt[id].date.hour < edt[id].length) && edt[id+k].date.day == edt[id].date.day) { + if(edt[id+k].group == grp) { + return true; + } + k++; + } + return false; +} + void add_colles_for_group_MP2I(int* weeks_len, creneau* edt, int len_edt, colleur* chads, int len_chads, int n_weeks, int grp, topic start_rotation, int mth, int inf) { topic rotation = start_rotation; // physics/english rotation int math = mth; // math (3/4) @@ -801,7 +812,7 @@ void add_colles_for_group_MP2I(int* weeks_len, creneau* edt, int len_edt, colleu // - he is a INFO colleur // if a colle has been addded, interrupt the for and while loops for(int dude = 0; dude < len_perm*(1-found); dude++) { - if(chads[perm[dude]].mat == INFO) { + if(chads[perm[dude]].mat == INFO && !is_overlap_creneau(edt, len_edt, k+r%weeklen, grp)) { add_colle(edt, chads, grp, k+r%weeklen, perm[dude]); found = true; info = 0; @@ -923,8 +934,49 @@ void write_to_file(char* filename, creneau* edt, int len_edt) { fclose(ptr); } +int min(int x, int y) { + if(x < y) { + return x; + } + return y; +} -void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_groups, int n_weeks) { +int max(int x, int y) { + if(x > y) { + return x; + } + return y; +} + +int score(creneau* edt, int len_edt, int grp) { + int score = 100; + + int dist = 0; + for(int i = 0; i < len_edt; i++) { + for(int j = i+1; j < len_edt; j++) { + if(edt[i].group == grp && edt[j].group == grp) { + dist = date_dist(edt[i].date, edt[j].date); + if(dist == 0) { + score -= 7; + } + if(str_equal(edt[i].name, edt[j].name)) { + score -= 5*min(0, 28-dist); + } + if(dist == 7 && edt[i].date.hour == edt[j].date.hour) { + score -= 5; + } + if(dist < 7 && edt[i].date.hour == 18 && edt[j].date.hour == 18) { + score -= 10; + } + } + } + } + + return score; +} + +void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_groups, int n_weeks, int n_sim) { + int start = time(NULL); int* weeks_len = malloc(sizeof(int)*n_weeks); // this list is used to tell the above code what index of edt it has to go to search for a colle int ptr = 0; @@ -938,11 +990,7 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group current++; } - for(int i = 0; i < n_groups; i++) { - printf("Adding colles for group %d...\n", i+1); - add_colles_for_group_MP2I(weeks_len, edt, len_edt, chads, len_chads, n_weeks, i+1, (topic)(2+i%2), i%4,1+i%4); - } - + /* print_one_week(edt, len_edt, edt[0].date); printf("\n"); print_one_week(edt, len_edt, edt[19].date); @@ -950,9 +998,60 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group print_one_week(edt, len_edt, edt[38].date); printf("\n"); print_one_week(edt, len_edt, edt[57].date); + */ + printf("\n"); - write_to_file("output.csv", edt, len_edt); - printf("Completed\n"); + int max_score = 0; + int global_min = 0; + int screwed_group = 0; + int local_score = 0; + int local_min = 0; + int local_group = 0; + int temp = 0; + int a = n_sim/100; + for(int k = 0; k < n_sim*(1 - (max_score == n_groups*100)); k++) { + if(k >= a) { + printf("\x1b[1F"); + printf("\x1b[2K"); + printf("%d% Completed (current max is %d/%d)\n", 100*a/n_sim, max_score, 100*n_groups); + a += n_sim/100; + } + local_score = 0; + local_min = 100; + for(int i = 0; i < n_groups; i++) { + //rintf("Adding colles for group %d...\n", i+1); + add_colles_for_group_MP2I(weeks_len, edt, len_edt, chads, len_chads, n_weeks, i+1, (topic)(2+i%2), i%4,1+i%4); + } + 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); + local_score += temp; + if(local_min > temp) { + local_min = temp; + local_group = i+1; + } + } + if(local_score > max_score) { + max_score = local_score; + screwed_group = local_group; + global_min = local_min; + write_to_file("output.csv", edt, len_edt); + } + for(int r = 0; r < len_edt; r++) { + remove_colle(edt, r); + } + } + + printf("\x1b[1F"); + printf("\x1b[2K"); + if(max_score == 100*n_groups) { + printf("Interrupting early due to a perfect score achieved\n"); + } else { + printf("100% Completed, best score is %d/%d\n", max_score, 100*n_groups); + printf("Most screwed group is %d with a score of %d/100\n", screwed_group, global_min); + } + int end = time(NULL); + printf("Took %ds to found\n", end-start); } @@ -964,5 +1063,3 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group - - diff --git a/main.c b/main.c index 5412270..104d584 100644 --- a/main.c +++ b/main.c @@ -18,7 +18,7 @@ int main() { /*int* a = malloc(sizeof(int)*10); generate_random_perm(a, 10); print_arr(a, 10);*/ - aux_2(edt, len_creneau, dudes, n_colleurs, 7, 4); + aux_2(edt, len_creneau, dudes, n_colleurs, 7, 4, 3000); //generate_colles_v1(edt, len_creneau, dudes, n_colleurs, 9, 4); //generate_colles_v1(edt, len_creneau, dudes, n_colleurs, 15, 4); diff --git a/output.csv b/output.csv index b0044bb..81f9ea0 100644 --- a/output.csv +++ b/output.csv @@ -1,77 +1,77 @@ hour,day,month,year,length,group,colleur,matiere -12,5,2,2024,1,2,Rapin,Maths -13,5,2,2024,1,1,Herbaud,Anglais -18,5,2,2024,1,2,Chibani,Physique -17,6,2,2024,1,3,Rapin,Maths -18,6,2,2024,1,3,Belaggoune,Anglais -14,7,2,2024,1,4,Chevalier,Physique +12,5,2,2024,1,1,Herbaud,Anglais +13,5,2,2024,1,2,Colin,Physique +18,5,2,2024,1,0,none,none +17,6,2,2024,1,4,Poupy,Physique +18,6,2,2024,1,6,Carpintero,Maths +14,7,2,2024,1,7,Mullaert,Maths 14,7,2,2024,2,4,Jospin,Info 14,7,2,2024,2,0,none,none 14,7,2,2024,2,0,none,none -15,7,2,2024,1,5,Mann,Anglais -16,7,2,2024,1,4,Mullaert,Maths -17,7,2,2024,1,7,Mullaert,Maths -18,7,2,2024,1,7,Belaggoune,Anglais -14,8,2,2024,1,6,Chevalier,Physique -15,8,2,2024,1,6,Mullaert,Maths -18,8,2,2024,1,0,none,none +15,7,2,2024,1,3,Boully,Maths +16,7,2,2024,1,3,Belaggoune,Anglais +17,7,2,2024,1,6,Chibani,Physique +18,7,2,2024,1,5,Le_Gourielec,Anglais +14,8,2,2024,1,4,Rapin,Maths +15,8,2,2024,1,2,Rapin,Maths +18,8,2,2024,1,7,Belaggoune,Anglais 16,9,2,2024,1,0,none,none 17,9,2,2024,1,0,none,none 18,9,2,2024,1,0,none,none -12,12,2,2024,1,0,none,none -13,12,2,2024,1,2,Le_Gourielec,Anglais -18,12,2,2024,1,5,Boully,Maths -17,13,2,2024,1,4,Le_Gourielec,Anglais -18,13,2,2024,1,3,Carpintero,Maths -14,14,2,2024,1,5,Chevalier,Physique +12,12,2,2024,1,7,Chibani,Physique +13,12,2,2024,1,0,none,none +18,12,2,2024,1,3,Colin,Physique +17,13,2,2024,1,5,Chevalier,Physique +18,13,2,2024,1,0,none,none +14,14,2,2024,1,6,Mann,Anglais 14,14,2,2024,2,3,Jospin,Info 14,14,2,2024,2,7,Jospin,Info 14,14,2,2024,2,0,none,none -15,14,2,2024,1,1,Chevalier,Physique -16,14,2,2024,1,1,Carpintero,Maths -17,14,2,2024,1,3,Poupy,Physique -18,14,2,2024,1,4,Boully,Maths -14,15,2,2024,1,7,Rapin,Maths -15,15,2,2024,1,6,Mann,Anglais -18,15,2,2024,1,7,Colin,Physique +15,14,2,2024,1,7,Rapin,Maths +16,14,2,2024,1,5,Mullaert,Maths +17,14,2,2024,1,1,Rapin,Maths +18,14,2,2024,1,1,Chevalier,Physique +14,15,2,2024,1,2,Belaggoune,Anglais +15,15,2,2024,1,3,Rapin,Maths +18,15,2,2024,1,4,Boully,Maths 16,16,2,2024,1,0,none,none 17,16,2,2024,1,0,none,none 18,16,2,2024,1,0,none,none -12,19,2,2024,1,4,Colin,Physique -13,19,2,2024,1,6,Poupy,Physique -18,19,2,2024,1,7,Belaggoune,Anglais -17,20,2,2024,1,4,Rapin,Maths -18,20,2,2024,1,0,none,none -14,21,2,2024,1,5,Mann,Anglais +12,19,2,2024,1,0,none,none +13,19,2,2024,1,7,Herbaud,Anglais +18,19,2,2024,1,0,none,none +17,20,2,2024,1,4,Mullaert,Maths +18,20,2,2024,1,6,Boully,Maths +14,21,2,2024,1,0,none,none 14,21,2,2024,2,2,Jospin,Info 14,21,2,2024,2,6,Jospin,Info 14,21,2,2024,2,0,none,none -15,21,2,2024,1,1,Le_Gourielec,Anglais -16,21,2,2024,1,2,Boully,Maths -17,21,2,2024,1,1,Rapin,Maths -18,21,2,2024,1,2,Colin,Physique -14,22,2,2024,1,5,Carpintero,Maths -15,22,2,2024,1,6,Rapin,Maths -18,22,2,2024,1,3,Mann,Anglais -16,23,2,2024,1,0,none,none +15,21,2,2024,1,1,Mullaert,Maths +16,21,2,2024,1,2,Chibani,Physique +17,21,2,2024,1,3,Belaggoune,Anglais +18,21,2,2024,1,5,Mann,Anglais +14,22,2,2024,1,4,Colin,Physique +15,22,2,2024,1,2,Mullaert,Maths +18,22,2,2024,1,1,Mann,Anglais +16,23,2,2024,1,5,Mullaert,Maths 17,23,2,2024,1,0,none,none 18,23,2,2024,1,0,none,none -12,26,2,2024,1,0,none,none -13,26,2,2024,1,7,Chibani,Physique -18,26,2,2024,1,5,Colin,Physique -17,27,2,2024,1,4,Mann,Anglais -18,27,2,2024,1,0,none,none -14,28,2,2024,1,5,Carpintero,Maths +12,26,2,2024,1,7,Chevalier,Physique +13,26,2,2024,1,1,Rapin,Maths +18,26,2,2024,1,2,Belaggoune,Anglais +17,27,2,2024,1,6,Rapin,Maths +18,27,2,2024,1,3,Boully,Maths +14,28,2,2024,1,3,Poupy,Physique 14,28,2,2024,2,1,Jospin,Info 14,28,2,2024,2,5,Jospin,Info 14,28,2,2024,2,0,none,none -15,28,2,2024,1,1,Poupy,Physique -16,28,2,2024,1,1,Rapin,Maths -17,28,2,2024,1,2,Le_Gourielec,Anglais -18,28,2,2024,1,2,Rapin,Maths -14,29,2,2024,1,3,Rapin,Maths -15,29,2,2024,1,6,Carpintero,Maths -18,29,2,2024,1,6,Belaggoune,Anglais -16,1,3,2024,1,7,Rapin,Maths +15,28,2,2024,1,2,Rapin,Maths +16,28,2,2024,1,4,Mann,Anglais +17,28,2,2024,1,5,Mullaert,Maths +18,28,2,2024,1,6,Mann,Anglais +14,29,2,2024,1,7,Carpintero,Maths +15,29,2,2024,1,5,Chevalier,Physique +18,29,2,2024,1,1,Chibani,Physique +16,1,3,2024,1,0,none,none 17,1,3,2024,1,0,none,none 18,1,3,2024,1,0,none,none