Пример #1
0
 /**
  * Receive all messages and compute the new belief. 
  */
 inline void update_belief(const vertex_type& vertex,
                           StateManager& state) {
   
   // Get the belief from the state manager
   belief_type* blf = state.checkout_belief(vertex);
   // Wipe out the old value for the belief
   if(vertex.is_variable()) {
     *blf = 1;
   } else if(vertex.is_factor()) {
     *blf = vertex.factor();
   } else {
     assert(false);
   }
   // For each of the neighbor variables 
   foreach(const vertex_type& vertex_source, state.neighbors(vertex)) {
     // get the in message
     message_type* in_msg = 
       state.try_checkout(vertex_source, vertex, Reading);
     if(in_msg != NULL) {
       // Combine the in_msg with the destination factor
       blf->combine_in(*in_msg, csr_.dot_op);
       // return the message to the state manager
       state.checkin(vertex_source, vertex, in_msg);
       // normalize the belief
       blf->normalize();
     }
   }  
   // Do an extra normalization (just in case no messages were
   // available)
   blf->normalize();
   // ASSERT WE BLF IS A VALID DISTRIBUTION (we should check this)
   // Save the belief
   state.checkin_belief(vertex, blf);
 }// End of update belief    
Пример #2
0
    /**
     * Receive all messages into the factor f and compute all new
     * outbound messages.
     */
    void send_messages(const vertex_type& vertex,
                              StateManager& state) {
      // Get the belief from the state manager
      belief_type* blf;
      // Wipe out the old value for the belief
      if(vertex.is_variable()) {
        blf = state.checkout_belief(vertex);
        (*blf) = 1;
        blf->normalize();
//        std::cout << "var\n";
      } else if(vertex.is_factor()) {
        blf = new belief_type();
        (*blf) = vertex.factor();
        blf->normalize();
//        std::cout << "fact\n";
      } else {
        assert(false);
      }
//      std::cout << blf->arguments()<<"\n";
      std::vector<vertex_type> neighbors;
      std::vector<message_type*> neighbor_inmsg;
      // For each of the neighbor variables 
//      std::cout << (*blf);
      foreach(const vertex_type& vertex_source, state.neighbors(vertex)) {
        // get the in message
        message_type* in_msg = 
          state.checkout(vertex_source, vertex, Reading);
          // remember the messages and which neighbor it came from
          neighbors.push_back(vertex_source);
          neighbor_inmsg.push_back(in_msg);
          // Combine the in_msg with the destination factor
          if (vertex.is_variable()) {
            assert(in_msg->arguments().size() == 1);
            assert(in_msg->arguments().contains(&(vertex.variable())));
          }
          else if (vertex.is_factor()) {
            assert(blf->arguments().contains(&(vertex_source.variable())));
          }
          blf->combine_in(*in_msg, csr_.dot_op);
//          std::cout << "Include: " << (*in_msg);
          // normalize the belief
          blf->normalize();
//          std::cout << (*blf);
      }  
      //std::cout<< "------------------------------------";
      // compute the outgoing messages
      message_type oldmessage;
      for (size_t i = 0; i< neighbors.size(); ++i) {
        message_type* out_msg = state.checkout(vertex, neighbors[i], Writing);
        // divide out the incoming message here
        oldmessage = (*out_msg);
//        std::cout << "Exclude: " << (*neighbor_inmsg[i]);
        if (neighbors[i].is_variable()) {
          (*out_msg) = blf->collapse(csr_.cross_op, make_domain(&neighbors[i].variable()));
          out_msg->combine_in(*(neighbor_inmsg[i]), divides_op);
        }
        else {
          (*out_msg) = combine(*blf, *(neighbor_inmsg[i]), divides_op);
        }
        
        out_msg->normalize();
//        std::cout<<*out_msg;
        // Save  the damped message to the destination message
        // lets only damp the factor to variable updates
        // otherwise we will 'doubly damp' the messages in a pairwise MRF
        if(neighbors[i].is_variable()) {
          (*out_msg) = weighted_update(*out_msg, oldmessage, damping_);
        }
        // checkin the final message to the state manager
        state.checkin(neighbors[i], vertex, neighbor_inmsg[i]);
        state.checkin(vertex, neighbors[i], out_msg);
      }
//      getchar();
      // lets checkin the belief while we are at it
      if (vertex.is_variable()) state.checkin_belief(vertex,blf);
      else delete blf;
      state.mark_visited(vertex);
    }