int treeOptimizeThorough(tree *tr, int mintrav, int maxtrav) { int i; bestlist *bestT; nodeRectifier(tr); bestT = (bestlist *) malloc(sizeof(bestlist)); bestT->ninit = 0; initBestTree(bestT, 1, tr->mxtips); if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; tr->startLH = tr->endLH = tr->likelihood; for(i = 1; i <= tr->mxtips + tr->mxtips - 2; i++) { tr->bestOfNode = unlikely; if(rearrangeBIG(tr, tr->nodep[i], mintrav, maxtrav)) { if((tr->endLH > tr->startLH) && (tr->bestOfNode != unlikely)) { restoreTreeFast(tr); quickSmoothLocal(tr, 3); tr->startLH = tr->endLH = tr->likelihood; } else { if(tr->bestOfNode != unlikely) { resetBestTree(bestT); saveBestTree(bestT, tr); restoreTreeFast(tr); quickSmoothLocal(tr, 3); if(tr->likelihood < tr->startLH) { int res; res = recallBestTree(bestT, 1, tr); assert(res > 0); } else tr->startLH = tr->endLH = tr->likelihood; } } } } freeBestTree(bestT); free(bestT); return 1; }
double treeOptimizeRapid(tree *tr, int mintrav, int maxtrav, analdef *adef, bestlist *bt) { int i, index, *perm = (int*)NULL; nodeRectifier(tr); if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; resetInfoList(); resetBestTree(bt); tr->startLH = tr->endLH = tr->likelihood; if(tr->doCutoff) { if(tr->bigCutoff) { if(tr->itCount == 0) tr->lhCutoff = 0.5 * (tr->likelihood / -1000.0); else tr->lhCutoff = 0.5 * ((tr->lhAVG) / ((double)(tr->lhDEC))); } else { if(tr->itCount == 0) tr->lhCutoff = tr->likelihood / -1000.0; else tr->lhCutoff = (tr->lhAVG) / ((double)(tr->lhDEC)); } tr->itCount = tr->itCount + 1; tr->lhAVG = 0; tr->lhDEC = 0; } if(adef->permuteTreeoptimize) { int n = tr->mxtips + tr->mxtips - 2; perm = (int *)rax_malloc(sizeof(int) * (n + 1)); makePermutation(perm, n, adef); } for(i = 1; i <= tr->mxtips + tr->mxtips - 2; i++) { tr->bestOfNode = unlikely; if(adef->permuteTreeoptimize) index = perm[i]; else index = i; if(rearrangeBIG(tr, tr->nodep[index], mintrav, maxtrav)) { if(Thorough) { if(tr->endLH > tr->startLH) { restoreTreeFast(tr); tr->startLH = tr->endLH = tr->likelihood; saveBestTree(bt, tr); } else { if(tr->bestOfNode != unlikely) restoreTopologyOnly(tr, bt); } } else { insertInfoList(tr->nodep[index], tr->bestOfNode); if(tr->endLH > tr->startLH) { restoreTreeFast(tr); tr->startLH = tr->endLH = tr->likelihood; } } } } if(!Thorough) { Thorough = 1; for(i = 0; i < iList.valid; i++) { tr->bestOfNode = unlikely; if(rearrangeBIG(tr, iList.list[i].node, mintrav, maxtrav)) { if(tr->endLH > tr->startLH) { restoreTreeFast(tr); tr->startLH = tr->endLH = tr->likelihood; saveBestTree(bt, tr); } else { if(tr->bestOfNode != unlikely) { restoreTopologyOnly(tr, bt); } } } } Thorough = 0; } if(adef->permuteTreeoptimize) rax_free(perm); return tr->startLH; }
int determineRearrangementSetting(tree *tr, analdef *adef, bestlist *bestT, bestlist *bt) { int i, mintrav, maxtrav, bestTrav, impr, index, MaxFast, *perm = (int*)NULL; double startLH; boolean cutoff; MaxFast = 26; startLH = tr->likelihood; cutoff = tr->doCutoff; tr->doCutoff = FALSE; mintrav = 1; maxtrav = 5; bestTrav = maxtrav = 5; impr = 1; resetBestTree(bt); if(adef->permuteTreeoptimize) { int n = tr->mxtips + tr->mxtips - 2; perm = (int *)rax_malloc(sizeof(int) * (n + 1)); makePermutation(perm, n, adef); } while(impr && maxtrav < MaxFast) { recallBestTree(bestT, 1, tr); nodeRectifier(tr); if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; tr->startLH = tr->endLH = tr->likelihood; for(i = 1; i <= tr->mxtips + tr->mxtips - 2; i++) { if(adef->permuteTreeoptimize) index = perm[i]; else index = i; tr->bestOfNode = unlikely; if(rearrangeBIG(tr, tr->nodep[index], mintrav, maxtrav)) { if(tr->endLH > tr->startLH) { restoreTreeFast(tr); tr->startLH = tr->endLH = tr->likelihood; } } } treeEvaluate(tr, 0.25); saveBestTree(bt, tr); /*printf("DETERMINE_BEST: %d %f\n", maxtrav, tr->likelihood);*/ if(tr->likelihood > startLH) { startLH = tr->likelihood; printLog(tr, adef, FALSE); bestTrav = maxtrav; impr = 1; } else { impr = 0; } maxtrav += 5; if(tr->doCutoff) { tr->lhCutoff = (tr->lhAVG) / ((double)(tr->lhDEC)); tr->itCount = tr->itCount + 1; tr->lhAVG = 0; tr->lhDEC = 0; } } recallBestTree(bt, 1, tr); tr->doCutoff = cutoff; if(adef->permuteTreeoptimize) rax_free(perm); return bestTrav; }