void HillClimber::initializeSampler( void ) { std::vector<DagNode *>& dagNodes = model->getDagNodes(); std::vector<DagNode *> orderedStochNodes = model->getOrderedStochasticNodes( ); // Get rid of previous move schedule, if any if ( schedule ) { delete schedule; } schedule = NULL; // Get initial lnProbability of model // first we touch all nodes so that the likelihood is dirty for (std::vector<DagNode *>::iterator i=dagNodes.begin(); i!=dagNodes.end(); i++) { DagNode *the_node = *i; the_node->setMcmcMode( true ); the_node->setPriorOnly( false ); the_node->touch(); } int numTries = 0; int maxNumTries = 100; double lnProbability = 0.0; for ( ; numTries < maxNumTries; numTries ++ ) { // a flag if we failed to find a valid starting value bool failed = false; lnProbability = 0.0; for (std::vector<DagNode *>::iterator i=dagNodes.begin(); i!=dagNodes.end(); i++) { DagNode* node = (*i); node->touch(); double lnProb = node->getLnProbability(); if ( !RbMath::isAComputableNumber(lnProb) ) { std::stringstream ss; ss << "Could not compute lnProb for node " << node->getName() << "." << std::endl; node->printValue( ss ); ss << std::endl; RBOUT( ss.str() ); // set the flag failed = true; break; } lnProbability += lnProb; } // now we keep all nodes so that the likelihood is stored for (std::vector<DagNode *>::iterator i=dagNodes.begin(); i!=dagNodes.end(); i++) { (*i)->keep(); } if ( failed == true ) { std::cout << "Drawing new initial states ... " << std::endl; for (std::vector<DagNode *>::iterator i=orderedStochNodes.begin(); i!=orderedStochNodes.end(); i++) { if ( !(*i)->isClamped() && (*i)->isStochastic() ) { (*i)->redraw(); (*i)->reInitialized(); } else if ( (*i)->isClamped() ) { // make sure that the clamped node also recompute their probabilities (*i)->reInitialized(); (*i)->touch(); } } } else { break; } } if ( numTries == maxNumTries ) { std::stringstream msg; msg << "Unable to find a starting state with computable probability"; if ( numTries > 1 ) { msg << " after " << numTries << " tries"; } throw RbException( msg.str() ); } // Create the move scheduler if ( scheduleType == "sequential" ) { schedule = new SequentialMoveSchedule( &moves ); } else if ( scheduleType == "single" ) { schedule = new SingleRandomMoveSchedule( &moves ); } else { schedule = new RandomMoveSchedule( &moves ); } generation = 0; }
/** Monitor value at generation gen */ void ScreenMonitor::monitor(unsigned long gen) { // get the printing frequency unsigned long samplingFrequency = printgen; if (gen % samplingFrequency == 0) { // print the iteration number first std::cout << gen; if ( posterior ) { // add a separator before every new element std::cout << separator; const std::vector<DagNode*> &n = model->getDagNodes(); double pp = 0.0; for (std::vector<DagNode*>::const_iterator it = n.begin(); it != n.end(); ++it) { pp += (*it)->getLnProbability(); } std::cout << pp; } if ( likelihood ) { // add a separator before every new element std::cout << separator; const std::vector<DagNode*> &n = model->getDagNodes(); double pp = 0.0; for (std::vector<DagNode*>::const_iterator it = n.begin(); it != n.end(); ++it) { if ( (*it)->isClamped() ) { pp += (*it)->getLnProbability(); } } std::cout << pp; } if ( prior ) { // add a separator before every new element std::cout << separator; const std::vector<DagNode*> &n = model->getDagNodes(); double pp = 0.0; for (std::vector<DagNode*>::const_iterator it = n.begin(); it != n.end(); ++it) { if ( !(*it)->isClamped() ) { pp += (*it)->getLnProbability(); } } std::cout << pp; } for (std::vector<DagNode*>::iterator i = nodes.begin(); i != nodes.end(); ++i) { // add a separator before every new element std::cout << separator; // get the node DagNode *node = *i; // print the value node->printValue(std::cout, separator); } std::cout << std::endl; } }