Exemplo n.º 1
0
        /** Function for computing the potentials of the nodes and the edges in
         * the graph.
         */
        void computePotentials()
        {
            // Method steps:
            //  1. Compute node potentials
            //  2. Compute edge potentials

            //
            //  1. Node potentials
            //

            std::vector<CNodePtr>::iterator it;

            //cout << "NODE POTENTIALS" << endl;

            for ( it = m_nodes.begin(); it != m_nodes.end(); it++ )
            {
                CNodePtr nodePtr = *it;

                if ( !nodePtr->finalPotentials() )
                {
                    // Get the node type
                    //size_t type = nodePtr->getType()->getID();

                    // Compute the node potentials according to the node type and its
                    // extracted features

                    Eigen::VectorXd potentials = nodePtr->getType()->computePotentials( nodePtr->getFeatures() );

                    // Apply the node class multipliers
                    potentials = potentials.cwiseProduct( nodePtr->getClassMultipliers() );

                    /*Eigen::VectorXd fixed = nodePtr->getFixed();

                    potentials = potentials.cwiseProduct( fixed );*/

                    nodePtr->setPotentials( potentials );
                }

            }

            //
            //  2. Edge potentials
            //

            std::vector<CEdgePtr>::iterator it2;

            //cout << "EDGE POTENTIALS" << endl;

            for ( it2 = m_edges.begin(); it2 != m_edges.end(); it2++ )
            {
                CEdgePtr edgePtr = *it2;

                Eigen::MatrixXd potentials
                        = edgePtr->getType()->computePotentials( edgePtr->getFeatures() );

                edgePtr->setPotentials ( potentials );
            }

        }
Exemplo n.º 2
0
void UPGMpp::applyMaskToPotentials(CGraph &graph, map<size_t,vector<size_t> > &mask )
{
    vector<CNodePtr> &nodes = graph.getNodes();

    for ( size_t node_index = 0; node_index < nodes.size(); node_index++ )
    {
        CNodePtr nodePtr    = nodes[node_index];
        size_t nodeID       = nodePtr->getID();

        if ( mask.count(nodeID) )
        {
            Eigen::VectorXd nodePot = nodePtr->getPotentials();
            Eigen::VectorXd potMask( nodePot.rows() );
            potMask.fill(0);

            for ( size_t mask_index = 0; mask_index < mask[nodeID].size(); mask_index++ )
                potMask(mask[nodeID][mask_index]) = 1;

            nodePot = nodePot.cwiseProduct(potMask);

            nodePtr->setPotentials( nodePot );
        }
    }
}
Exemplo n.º 3
0
        /** Method for getting a bound graph from the one stored into the object.
          * \param boundGraph: resulting graph, it must be empty when the method
          * is called.
          * \param nodesToBound: a map containing as key the id of a node, and
          * as value the class/state to be bound.
          */
        void getBoundGraph( CGraph &boundGraph,
                            std::map<size_t,size_t> nodesToBound )
        {
            // Check that the graph is empty
            assert( boundGraph.getNodes().size() == 0 );

            // Copy the graph into boundGraph

            // Copy nodes
            for ( size_t node = 0; node < m_nodes.size(); node++ )
            {
                CNodePtr nodePtr(new CNode(*m_nodes[node]));
                boundGraph.addNode( nodePtr );
            }

            // Copy edges
            for ( size_t edge = 0; edge < m_edges.size(); edge++ )
            {
                CEdgePtr edgePtr( new CEdge(*m_edges[edge]));
                boundGraph.addEdge( edgePtr );
            }

            // Okey, now iterate over the nodes to be bound, removing then from
            // the resulting graph by expanding the potential associated with
            // their bound state
            for ( std::map<size_t,size_t>::iterator it = nodesToBound.begin();
                  it != nodesToBound.end();
                  it++ )
            {
                size_t nodeID = it->first;
                size_t nodeState = it->second;

                // Potential of the state to be bounded
                double nodePotential = boundGraph.getNodeWithID( nodeID )->getPotentials()( nodeState );

                // Iterate over the neighbours, updating their node potentials
                // according to the state of the node to be bound and its potential

                pair<multimap<size_t,CEdgePtr>::iterator,multimap<size_t,CEdgePtr>::iterator > neighbors;

                neighbors = boundGraph.getEdgesF().equal_range(nodeID);

                for ( multimap<size_t,CEdgePtr>::iterator it = neighbors.first;
                      it != neighbors.second;
                      it++ )
                {
                    CEdgePtr edgePtr( (*it).second );
                    Eigen::MatrixXd edgePotentials = edgePtr->getPotentials();

                    size_t ID1, ID2;
                    edgePtr->getNodesID(ID1,ID2);

                    // If the node to be bound is the first one appearing in the
                    // edge.
                    if ( ID1 == nodeID )
                    {
                        CNodePtr nodePtr                = boundGraph.getNodeWithID( ID2 );
                        Eigen::VectorXd boundPotentials = edgePotentials.row(nodeState).transpose()*nodePotential;
                        Eigen::VectorXd newPotentials   = nodePtr->getPotentials().cwiseProduct(boundPotentials);
                        nodePtr->setPotentials( newPotentials );
                    }
                    else    // If it is the second one in the edge
                    {
                        CNodePtr nodePtr                = boundGraph.getNodeWithID( ID1 );
                        Eigen::VectorXd boundPotentials = edgePotentials.col( nodeState )*nodePotential;
                        Eigen::VectorXd newPotentials   = nodePtr->getPotentials().cwiseProduct(boundPotentials);
                        nodePtr->setPotentials( newPotentials );
                    }
                }

                // Now that the potential of the state of the node to bound
                // has been expanded, delete the node from the graph.
                boundGraph.deleteNode( nodeID );
            }
        }