[122] | 1 | #include "Graf.h" |
---|
| 2 | #include "Genetic.h" |
---|
| 3 | #include "Generator.h" |
---|
| 4 | #include <vector> |
---|
| 5 | #include <time.h> |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * The constructor |
---|
| 12 | * |
---|
| 13 | * @param no_ch the number of chromosomes |
---|
| 14 | * @param t the graph of tasks |
---|
| 15 | * @param nr_procs the number of processors |
---|
| 16 | */ |
---|
| 17 | Generator::Generator(int no_ch, TaskGraf t, int nr_procs) |
---|
| 18 | { |
---|
| 19 | no_chromosomes = no_ch; |
---|
| 20 | no_procs = nr_procs; |
---|
| 21 | tasks = t; |
---|
| 22 | |
---|
| 23 | ch_list = (Chromosome * )calloc(no_ch, sizeof(Chromosome)); |
---|
| 24 | returned = 0; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Method returning the next chromosome from the list |
---|
| 29 | * |
---|
| 30 | * @return the next chromosome |
---|
| 31 | */ |
---|
| 32 | Chromosome Generator::getNext() |
---|
| 33 | { |
---|
| 34 | if(returned >= no_chromosomes) |
---|
| 35 | return Chromosome(); |
---|
| 36 | else |
---|
| 37 | return ch_list[returned++]; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Setter for the list of floating nodes |
---|
| 42 | * |
---|
| 43 | * @param the list of floating nodes |
---|
| 44 | */ |
---|
| 45 | void Generator::setFloatingNodes(vector<int> &float_n){ |
---|
| 46 | f_nodes = float_n; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Method generating the chromosomes list |
---|
| 51 | * |
---|
| 52 | * @return void |
---|
| 53 | */ |
---|
| 54 | void Generator::generate() |
---|
| 55 | { |
---|
| 56 | tasks.sortare_topologica(); |
---|
| 57 | tasks.sortare_topologica_inversa(); |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | vector <vector <int> > levels; |
---|
| 61 | int level; |
---|
| 62 | for(int i = 0 ; i <= tasks.max_level ; i ++) |
---|
| 63 | levels.push_back(vector<int>()); |
---|
| 64 | for(int i = 0 ; i < tasks.nr_noduri; i ++) |
---|
| 65 | { |
---|
| 66 | level = tasks.noduri[i].niv_topo_min; |
---|
| 67 | levels[level].push_back(tasks.noduri[i].id); |
---|
| 68 | } |
---|
| 69 | /* |
---|
| 70 | for(int i = 1 ; i < max ; i ++) |
---|
| 71 | { |
---|
| 72 | printf("Level %i: ", i); |
---|
| 73 | for(unsigned int j = 0 ; j < levels[i].size() ; j ++) |
---|
| 74 | printf("%i ", tasks.noduri[levels[i][j]].id); |
---|
| 75 | printf("\n"); |
---|
| 76 | } |
---|
| 77 | */ |
---|
| 78 | // srand(time(NULL)); |
---|
| 79 | |
---|
| 80 | int id_proc; |
---|
| 81 | //for each chromosome |
---|
| 82 | for(int i = 0 ; i < no_chromosomes ; i ++) |
---|
| 83 | { |
---|
| 84 | // printf("Chromosome %i\n",i); |
---|
| 85 | //number of set genes |
---|
| 86 | //int set = 0 ; |
---|
| 87 | //for each topological level |
---|
| 88 | ch_list[i].f_nodes = f_nodes; |
---|
| 89 | for(int j = 1 ; j < tasks.max_level ; j ++) |
---|
| 90 | { |
---|
| 91 | // printf("\tNiv topo %i\n", j); |
---|
| 92 | //the number of tasks with the specific topological level |
---|
| 93 | int nr = levels[j].size(); |
---|
| 94 | //for each task |
---|
| 95 | int * uz = (int *)calloc(nr, sizeof(int)); |
---|
| 96 | // printf("\tnr tasks pe level %i\n", nr); |
---|
| 97 | for(int k = 0 ; k < nr ; k ++) |
---|
| 98 | { |
---|
| 99 | |
---|
| 100 | // printf("\trun for tasks no %i\n", k); |
---|
| 101 | //generate the procesor id |
---|
| 102 | id_proc = rand() % no_procs; |
---|
| 103 | // printf("\t\tid proc = %i\n", id_proc); |
---|
| 104 | |
---|
| 105 | int id = rand() % (nr-k); |
---|
| 106 | // printf("\t\tid random = %i\n", id); |
---|
| 107 | int g = 0; |
---|
| 108 | for(int l = 0 ; l < nr ;l++) |
---|
| 109 | { |
---|
| 110 | // printf("\t\tl = %i, g = %i\n",l, g); |
---|
| 111 | // printf("uz "); |
---|
| 112 | // for(int m = 0 ; m < nr ; m ++) |
---|
| 113 | // printf("%i ", uz[m]); |
---|
| 114 | // printf("\n"); |
---|
| 115 | if(uz[l] == 0) |
---|
| 116 | { |
---|
| 117 | if(g == id) |
---|
| 118 | { |
---|
| 119 | uz[l] = 1; |
---|
| 120 | //store into the chromosome |
---|
| 121 | // printf("\t\tgene %i %i %i\n", levels[j][l], id_proc, j); |
---|
| 122 | Gene g = Gene(levels[j][l], id_proc, j); |
---|
| 123 | ch_list[i].append(g); |
---|
| 124 | l = nr; |
---|
| 125 | |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | g++; |
---|
| 129 | } |
---|
| 130 | //else |
---|
| 131 | // g++; |
---|
| 132 | // int t; |
---|
| 133 | // scanf("%i", &t); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | } |
---|