/**
 * Overloaded assignment operator.
 * We need a deep copy of the operator.
 */
MetropolisHastingsMove& MetropolisHastingsMove::operator=(const RevBayesCore::MetropolisHastingsMove &m)
{

    if ( this != &m )
    {
        // free memory
        delete proposal;

        for (std::set<DagNode*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
        {
            // get the pointer to the current node
            DagNode* theNode = *it;

            // add myself to the set of moves
            theNode->removeMove( this );

            // decrease the DAG node reference count because we also have a pointer to it
            if ( theNode->decrementReferenceCount() == 0 )
            {
                delete theNode;
            }

        }

        affectedNodes   = m.affectedNodes;
        nodes           = m.nodes;
        numAccepted     = m.numAccepted;
        proposal        = m.proposal->clone();


        for (std::set<DagNode*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
        {
            // get the pointer to the current node
            DagNode* theNode = *it;

            // add myself to the set of moves
            theNode->addMove( this );

            // increase the DAG node reference count because we also have a pointer to it
            theNode->incrementReferenceCount();

        }
    }

    return *this;
}
Пример #2
0
/**
 * Overloaded assignment operator.
 * We need a deep copy of the operator.
 */
AbstractMove& AbstractMove::operator=(const RevBayesCore::AbstractMove &m)
{
    
    if ( this != &m )
    {
        // delegate
        Move::operator=(m);
        
        for (size_t i = 0; i < nodes.size(); ++i)
        {
            // get the pointer to the current node
            DagNode* theNode = nodes[i];
            
            // add myself to the set of moves
            theNode->removeMove( this );
            
            // decrease the DAG node reference count because we also have a pointer to it
            if ( theNode->decrementReferenceCount() == 0 )
            {
                delete theNode;
            }
            
        }
        
        affectedNodes   = m.affectedNodes;
        nodes           = m.nodes;
        
        
        for (size_t i = 0; i < nodes.size(); ++i)
        {
            // get the pointer to the current node
            DagNode* theNode = nodes[i];
            
            // add myself to the set of moves
            theNode->addMove( this );
            
            // increase the DAG node reference count because we also have a pointer to it
            theNode->incrementReferenceCount();
            
        }
        
    }
    
    return *this;
}
Пример #3
0
/**
 * Constructor
 *
 * Here we simply allocate and initialize the move object.
 *
 * \param[in]    w   The weight how often the proposal will be used (per iteration).
 * \param[in]    t   If auto tuning should be used.
 */
AbstractMove::AbstractMove( const std::vector<DagNode*> &n, double w, bool t ) :
    nodes( n ),
    affectedNodes( ),
    weight( w ),
    autoTuning( t )
{
    
    for (std::vector<DagNode*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
    {
        // get the pointer to the current node
        DagNode* theNode = *it;
        
        // add myself to the set of moves
        theNode->addMove( this );
        
        // increase the DAG node reference count because we also have a pointer to it
        theNode->incrementReferenceCount();
        
    }
    
    
    // remove all "core" nodes from affectedNodes so their probabilities are not double-counted
    for (size_t i = 0; i < affectedNodes.size(); ++i)
    {
        RbOrderedSet<DagNode*>::iterator it = affectedNodes.begin();
        std::advance(it, i);
        
        for (size_t j = 0; j < nodes.size(); ++j)
        {
            if ( nodes[j] == *it )
            {
                affectedNodes.erase(*it);
                --i;
                break;
            }
            
        }
        
    }
    
}
/**
 * Copy constructor.
 * We need to create a deep copy of the proposal here.
 *
 * \param[in]   m   The object to copy.
 *
 */
MetropolisHastingsMove::MetropolisHastingsMove(const MetropolisHastingsMove &m) : AbstractMove(m),
    affectedNodes( m.affectedNodes ),
    nodes( m.nodes ),
    numAccepted( m.numAccepted ),
    proposal( m.proposal->clone() )
{

    for (std::set<DagNode*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
    {
        // get the pointer to the current node
        DagNode* theNode = *it;

        // add myself to the set of moves
        theNode->addMove( this );

        // increase the DAG node reference count because we also have a pointer to it
        theNode->incrementReferenceCount();

    }

}
Пример #5
0
AbstractMove::AbstractMove( const AbstractMove &m ) : Move( m ),
    nodes( m.nodes ),
    affectedNodes( m.affectedNodes ),
    weight( m.weight ),
    autoTuning( m.autoTuning  )
{
    
    
    for (size_t i = 0; i < nodes.size(); ++i)
    {
        // get the pointer to the current node
        DagNode* theNode = nodes[i];
        
        // add myself to the set of moves
        theNode->addMove( this );
        
        // increase the DAG node reference count because we also have a pointer to it
        theNode->incrementReferenceCount();
        
    }
    
}
/**
 * Constructor
 *
 * Here we simply allocate and initialize the move object.
 *
 * \param[in]    w   The weight how often the proposal will be used (per iteration).
 * \param[in]    t   If auto tuning should be used.
 */
MetropolisHastingsMove::MetropolisHastingsMove( Proposal *p, double w, bool t ) : AbstractMove(w,t),
    affectedNodes(),
    nodes(),
    numAccepted( 0 ),
    proposal( p )
{
    nodes = proposal->getNodes();

    for (std::set<DagNode*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
    {
        // get the pointer to the current node
        DagNode* theNode = *it;

        // add myself to the set of moves
        theNode->addMove( this );

        // increase the DAG node reference count because we also have a pointer to it
        theNode->incrementReferenceCount();

        // get the affected nodes if we would update this node
        // then we don't need to get the affected nodes every time again
        theNode->getAffectedNodes( affectedNodes );
    }

    // remove all "core" nodes from affectedNodes so their probabilities are not double-counted
    for (size_t i = 0; i < affectedNodes.size(); ++i)
    {
        std::set<DagNode*>::iterator it = affectedNodes.begin();
        std::advance(it, i);

        if ( nodes.find(*it) != nodes.end() )
        {
            affectedNodes.erase(*it);
            --i;
        }
    }
}