[122] | 1 | #include <stdio.h> |
---|
| 2 | #include "Genetic.h" |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | /* Gene class */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Default constructor. |
---|
| 9 | */ |
---|
| 10 | Gene::Gene(){ |
---|
| 11 | task_index = -1; |
---|
| 12 | proc_index = -1; |
---|
| 13 | level = -1; |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Constructor |
---|
| 18 | * |
---|
| 19 | * @param task the index of the task |
---|
| 20 | * @param proc the index of the processor |
---|
| 21 | * @param lev the topological level of the task |
---|
| 22 | */ |
---|
| 23 | Gene::Gene(int task,int proc,int lev){ |
---|
| 24 | task_index = task; |
---|
| 25 | proc_index = proc; |
---|
| 26 | level = lev; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Copy operator |
---|
| 31 | */ |
---|
| 32 | Gene& Gene::operator=(const Gene& clone){ |
---|
| 33 | task_index = clone.task_index; |
---|
| 34 | proc_index = clone.proc_index; |
---|
| 35 | level = clone.level; |
---|
| 36 | return *this; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Method printing the gene's configuration |
---|
| 41 | */ |
---|
| 42 | void Gene::print(){ |
---|
| 43 | printf("(%d, %d, %d)", task_index, proc_index, level); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * the default constructor |
---|
| 48 | */ |
---|
| 49 | /* Chromosome class */ |
---|
| 50 | Chromosome::Chromosome(){ |
---|
| 51 | makespan=0; |
---|
| 52 | fitness=0; |
---|
| 53 | evalT1=0; |
---|
| 54 | evalT2=0; |
---|
| 55 | evalT3=0; |
---|
| 56 | loadBalance=0; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Copy operator |
---|
| 61 | */ |
---|
| 62 | Chromosome& Chromosome::operator=(const Chromosome& clone){ |
---|
| 63 | makespan=clone.makespan; |
---|
| 64 | fitness=clone.fitness; |
---|
| 65 | evalT1=clone.evalT1; |
---|
| 66 | evalT2=clone.evalT2; |
---|
| 67 | evalT3=clone.evalT3; |
---|
| 68 | loadBalance=clone.loadBalance; |
---|
| 69 | genes=clone.genes; |
---|
| 70 | f_nodes=clone.f_nodes; |
---|
| 71 | return *this; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Iterator operator |
---|
| 76 | */ |
---|
| 77 | Gene& Chromosome::operator[](int poz){ |
---|
| 78 | if(poz >= 0 && poz <(int)genes.size()){ |
---|
| 79 | return genes[poz]; |
---|
| 80 | } |
---|
| 81 | return null_gene; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Method that adds a gene to the genes list |
---|
| 86 | * |
---|
| 87 | * @param g the new gene |
---|
| 88 | */ |
---|
| 89 | void Chromosome::append(Gene &g){ |
---|
| 90 | genes.push_back(g); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Method that returns the size of the genes list |
---|
| 95 | * |
---|
| 96 | * @return the size |
---|
| 97 | */ |
---|
| 98 | int Chromosome::size(void){ |
---|
| 99 | return genes.size(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Method that initializes the population |
---|
| 104 | */ |
---|
| 105 | void Chromosome::init(void){ |
---|
| 106 | fitness = 0; |
---|
| 107 | makespan = 0; |
---|
| 108 | loadBalance = 0; |
---|
| 109 | evalT1 = 0; |
---|
| 110 | evalT2 = 0; |
---|
| 111 | evalT3 = 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Method that inserts a gene to the genes list in the specified position |
---|
| 116 | * |
---|
| 117 | * @param g the new gene |
---|
| 118 | * @param poz the position in the list |
---|
| 119 | */ |
---|
| 120 | void Chromosome::insert(Gene g,int poz){ |
---|
| 121 | genes.insert(genes.begin()+poz, g); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Method that removes a gene from the genes list |
---|
| 126 | * |
---|
| 127 | * @param poz the gene's position |
---|
| 128 | * @return the removed gene |
---|
| 129 | */ |
---|
| 130 | Gene Chromosome::remove(int poz){ |
---|
| 131 | Gene g = genes[poz]; |
---|
| 132 | genes.erase(genes.begin()+poz); |
---|
| 133 | return g; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Method that prints the chromosome's configuration |
---|
| 138 | */ |
---|
| 139 | void Chromosome::print() const{ |
---|
| 140 | printf("%f %f %lf %lf %lf %lf\n", makespan, fitness, evalT1, evalT2, evalT3, loadBalance); |
---|
| 141 | printf("[%i]:", genes.size()); |
---|
| 142 | for (unsigned int i=0;i<genes.size();i++) |
---|
| 143 | printf("(%d %d %d)",genes[i].task_index, genes[i].proc_index,genes[i].level); |
---|
| 144 | printf("\n[%i]:", f_nodes.size()); |
---|
| 145 | for (unsigned int i=0;i<f_nodes.size();i++) |
---|
| 146 | printf("%d ",f_nodes[i]); |
---|
| 147 | printf("\n"); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|