|
| 1 | +/************************************************************************************************************************* |
| 2 | + * DESCRIPTION: This function generates the taxa addition order using max mini algorithm. * |
| 3 | + * * |
| 4 | + * INPUTS: None * |
| 5 | + * * |
| 6 | + * OUTPUTS: Queue containing the addition order of taxa, writes directly into global queue. Doesnt return anything * |
| 7 | + ************************************************************************************************************************/ |
| 8 | +#include "header.h" |
| 9 | + |
| 10 | +void taxaAddOrder(void) { |
| 11 | + int taxaOver = 3; |
| 12 | + struct subTask *sourceTree, *childTree; |
| 13 | + struct maxMiniTaxaPosWLength mMTPWL; |
| 14 | + struct taxaQ *t; |
| 15 | + |
| 16 | + sourceTree = genIstSubTask(); |
| 17 | + /* enqueue all taxa to be checked for addition order */ |
| 18 | + /* this will be from 4 to NUMBER of TAXA */ |
| 19 | + enqueueTaxa(); |
| 20 | + /* apply maxmini, & enqueue first addition order taxa */ |
| 21 | + mMTPWL = maxMini(sourceTree); |
| 22 | + enqueueAddOrdTaxa(mMTPWL.taxa); |
| 23 | + taxaOver += 1; |
| 24 | + /* for all taxa remaining to be enqueued in addition order */ |
| 25 | + while (taxaOver < matrix.num_taxa) { |
| 26 | +#ifdef TAXA_ADD_ORDER_VERBOSE |
| 27 | + printf("\t\t\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~\n"); |
| 28 | + printf("\t\t\t\t\t\tADDING NEXT TAXA ORDER\n"); |
| 29 | + printf("\t\t\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~"); |
| 30 | +#endif |
| 31 | + /* generate next task with previous maxmini taxa, position */ |
| 32 | + childTree = genNextTask(sourceTree, childTree, mMTPWL.taxa, mMTPWL.pos); |
| 33 | + /* free parent task, it is not needed anymode */ |
| 34 | + freeTree(sourceTree); |
| 35 | + sourceTree = childTree; |
| 36 | +#ifdef TAXA_ADD_ORDER_VERBOSE |
| 37 | + printTree(sourceTree,mMTPWL.pos); |
| 38 | +#endif |
| 39 | + /* apply maxmini, & enqueue next addition order taxa */ |
| 40 | + mMTPWL = maxMini(sourceTree); |
| 41 | + enqueueAddOrdTaxa(mMTPWL.taxa); |
| 42 | + taxaOver += 1; |
| 43 | + } |
| 44 | + /* if the final tree cost is less than best cost computed */ |
| 45 | + /* by SG algorithm, set this as best cost */ |
| 46 | + if (mMTPWL.length < bestCost) { |
| 47 | + bestCost = mMTPWL.length; |
| 48 | + flushBestCostStack(); |
| 49 | + childTree = genNextTask(sourceTree, childTree, mMTPWL.taxa, mMTPWL.pos); |
| 50 | + freeTree(sourceTree); |
| 51 | + pushBestCostNode(childTree); |
| 52 | + } |
| 53 | + else { |
| 54 | + freeTree(sourceTree); |
| 55 | + } |
| 56 | + |
| 57 | + //printf("\t\t\t[BEST SCORE %d]\n",bestCost+pNiCost); |
| 58 | + //fprintf(fp,"\t\t\t[BEST SCORE %d]\n",bestCost+pNiCost); |
| 59 | + |
| 60 | + //printf("\tTaxa addition order: 1 2 3 "); |
| 61 | + //fprintf(fp,"\tTaxa addition order: 1 2 3 "); |
| 62 | + taxaQueue[0] = 1; |
| 63 | + taxaQueue[1] = 2; |
| 64 | + taxaQueue[2] = 3; |
| 65 | + for (taxaOver=3, t=tAddOrdQHead; t != NULL; t = t->next, taxaOver=taxaOver+1) { |
| 66 | + taxaQueue[taxaOver] = t->taxa; |
| 67 | + //printf("%d ",t->taxa); |
| 68 | + //fprintf(fp,"%d ",t->taxa); |
| 69 | + } |
| 70 | + //printf("\n"); |
| 71 | + //fprintf(fp,"\n"); |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +/************************************************************************************************************************* |
| 76 | + * DESCRIPTION: This function implements maxmini algorithm * |
| 77 | + * * |
| 78 | + * INPUTS: Sub task on which to apply maxmini algorithm * |
| 79 | + * * |
| 80 | + * OUTPUTS: structure containing the taxon to be added next, position where it is to be added and tree lenght * |
| 81 | + ************************************************************************************************************************/ |
| 82 | +struct maxMiniTaxaPosWLength maxMini(struct subTask *sourceTree) { |
| 83 | + struct maxMiniTaxaPosWLength prevmMTPWL, currmMTPWL; |
| 84 | + struct taxaQ *tQNode; |
| 85 | + |
| 86 | + int taxaNum; |
| 87 | + |
| 88 | + prevmMTPWL.length = 0; |
| 89 | + prevmMTPWL.taxa = 0; |
| 90 | + prevmMTPWL.pos = 0; |
| 91 | + currmMTPWL.length = 0; |
| 92 | + currmMTPWL.taxa = 0; |
| 93 | + currmMTPWL.pos = 0; |
| 94 | + |
| 95 | +#ifdef MAX_MINI_VERBOSE |
| 96 | + printf("\n\t\t*************************************************************************************************\n"); |
| 97 | + printf("\n\t\t\t\t\t\t\tMAXMINI FUNCTION\n"); |
| 98 | + printf("\n\t\t*************************************************************************************************\n"); |
| 99 | +#endif |
| 100 | + /* for all taxa remaining to be added */ |
| 101 | + for (tQNode = taxaQHead; tQNode != NULL; tQNode=tQNode->next) { |
| 102 | + taxaNum = tQNode->taxa; |
| 103 | + /* get tree with minimum length with this taxa added to the */ |
| 104 | + /* subtask under view (check for all positions) */ |
| 105 | +#ifdef MAXMINI |
| 106 | + currmMTPWL = getMinTree(sourceTree, taxaNum); |
| 107 | +#endif |
| 108 | +#ifdef MAXMAX |
| 109 | + currmMTPWL = getMaxTree(sourceTree, taxaNum); |
| 110 | +#endif |
| 111 | +#ifdef MAX_MINI_VERBOSE |
| 112 | + printf("\n\t\t->->->->->->RETAIN MAXIMUM LENGTH OF ALL MINIMUM LENGTHS<-<-<-<-<-<-\n"); |
| 113 | + printf("\t\tCurr {MinLen:%3d @ Pos:%3d w/ Taxa:%3d} vs. Prev {MinLen:%3d @ Pos:%3d w/ Taxa:%3d}\n",currmMTPWL.length, currmMTPWL.pos, currmMTPWL.taxa, prevmMTPWL.length, prevmMTPWL.pos, prevmMTPWL.taxa); |
| 114 | +#endif |
| 115 | + /* this length > previous length, this is maximum, keep it */ |
| 116 | + if (currmMTPWL.length > prevmMTPWL.length) { |
| 117 | +#ifdef MAX_MINI_VERBOSE |
| 118 | + printf("\t\tCurr Min Lgt (%d) > Prev Min Lgt (%d)=> ",currmMTPWL.length,prevmMTPWL.length); |
| 119 | +#endif |
| 120 | + prevmMTPWL.pos = currmMTPWL.pos; |
| 121 | + prevmMTPWL.taxa = currmMTPWL.taxa; |
| 122 | + prevmMTPWL.length = currmMTPWL.length; |
| 123 | +#ifdef MAX_MINI_VERBOSE |
| 124 | + printf("Update Curr Max Len to {MaxLen:%3d @ Pos:%3d w/ Taxa:%3d}\n", prevmMTPWL.length, prevmMTPWL.pos, prevmMTPWL.taxa); |
| 125 | +#endif |
| 126 | + } |
| 127 | + } |
| 128 | + /* remove taxa added to taxa addition order */ |
| 129 | + deleteTaxa(prevmMTPWL.taxa); |
| 130 | + |
| 131 | + return (prevmMTPWL); |
| 132 | +} |
| 133 | + |
| 134 | +/************************************************************************************************************************** |
| 135 | + * DESCRIPTION: This function returns the tree with minimum length, with the position where a given taxa should be added * |
| 136 | + * This checks the given taxa at all positions and returns the minimum tree. If there is a tie, it just picks* |
| 137 | + * up the first one and ignores the rest * |
| 138 | + * * |
| 139 | + * INPUTS: Sub task on which to apply maxmini algorithm, and taxa which is to be added * |
| 140 | + * * |
| 141 | + * OUTPUTS: Structure containing the taxon to be added next, position where it is to be added and tree length * |
| 142 | + *************************************************************************************************************************/ |
| 143 | +struct maxMiniTaxaPosWLength getMinTree(struct subTask *sourceTree, int taxaNum) { |
| 144 | + struct subTask *childTree; |
| 145 | + int currentLength = 0; |
| 146 | + int prevLength = 0; |
| 147 | + |
| 148 | + struct maxMiniTaxaPosWLength mMTPWL; |
| 149 | + |
| 150 | + int pos; |
| 151 | + int currMinPos; |
| 152 | + int currMinTaxa; |
| 153 | + int arraySize = sourceTree->sizeOfSubTaskArray; |
| 154 | + |
| 155 | +#ifdef GET_MIN_TREE_VERBOSE |
| 156 | + printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); |
| 157 | + printf("\t\t\t\t CHECKING MINIMUM TREE LENGTH FOR ALL POSITIONS FOR TAXA: %d\n",taxaNum); |
| 158 | + printf("\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); |
| 159 | +#endif |
| 160 | + /* generate first case, adding taxa in position 0 */ |
| 161 | + childTree = genNextTask(sourceTree, childTree, taxaNum, 0); |
| 162 | +#ifdef GET_MIN_TREE_VERBOSE |
| 163 | + printTree(childTree, 0); |
| 164 | +#endif |
| 165 | + /* update initial tree length to the length in position 0 */ |
| 166 | + prevLength = childTree->costOfSubTask; |
| 167 | + freeTree(childTree); |
| 168 | + |
| 169 | + currMinPos = 0; |
| 170 | + currMinTaxa = taxaNum; |
| 171 | + |
| 172 | + /* for all remaining positions */ |
| 173 | + for (pos=1; pos < arraySize; pos++) { |
| 174 | + /* do not try position before internal node -1 */ |
| 175 | + /* this is done to ensure that the integrity of tree */ |
| 176 | + if (sourceTree->node[pos].id != -1) { |
| 177 | + childTree = genNextTask(sourceTree,childTree, taxaNum, pos); |
| 178 | +#ifdef GET_MIN_TREE_VERBOSE |
| 179 | + printTree(childTree, pos); |
| 180 | +#endif |
| 181 | + currentLength = childTree->costOfSubTask; |
| 182 | +#ifdef GET_MIN_TREE_VERBOSE |
| 183 | + printf("\n\t\t->->->->->->RETAIN MINIMUM LENGTH FROM CURRENT & PREVIOUS LENGTHS<-<-<-<-<-<-\n"); |
| 184 | + printf("\n\t\t\tCurr {MinLen:%3d @ Pos:%3d w/ Taxa:%3d} vs. Prev {MinLen:%3d @ Pos:%3d w/ Taxa:%3d}\n\n",currentLength, pos, taxaNum, prevLength, currMinPos, currMinTaxa); |
| 185 | +#endif |
| 186 | + /* if this length is less than previous length, keep it */ |
| 187 | + if (currentLength < prevLength) { |
| 188 | +#ifdef GET_MIN_TREE_VERBOSE |
| 189 | + printf("\t\tCurr Lgt (%d) < Prev Lgt (%d)=> ",currentLength,prevLength); |
| 190 | +#endif |
| 191 | + prevLength = currentLength; |
| 192 | + currMinPos = pos; |
| 193 | + currMinTaxa = taxaNum; |
| 194 | +#ifdef GET_MIN_TREE_VERBOSE |
| 195 | + printf("Update Curr Lgt to: %d & keeping Taxa: %d, Pos: %d\n\n",currentLength,currMinTaxa,currMinPos); |
| 196 | +#endif |
| 197 | + } |
| 198 | + |
| 199 | + freeTree(childTree); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /* return position & tree with minimum length */ |
| 204 | + mMTPWL.taxa = currMinTaxa; |
| 205 | + mMTPWL.pos = currMinPos; |
| 206 | + mMTPWL.length = prevLength; |
| 207 | + |
| 208 | + return (mMTPWL); |
| 209 | +} |
| 210 | + |
| 211 | +/************************************************************************************************************************** |
| 212 | + * DESCRIPTION: This function returns the tree with maximum length, with the position where a given taxa should be added * |
| 213 | + * This checks the given taxa at all positions and returns the maximum tree. If there is a tie, it just picks* |
| 214 | + * up the first one and ignores the rest * |
| 215 | + * * |
| 216 | + * INPUTS: Sub task on which to apply maxmax algorithm, and taxa which is to be added * |
| 217 | + * * |
| 218 | + * OUTPUTS: Structure containing the taxon to be added next, position where it is to be added and tree length * |
| 219 | + *************************************************************************************************************************/ |
| 220 | +struct maxMiniTaxaPosWLength getMaxTree(struct subTask *sourceTree, int taxaNum) { |
| 221 | + struct subTask *childTree; |
| 222 | + int currentLength = 0; |
| 223 | + int prevLength = 0; |
| 224 | + |
| 225 | + struct maxMiniTaxaPosWLength mMTPWL; |
| 226 | + |
| 227 | + int pos; |
| 228 | + int currMinPos; |
| 229 | + int currMinTaxa; |
| 230 | + int arraySize = sourceTree->sizeOfSubTaskArray; |
| 231 | + |
| 232 | +#ifdef GET_MIN_TREE_VERBOSE |
| 233 | + printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); |
| 234 | + printf("\t\t\t\t CHECKING MINIMUM TREE LENGTH FOR ALL POSITIONS FOR TAXA: %d\n",taxaNum); |
| 235 | + printf("\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); |
| 236 | +#endif |
| 237 | + /* generate first case, adding taxa in position 0 */ |
| 238 | + childTree = genNextTask(sourceTree, childTree, taxaNum, 0); |
| 239 | +#ifdef GET_MIN_TREE_VERBOSE |
| 240 | + printTree(childTree, 0); |
| 241 | +#endif |
| 242 | + /* update initial tree length to the length in position 0 */ |
| 243 | + prevLength = childTree->costOfSubTask; |
| 244 | + freeTree(childTree); |
| 245 | + |
| 246 | + currMinPos = 0; |
| 247 | + currMinTaxa = taxaNum; |
| 248 | + |
| 249 | + /* for all remaining positions */ |
| 250 | + for (pos=1; pos < arraySize; pos++) { |
| 251 | + /* do not try position before internal node -1 */ |
| 252 | + /* this is done to ensure that the integrity of tree */ |
| 253 | + if (sourceTree->node[pos].id != -1) { |
| 254 | + childTree = genNextTask(sourceTree,childTree, taxaNum, pos); |
| 255 | +#ifdef GET_MIN_TREE_VERBOSE |
| 256 | + printTree(childTree, pos); |
| 257 | +#endif |
| 258 | + currentLength = childTree->costOfSubTask; |
| 259 | +#ifdef GET_MIN_TREE_VERBOSE |
| 260 | + printf("\n\t\t->->->->->->RETAIN MAXIMUM LENGTH FROM CURRENT & PREVIOUS LENGTHS<-<-<-<-<-<-\n"); |
| 261 | + printf("\n\t\t\tCurr {MinLen:%3d @ Pos:%3d w/ Taxa:%3d} vs. Prev {MinLen:%3d @ Pos:%3d w/ Taxa:%3d}\n\n",currentLength, pos, taxaNum, prevLength, currMinPos, currMinTaxa); |
| 262 | +#endif |
| 263 | + /* if this length is more than previous length, keep it */ |
| 264 | + if (currentLength > prevLength) { |
| 265 | +#ifdef GET_MIN_TREE_VERBOSE |
| 266 | + printf("\t\tCurr Lgt (%d) > Prev Lgt (%d)=> ",currentLength,prevLength); |
| 267 | +#endif |
| 268 | + prevLength = currentLength; |
| 269 | + currMinPos = pos; |
| 270 | + currMinTaxa = taxaNum; |
| 271 | +#ifdef GET_MIN_TREE_VERBOSE |
| 272 | + printf("Update Curr Lgt to: %d & keeping Taxa: %d, Pos: %d\n\n",currentLength,currMinTaxa,currMinPos); |
| 273 | +#endif |
| 274 | + } |
| 275 | + |
| 276 | + freeTree(childTree); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + /* return position & tree with minimum length */ |
| 281 | + mMTPWL.taxa = currMinTaxa; |
| 282 | + mMTPWL.pos = currMinPos; |
| 283 | + mMTPWL.length = prevLength; |
| 284 | + |
| 285 | + return (mMTPWL); |
| 286 | +} |
0 commit comments