#include "Graf.h" #include "Genetic.h" #include "Generator.h" #include #include #include #include /** * The constructor * * @param no_ch the number of chromosomes * @param t the graph of tasks * @param nr_procs the number of processors */ Generator::Generator(int no_ch, TaskGraf t, int nr_procs) { no_chromosomes = no_ch; no_procs = nr_procs; tasks = t; ch_list = (Chromosome * )calloc(no_ch, sizeof(Chromosome)); returned = 0; } /** * Method returning the next chromosome from the list * * @return the next chromosome */ Chromosome Generator::getNext() { if(returned >= no_chromosomes) return Chromosome(); else return ch_list[returned++]; } /** * Setter for the list of floating nodes * * @param the list of floating nodes */ void Generator::setFloatingNodes(vector &float_n){ f_nodes = float_n; } /** * Method generating the chromosomes list * * @return void */ void Generator::generate() { tasks.sortare_topologica(); tasks.sortare_topologica_inversa(); vector > levels; int level; for(int i = 0 ; i <= tasks.max_level ; i ++) levels.push_back(vector()); for(int i = 0 ; i < tasks.nr_noduri; i ++) { level = tasks.noduri[i].niv_topo_min; levels[level].push_back(tasks.noduri[i].id); } /* for(int i = 1 ; i < max ; i ++) { printf("Level %i: ", i); for(unsigned int j = 0 ; j < levels[i].size() ; j ++) printf("%i ", tasks.noduri[levels[i][j]].id); printf("\n"); } */ // srand(time(NULL)); int id_proc; //for each chromosome for(int i = 0 ; i < no_chromosomes ; i ++) { // printf("Chromosome %i\n",i); //number of set genes //int set = 0 ; //for each topological level ch_list[i].f_nodes = f_nodes; for(int j = 1 ; j < tasks.max_level ; j ++) { // printf("\tNiv topo %i\n", j); //the number of tasks with the specific topological level int nr = levels[j].size(); //for each task int * uz = (int *)calloc(nr, sizeof(int)); // printf("\tnr tasks pe level %i\n", nr); for(int k = 0 ; k < nr ; k ++) { // printf("\trun for tasks no %i\n", k); //generate the procesor id id_proc = rand() % no_procs; // printf("\t\tid proc = %i\n", id_proc); int id = rand() % (nr-k); // printf("\t\tid random = %i\n", id); int g = 0; for(int l = 0 ; l < nr ;l++) { // printf("\t\tl = %i, g = %i\n",l, g); // printf("uz "); // for(int m = 0 ; m < nr ; m ++) // printf("%i ", uz[m]); // printf("\n"); if(uz[l] == 0) { if(g == id) { uz[l] = 1; //store into the chromosome // printf("\t\tgene %i %i %i\n", levels[j][l], id_proc, j); Gene g = Gene(levels[j][l], id_proc, j); ch_list[i].append(g); l = nr; } else g++; } //else // g++; // int t; // scanf("%i", &t); } } } } }