/*
* Colloscope - A program that generates a colloscope for French 'classes prépas'
* Copyright (C) 2024 Alexandre Aboujaib
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#include "logger.h"
#include "structure.h"
#include "algorithm.h"
bool is_equal_date(date d1, date d2) {
return (d1.hour == d2.hour && d1.day == d2.day && d2.month == d1.month /*&& d2.year == d1.year*/);
}
int get_date_index(creneau* edt, int len_creneau, date d) {
// could be done in log(n), I know
// yields the 1st occurence of d in edt
int x = -1;
for(int i = 0; i < len_creneau; i++) {
x = date_dist(d, edt[i].date);
if(x >= 0) {
return i;
}
}
return -1;
}
int get_next_friday(creneau* edt, int len_creneau, date d) {
// could be done in log(n), I know again
int x = -1;
for(int i = 0; i < len_creneau; i++) {
x = date_dist(d, edt[i].date);
if(x >= 0 && (i == len_creneau-1 || date_dist(d, edt[i+1].date) - x > 1)) {
return i;
}
}
return -1;
}
// typedef struct colleur {char* name; int namelen; topic mat; date* disp; int n_disp;} colleur;
colleur* get_colleurs(colleur* cl, int len_cl, date d, int* how_many) {
colleur* res = malloc(sizeof(colleur)*30); // max. 3 colles per creneau
int ptr = 0;
for(int i = 0; i < len_cl; i++) {
for(int j = 0; j < cl[i].n_disp; j++) {
if(is_equal_date(cl[i].disp[j], d)) {
if(ptr >= 30) {
error("Too many colleurs detected for a creneau\n");
}
res[ptr] = cl[i];
ptr++;
j = cl[i].n_disp;
}
}
}
*how_many = ptr;
return res;
}
void swap(int* arr, int i, int j) {
if(i != j) {
arr[i] += arr[j];
arr[j] = arr[i] - arr[j];
arr[i] -= arr[j];
}
}
void generate_random_perm(int* arr, int len) {
// generate a random perm of int between 0 and len-1
for(int i = 0; i < len; i++) {
arr[i] = i;
}
for(int i = 0; i < len; i++) {
swap(arr, i, rand()%len);
}
}
void print_arr(int* arr, int len) {
printf("[");
for(int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("]\n");
}
void add_colle(creneau* edt, colleur* chads, int grp, int id_edt, int id_chad) {
edt[id_edt].group = grp;
edt[id_edt].namelen = chads[id_chad].namelen;
str_copy(chads[id_chad].name, chads[id_chad].namelen, edt[id_edt].name);
edt[id_edt].mat = chads[id_chad].mat;
}
void remove_colle(creneau* edt, int id_edt) {
edt[id_edt].group = 0;
edt[id_edt].namelen = 0;
str_copy("none", 4, edt[id_edt].name);
edt[id_edt].mat = NOTHING;
}
void move_colle(creneau* edt, int len_edt, int id_src, int id_dest) {
if((id_src < len_edt && id_src >= 0 && id_dest < len_edt && id_dest >= 0) == false) {
printf("Bad\n");
exit(1);
}
edt[id_dest].group = edt[id_src].group;
edt[id_dest].namelen = edt[id_src].namelen;
str_copy(edt[id_src].name, edt[id_src].namelen, edt[id_dest].name);
edt[id_dest].mat = edt[id_src].mat;
remove_colle(edt, id_src);
}
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;
}
}
return -1;
}
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)) {
return true;
}
k++;
}
k = 1;
while(id-k >= 0 && is_equal_date(edt[id].date, edt[id-k].date)) {
if(str_equal(edt[id].name, edt[id-k].name)) {
return true;
}
k++;
}
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++;
}
k = 1;
while(id-k >= 0 && is_equal_date(edt[id].date, edt[id-k].date)) {
if(edt[id+k].group == grp) {
return true;
}
k++;
}
return false;
}
int free_math_space(creneau* edt, int len_edt, int id) {
int k = 1;
int howmany = 0;
while(id+k < len_edt && is_equal_date(edt[id].date, edt[id+k].date)) {
if(edt[id+k].mat == NOTHING || edt[id+k].mat == MATH) {
howmany++;
}
k++;
}
k = 1;
while(id-k >= 0 && is_equal_date(edt[id].date, edt[id-k].date)) {
if(edt[id-k].mat == NOTHING || edt[id-k].mat == MATH) {
howmany++;
}
k++;
}
return howmany;
}
static int* perm0 ;
static int perm0len ;
int colleur_to_id(colleur* colleurs, int len_colleurs, char* name) {
for(int i = 0; i < len_colleurs; i++) {
if(str_equal(name, colleurs[i].name)) {
return i;
}
}
fatal("colleur %d not found", name);
exit(1);
}
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, int* skip_count, int mat_halt, int info_app, int** occs, int* averages) {
topic rotation = start_rotation; // physics/english rotation
int math = mth; // math (3/4)
int info = inf; // info (1/6)
int r; // randomize the 1st date
int k = 0; // offset
int halt = 0; // timeout in case a colle cannot be placed
bool found; // interrupt in case a valid colle has been found
int remaining_to_add = 0; /* self-explainatory
please note that this also tells what colle to add :
1 = physics/english colle
2 = math
3 (not implemented) = info
*/
int len_dudes = 0; // length of colleur*
int len_perm = 0; // length of int*
int math_dude = 0; // length of colleur*
int weeklen;
bool is_searching_in_blacklisted = true ;
bool is_limiting_sames = true ;
bool normal_skip = true;
for(int week = 0; week < n_weeks; week++) {
weeklen = weeks_len[week];
// update what colles to add
if(mat_halt == 1 || math == 0) {
math = mat_halt-1;
} else {
remaining_to_add++;
math--;
}
if(rotation == ENGLISH) {
rotation = PHYSICS;
} else {
rotation = ENGLISH;
}
remaining_to_add++; // physics/english
// initialize/reset variables
r = rand()%weeklen;
halt = 0;
found = false;
normal_skip = true;
info++;
is_searching_in_blacklisted = true ;
is_limiting_sames = true ;
// info colle
while(info >= info_app) {
if(edt[k+r%weeklen].mat == NOTHING && edt[k+r%weeklen].length == 2 && (!is_searching_in_blacklisted || edt[k+r%weeklen].blacklisted == true)) {
// if creneau is empty
// import all colleurs available
colleur* dudes = get_colleurs(chads, len_chads, edt[k+r%weeklen].date, &len_dudes);
len_perm = len_dudes;
// if there are colleurs available
if(len_dudes != 0) {
// randomize the order of colleurs
int* perm = malloc(sizeof(int)*len_perm);
generate_random_perm(perm, len_perm);
// for each one of them, add his colle for selected group pf and only if
// - he is a INFO colleur
// if a colle has been addded, interrupt the for andwhile loops
for(int dude = 0; dude < len_perm*(1-found); dude++) {
if(dudes[perm[dude]].mat == INFO && (!is_limiting_sames || occs[grp-1][perm[dude]] <= averages[perm[dude]]) && edt[k+r%weeklen].allow_grps[grp-1] == true) {
add_colle(edt, dudes, grp, k+r%weeklen, perm[dude]);
occs[grp-1][colleur_to_id(chads, len_chads, dudes[perm[dude]].name)] += 1;
found = true;
info = 0;
} else if(edt[k+r%weeklen].allow_grps[grp-1] == true) {
normal_skip = false;
}
}
free(perm);
}
free(dudes);
}
if(!found && halt > 3*weeklen) {
info = 0;
*skip_count += (1-normal_skip);
//warn("skipping info colle for group %d (week %d)", grp, week);
} else {
if(halt > weeklen) {
is_searching_in_blacklisted = false ;
}
if(halt > 2*weeklen) {
is_limiting_sames = false ;
}
}
r++;
halt++;
}
// reset the variables
r = rand()%weeklen;
found = false;
halt = 0;
normal_skip = true;
is_searching_in_blacklisted = true ;
is_limiting_sames = true ;
// if there is a math colle to add, enter this loop
while(remaining_to_add == 2) {
if(edt[k+r%weeklen].mat == NOTHING && edt[k+r%weeklen].length == 1 && (!is_searching_in_blacklisted || edt[k+r%weeklen].blacklisted == true)) {
// if creneau is empty
// import all colleurs available
colleur* dudes = get_colleurs(chads, len_chads, edt[k+r%weeklen].date, &len_dudes);
len_perm = len_dudes;
// if there are colleurs available
if(len_dudes != 0) {
// randomize the order of colleurs
int* perm = malloc(sizeof(int)*len_perm);
generate_random_perm(perm, len_perm);
// for each one of them, add his colle for selected group pf and only if
// - he is a MATH colleur
// - he does not have another colle at the same time (is_overlap)
// if a colle has been addded, interrupt the for andwhile loops
for(int dude = 0; dude < len_perm*(1-found); dude++) {
if(dudes[perm[dude]].mat == MATH && (!is_limiting_sames || occs[grp-1][perm[dude]] <= averages[perm[dude]]) && edt[k+r%weeklen].allow_grps[grp-1] == true) {
add_colle(edt, dudes, grp, k+r%weeklen, perm[dude]);
if(is_overlap(edt, len_edt, k+r%weeklen)) {
remove_colle(edt, k+r%weeklen);
} else {
found = true;
remaining_to_add--;
occs[grp-1][colleur_to_id(chads, len_chads, dudes[perm[dude]].name)] += 1;
}
}
}
free(perm);
} else {
error("No colleurs available at (%d %d %d %d)", edt[k+r%weeklen].date.hour, edt[k+r%weeklen].date.day, edt[k+r%weeklen].date.month, edt[k+r%weeklen].date.year);
}
free(dudes);
}
if(!found && halt > 3*weeklen) {
remaining_to_add--;
*skip_count += 1;
//warn("skipping math colle for group %d (week %d)", grp, week);
} else {
if(halt > weeklen) {
is_searching_in_blacklisted = false ;
}
if(halt > 2*weeklen) {
is_limiting_sames = false ;
}
}
r++;
halt++;
}
// reset the variables
r = rand()%weeklen;
found = false;
halt = 0;
is_searching_in_blacklisted = true ;
is_limiting_sames = true ;
// do it again for physics/english colles
while(remaining_to_add == 1) {
normal_skip = true;
if(edt[k+r%weeklen].mat == NOTHING && edt[k+r%weeklen].length == 1 && (!is_searching_in_blacklisted || edt[k+r%weeklen].blacklisted == true)) {
colleur* dudes = get_colleurs(chads, len_chads, edt[k+r%weeklen].date, &len_dudes);
len_perm = len_dudes;
if(len_dudes != 0) {
int* perm = malloc(sizeof(int)*len_perm);
generate_random_perm(perm, len_perm);
math_dude = 0;
for(int dude = 0; dude < len_dudes; dude++) {
if(dudes[dude].mat == MATH) {
math_dude++;
}
}
for(int dude = 0; dude < len_perm*(1-found); dude++) {
if(dudes[perm[dude]].mat == rotation && (!is_limiting_sames || occs[grp-1][perm[dude]] <= averages[perm[dude]]) && edt[k+r%weeklen].allow_grps[grp-1] == true) {
add_colle(edt, dudes, grp, k+r%weeklen, perm[dude]);
if(math_dude > free_math_space(edt, len_edt, k+r%weeklen) || is_overlap(edt, len_edt, k+r%weeklen)) {
remove_colle(edt, k+r%weeklen);
} else {
found = true;
remaining_to_add--;
occs[grp-1][colleur_to_id(chads, len_chads, dudes[perm[dude]].name)] += 1;
}
} else if(edt[k+r%weeklen].allow_grps[grp-1] == true) {
normal_skip = false;
}
}
free(perm);
} else {
error("No colleurs available at (%d %d %d %d)", edt[k+r%weeklen].date.hour, edt[k+r%weeklen].date.day, edt[k+r%weeklen].date.month, edt[k+r%weeklen].date.year);
}
free(dudes);
}
if(!found && halt > 3*weeklen) {
remaining_to_add--;
*skip_count += 1;
//warn("skipping L2 colle for group %d (week %d after %d / %d spins)", grp, week, halt, weeklen);
} else {
if(halt > 2*weeklen) {
is_limiting_sames = false ;
}
if(halt > weeklen) {
is_searching_in_blacklisted = false ;
}
}
r++;
halt++;
}
k += weeks_len[week];
remaining_to_add = 0;
}
}
void write_to_file(char* filename, creneau* edt, int len_edt) {
FILE* ptr = fopen(filename, "w");
fprintf(ptr, "hour,day,month,year,length,group,colleur,matiere\n");
for(int i = 0; i < len_edt; i++) {
fprintf(ptr, "%d,%d,%d,%d,%d,%d,%s,", edt[i].date.hour, edt[i].date.day, edt[i].date.month, edt[i].date.year, edt[i].length, edt[i].group, edt[i].name);
if(edt[i].mat == NOTHING) {
fprintf(ptr, "none");
} else if(edt[i].mat == MATH) {
fprintf(ptr, "Maths");
} else if(edt[i].mat == PHYSICS) {
fprintf(ptr, "Physique");
} else if(edt[i].mat == ENGLISH) {
fprintf(ptr, "Anglais");
} else if(edt[i].mat == INFO) {
fprintf(ptr, "Info");
} else if(edt[i].mat == FRENCH) {
fprintf(ptr, "Français");
} else {
fprintf(ptr, "Unknown");
}
fprintf(ptr, "\n");
}
fclose(ptr);
}
int min(int x, int y) {
if(x < y) {
return x;
}
return y;
}
int max(int x, int y) {
if(x > y) {
return x;
}
return y;
}
int penalty_poly(int occ, int n_weeks) {
if(occ == 0) {
return n_weeks*10;
}
return (occ-1)*(occ-1)*(occ-1);
}
int score(creneau* edt, int len_edt, int grp, int n_weeks, int** occ, int wd, int ht) {
int score = n_weeks*300;
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 -= n_weeks*2;
}
if(dist == 7 && edt[i].date.hour == edt[j].date.hour) {
score -= n_weeks;
}
if(dist <= 12) {
score -= n_weeks;
}
if(dist < 5 && edt[i].date.hour == 18 && edt[j].date.hour == 18) {
score -= n_weeks;
}
}
}
}
for(int tch = 0; tch < ht; tch++) {
score -= penalty_poly(occ[grp-1][tch], n_weeks);
}
//printf("((%d))\n", score);
return score;
}
int get_colleur_id(colleur* dudes, int n_dudes, char* target) {
for(int i = 0; i < n_dudes; i++) {
if(str_equal(dudes[i].name, target)) {
return i;
}
}
return -1;
}
char* get_name_from_id(colleur* dudes, int n_dudes, int id) {
for(int i = 0; i < n_dudes; i++) {
if(id == dudes[i].id) {
return dudes[i].name;
}
}
return "none";
}
topic get_mat_from_id(colleur* dudes, int n_dudes, int id) {
for(int i = 0; i < n_dudes; i++) {
if(id == dudes[i].id) {
return dudes[i].mat;
}
}
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++) {
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++;
}
}
int j = 0;
for(int i = 0; i < ptr_1; i++) {
if(j >= ptr_2) {
free(indexes_1);
free(indexes_2);
return false;
} else {
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;
}
topic int_to_mat(colleur* colleur, int id) {
return colleur[id].mat ;
}
void fill_zeroes(creneau* edt, int len_edt, colleur* colleurs, int len_colleurs, int n_groups, int n_weeks, int weeklen, int** occs, int* averages) {
for(int gr1 = 0; gr1 < n_groups; gr1++) {
for(int col1 = 0; col1 < len_colleurs; col1++) {
char* researched_colleur = colleurs[col1].name ;
if(occs[gr1][col1] == 0) {
int halt = 0 ;
for(int week = 0; week < n_weeks*(1-halt); week++) {
int r1 = rand()%weeklen;
int count1 = 0 ;
while(!halt && count1 < weeklen) {
int edtptr1 = week*weeklen + (r1+count1)%weeklen;
if(edt[edtptr1].group == gr1 +1 && edt[edtptr1].mat == int_to_mat(colleurs, col1)) {
// search another
if(occs[gr1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] > 1) { // G1 has enough colles with proposed_colleur
int r2 = rand()%weeklen;
int count2 = 0 ;
while(!halt && count2 < weeklen) {
int edtptr2 = week*weeklen + (r2+count2)%weeklen;
if(
edt[edtptr2].group != gr1 +1 && // not the same group
edt[edtptr2].mat == int_to_mat(colleurs, col1) && // same topic
str_equal(edt[edtptr2].name, colleurs[col1].name) && // colleur with 0 G1 colles
occs[edt[edtptr2].group-1][col1] > 1 && // G2 has some backup
occs[edt[edtptr2].group-1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] <=
averages[colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] // no overflow for G2
) {
halt = true ;
occs[gr1][col1] += 1;
occs[gr1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] -= 1;
occs[edt[edtptr2].group-1][col1] -= 1;
occs[edt[edtptr2].group-1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] += 1;
edt[edtptr1].group = edt[edtptr2].group;
edt[edtptr2].group = gr1+1;
}
count2++;
}
}
}
count1++;
}
}
}
}
}
}
void deplete_most(creneau* edt, int len_edt, colleur* colleurs, int len_colleurs, int n_groups, int n_weeks, int weeklen, int** occs, int* averages) {
for(int gr1 = 0; gr1 < n_groups; gr1++) {
for(int col1 = 0; col1 < len_colleurs; col1++) {
char* researched_colleur = colleurs[col1].name ;
if(occs[gr1][col1] > averages[col1]) {
int halt = 0 ;
for(int week = 0; week < n_weeks*(1-halt); week++) {
int r1 = rand()%weeklen;
int count1 = 0 ;
while(!halt && count1 < weeklen) {
int edtptr1 = week*weeklen + (r1+count1)%weeklen;
if(edt[edtptr1].group == gr1 +1 && edt[edtptr1].mat == int_to_mat(colleurs, col1)) {
// search another
if(occs[gr1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] > 1) { // G1 has enough colles with proposed_colleur
int r2 = rand()%weeklen;
int count2 = 0 ;
while(!halt && count2 < weeklen) {
int edtptr2 = week*weeklen + (r2+count2)%weeklen;
if(
edt[edtptr2].group != gr1 +1 && // not the same group
edt[edtptr2].mat == int_to_mat(colleurs, col1) && // same topic
str_equal(edt[edtptr2].name, colleurs[col1].name) && // colleur with too many G1 colles
occs[edt[edtptr2].group-1][col1] <= averages[colleur_to_id(colleurs, len_colleurs, edt[edtptr2].name)] && // G2 has some backup
occs[edt[edtptr2].group-1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] > 1// no overflow for G2
) {
halt = true ;
occs[gr1][col1] -= 1;
occs[gr1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] += 1;
occs[edt[edtptr2].group-1][col1] += 1;
occs[edt[edtptr2].group-1][colleur_to_id(colleurs, len_colleurs, edt[edtptr1].name)] -= 1;
edt[edtptr1].group = edt[edtptr2].group;
edt[edtptr2].group = gr1+1;
}
count2++;
}
}
}
count1++;
}
}
}
}
}
}
int** occurencies(creneau* edt, int len_edt, colleur* dudes, int n_groups, int n_colleurs, int n_weeks, bool is_debug) {
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;
}
}
int* averages = malloc(sizeof(int)*n_colleurs);
for(int i = 0; i < n_colleurs; i++) {
averages[i] = 0;
for(int grp = 0; grp < n_groups; grp++) {
averages[i] += res[grp][i];
}
averages[i] = averages[i] / n_groups;
}
bool halt = false;
for(int it = 0; it < 5*(1-is_debug); it++) {
for(int grp = 0; grp < n_groups; grp++) {
for(int dud = 0; dud < n_colleurs; dud++) {
max_occ = max(1, averages[dud]);
if(res[grp][dud] > max_occ && get_mat_from_id(dudes, n_colleurs, dud) != INFO) {
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)) {;
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;
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;
if(is_overlap_creneau(edt, len_edt, id_src, grp2+1)) {
res[grp][dud] -= 1;
res[grp2][dud2] -= 1;
res[grp2][dud] += 1;
res[grp][dud2] += 1;
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;
} else if(is_overlap_creneau(edt, len_edt, id_dest, grp+1)) {
res[grp][dud] -= 1;
res[grp2][dud2] -= 1;
res[grp2][dud] += 1;
res[grp][dud2] += 1;
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;
} else {
halt = true;
dud--;
}
}
}
}
}
}
}
halt = false;
}
}
}
free(averages);
return res;
}
void copy_matrix(int** src, int lines, int col, int** dest) {
for(int i = 0; i < lines; i++) {
for(int j = 0; j < col; j++) {
dest[i][j] = src[i][j];
}
}
}
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 math_halt, int info_app) {
int start = time(NULL);
int* weeks_len = malloc(sizeof(int)*n_weeks);
for(int i = 0; i < n_weeks; i++) {
weeks_len[i] = len_edt/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;
int current = 1;
/*for(int k = 1; k < len_edt; k++) {
if(date_dist(edt[k-1].date, edt[k].date) > 1 || k == len_edt-1) {
weeks_len[ptr] = current + (k == len_edt - 1);
current = 0;
ptr++;
}
current++;
}*/
debug("---------------------- %d ----------------------", n_groups);
print_arr(weeks_len, n_weeks);
int* group_stats = malloc(sizeof(int)*n_groups);
int* group_temp = malloc(sizeof(int)*n_groups);
int max_score = -900000;
int global_min = 0;
int global_skipped = 0;
int screwed_group = 0;
int local_score = 0;
int local_min = 0;
int local_group = 0;
int temp = 0;
int skipped = 0;
int a = 0;
int* permut = malloc(sizeof(int)*n_groups);
int** occs = malloc(sizeof(int*)*n_groups);
for(int i = 0; i < n_groups; i++) {
occs[i] = malloc(sizeof(int)*len_chads);
}
int* averages = malloc(sizeof(int)*len_chads);
for(int i = 0; i < len_chads; i++) {
averages[i] = 1+(int)(((double)chads[i].n_disp)/((double)n_groups));
}
print_arr(averages, len_chads);
int** occ_data = generate_matrix(n_groups, len_chads, 0);;
info("Testing %d combinations...", n_sim);
printf("\n");
perm0 = malloc(sizeof(int)*20) ;
for(int k = 0; k < n_sim*(1 - (max_score == n_groups*100)); k++) {
generate_random_perm(permut, n_groups);
if(k >= a) {
printf("\x1b[1F");
printf("\x1b[2K");
info("%d%% Completed (current max is %d/%d)", 100*a/n_sim, max_score, n_weeks*300*n_groups);
a += n_sim/100;
}
local_score = 0;
local_min = 909090909;
skipped = 0;
for(int iii = 0; iii < n_groups; iii++) {
for(int jjj = 0; jjj < len_chads; jjj++) {
occs[iii][jjj] = 0;
}
}
for(int i = 0; i < n_groups; i++) {
////warn("(%d) at (%d)", permut[i]+1, k);
add_colles_for_group_MP2I(weeks_len, edt, len_edt, chads, len_chads, n_weeks, (permut[i])+1, (topic)(2+(permut[i])%2), (permut[i])%math_halt, (permut[i])%info_app, &skipped, math_halt, info_app, occs, averages);
}
//int** temp_data = occurencies(edt, len_edt, chads, n_groups, len_chads, n_weeks, false);
fill_zeroes(edt, len_edt, chads, len_chads, n_groups, n_weeks, len_edt/n_weeks, occs, averages);
deplete_most(edt, len_edt, chads, len_chads, n_groups, n_weeks, len_edt/n_weeks, occs, averages);
deplete_most(edt, len_edt, chads, len_chads, n_groups, n_weeks, len_edt/n_weeks, occs, averages);
deplete_most(edt, len_edt, chads, len_chads, n_groups, n_weeks, len_edt/n_weeks, occs, averages);
deplete_most(edt, len_edt, chads, len_chads, n_groups, n_weeks, len_edt/n_weeks, occs, averages);
int** temp_data = occs;
//int** temp_data = adjust(edt, len_edt, chads, n_groups, len_chads, n_weeks);
for(int i = 0; i < n_groups; i++) {
temp = score(edt, len_edt, i+1, n_weeks, temp_data, n_groups, len_chads);
local_score += temp;
group_temp[i] = temp;
if(local_min > temp) {
local_min = temp;
local_group = i+1;
}
}
local_score -= skipped*200;
if(local_score > max_score) {
max_score = local_score;
screwed_group = local_group;
global_min = local_min;
global_skipped = skipped;
for(int p = 0; p < n_groups; p++) {
group_stats[p] = group_temp[p];
}
copy_matrix(temp_data, n_groups, len_chads, occ_data);
write_to_file(outname, edt, len_edt);
}
for(int r = 0; r < len_edt; r++) {
remove_colle(edt, r);
}
//destroy_matrix(temp_data, n_groups);
}
printf("\x1b[1F");
printf("\x1b[2K");
if(max_score == n_weeks*300*n_groups) {
info("Interrupting early due to a perfect score achieved");
} else {
info("100%% Completed, best score is %d/%d (without skip penalty) with %d skipped colle(s)", max_score+global_skipped*30, n_groups*n_weeks*300, global_skipped);
info("Most screwed group is %d with a score of %d/%d", screwed_group, global_min, n_weeks*300);
debug("Stats for all groups are :");
for(int i = 0; i < n_groups; i++) {
debug("Group %d : %d/%d (or %lf %%)", i+1, group_stats[i], n_weeks*300, ((double)group_stats[i]/(n_weeks*3.0)));
}
}
int end = time(NULL);
printf("\n");
info("Here is all data regarding colles occurencies :");
for(int k = 0; k < n_groups; k++) {
printf("Group %d : ", (k+1)%10);
print_arr(occ_data[k], len_chads);
}
info("Took %ds to find", end-start);
free(permut);
free(averages);
free(perm0);
free(group_stats);
free(group_temp);
free(weeks_len);
destroy_matrix(occ_data, n_groups);
info("Done.");
}