Exemple #1
0
void IPFP::computeMarginal( Message &out, unsigned int spec_depth ){
	
	const FragmentGraph *fg = moldata->getFragmentGraph();
	out.reset(fg->getNumFragments());

	Message *msg, tmp_msg(1);
	if( spec_depth > 0 ) msg = &(down_msgs[spec_depth-1]);
	else{	//Spectrum at depth 1 means there is no down message,
			//so only iterate over the 0 index
		tmp_msg.addToIdx(0, 0.0);
		msg = &tmp_msg;
	}
	
	Message::const_iterator it = msg->begin();
	for( ; it != msg->end(); ++it ){
		unsigned int idx = it.index();

		//Persistence term
		out.addToIdx(idx, beliefs.ps[idx][spec_depth]);

		//Transition terms
		std::vector<int>::const_iterator itt = (*fg->getFromIdTMap())[idx].begin();
		for( ; itt != (*fg->getFromIdTMap())[idx].end(); ++itt ){
			const Transition *t = fg->getTransitionAtIdx( *itt );
			out.addToIdx( t->getToId(), beliefs.tn[*itt][spec_depth] );
		}
	}
}
bool XBee_Message::append_msg(const GBeeRxPacket *msg) {
	XBee_Message tmp_msg(msg);
	return append_msg(tmp_msg);
}
Exemple #3
0
// collect status packets for 0.5 seconds.  Keep the last packet sent
// from each seperate power device.
int collect_messages(void) {
    PowerMessage *tmp_msg(NULL);

    timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000*100; //Collect packets for 1/10th of a second


    while (1) {
        if (tmp_msg == NULL)
            tmp_msg = new PowerMessage;
        assert(tmp_msg);

        // Wait for packets to arrive on socket.
        fd_set read_set;
        int max_sock = -1;
        FD_ZERO(&read_set);
        for (unsigned i = 0; i<Interfaces.size(); ++i)
            Interfaces[i]->AddToReadSet(read_set,max_sock);

        int result = select(max_sock+1, &read_set, NULL, NULL, &timeout);

        //fprintf(stderr,"*");

        if (result == -1) {
            perror("Select");
            return -1;
        }
        else if (result == 0) {
            // timeout
            return 0;
        }
        else if (result >= 1) {
            Interface *recvInterface = NULL;
            for (unsigned i = 0; i<Interfaces.size(); ++i) {
                //figure out which interface we recieved on
                if (Interfaces[i]->IsReadSet(read_set)) {
                    recvInterface = Interfaces[i];
                    break;
                }
            }

            int len = recv(recvInterface->recv_sock, tmp_msg, sizeof(*tmp_msg), 0);
            if (len == -1) {
                perror("Error recieving on socket");
                return -1;
            }
            else if (len != sizeof(*tmp_msg)) {
                fprintf(stderr, "recieved message of incorrect size %d\n", len);
            }
            else {
                if (process_message(tmp_msg, recvInterface)) {
                    return -1;
                }
            }
        }
        else {
            fprintf(stderr,"Unexpected select result %d\n", result);
            return -1;
        }
    }

    if (tmp_msg)
        delete tmp_msg;

    return 0;
}
Exemple #4
0
void IPFP::applyEvidenceAndPropagate( unsigned int spec_depth, Message &desired_marginals, Message &actual_marginals){

	const FragmentGraph *fg = moldata->getFragmentGraph(); 

	//Set the iterator for updating the factor
	Message *msg, tmp_msg(1);
	if( spec_depth > 0 ) msg = &(down_msgs[spec_depth-1]);
	else{	//Spectrum at depth 1 means there is no down message,
			//so only iterate over the 0 index
		tmp_msg.addToIdx(0, 0.0);
		msg = &tmp_msg;
	}
	
	//Update the affected factor	
	Message::const_iterator it = msg->begin();
	for( ; it != msg->end(); ++it ){
		
		unsigned int idx = it.index();

		//Persistence
		double tmp = beliefs.ps[idx][spec_depth];
		tmp += desired_marginals.getIdx(idx);
		tmp -= actual_marginals.getIdx(idx);
		tmp -= *it;	//down msg
		if( spec_depth < cfg->model_depth-1 )
			tmp -= up_msgs[spec_depth].getIdx(idx);
		fprobs.ps[idx][spec_depth] = tmp;

		//Transitions
		std::vector<int>::const_iterator itt = (*fg->getFromIdTMap())[idx].begin();
		for( ; itt != (*fg->getFromIdTMap())[idx].end(); ++itt ){

			const Transition *t = fg->getTransitionAtIdx(*itt);
			double tmp = beliefs.tn[*itt][spec_depth];
			tmp += desired_marginals.getIdx(t->getToId());
			tmp -= actual_marginals.getIdx(t->getToId());
			if( spec_depth > 0 )		
				tmp -= down_msgs[spec_depth-1].getIdx(t->getFromId());
			if( spec_depth < cfg->model_depth-1 )
				tmp -= up_msgs[spec_depth].getIdx(t->getToId());
			fprobs.tn[*itt][spec_depth] = tmp;
		}

	}

	//Propagate up to the top (update outgoing messages)
	for( int d = spec_depth-1; d >= 0; d-- ){
		it = down_msgs[d].begin();
		up_msgs[d].reset( fg->getNumFragments() );
		for( ; it != down_msgs[d].end(); ++it ){

			unsigned int idx = it.index();

			//Persistence
			if( (unsigned int)d < cfg->model_depth-2 ){
				double up_val = up_msgs[d+1].getIdx(idx);
				up_msgs[d].addToIdx(idx, fprobs.ps[idx][d+1] + up_val);
			}else up_msgs[d].addToIdx(idx, fprobs.ps[idx][d+1]);

			//Transitions
			std::vector<int>::const_iterator itt = (*fg->getFromIdTMap())[idx].begin();
			for( ; itt != (*fg->getFromIdTMap())[idx].end(); ++itt ){
				const Transition *t = fg->getTransitionAtIdx( *itt );
				double tmp;
				if( (unsigned int)d < cfg->model_depth-2 ){
					double up_val = up_msgs[d+1].getIdx(t->getToId());
					tmp = fprobs.tn[*itt][d+1] + up_val;
				}else tmp = fprobs.tn[*itt][d+1];
				up_msgs[d].addToIdx(idx, tmp);
			}
		}
	}

	//Propagate down to the bottom (update outgoing messages)
	for( unsigned int d = spec_depth; d < cfg->model_depth-1; d++ ){
		
		//Use a copy of the old down msg to get the used_idxs before resetting it
		Message old_down_msg = down_msgs[d];
		down_msgs[d].reset( fg->getNumFragments() );
		for( it = old_down_msg.begin(); it != old_down_msg.end(); ++it ){

			unsigned int idx = it.index();

			//Persistence
			if( d > 0 ){
				double down_val = down_msgs[d-1].getIdx(idx);
				down_msgs[d].addToIdx(idx, fprobs.ps[idx][d] + down_val);
			}else if( idx == 0 ) 
				down_msgs[d].addToIdx(idx, fprobs.ps[idx][d]);

			//Transitions
			std::vector<int>::const_iterator itt = (*fg->getToIdTMap())[idx].begin();
			for( ; itt != (*fg->getToIdTMap())[idx].end(); ++itt ){
				const Transition *t = fg->getTransitionAtIdx( *itt );
				if( d > 0 ){
					double down_val = down_msgs[d-1].getIdx(t->getFromId());
					down_msgs[d].addToIdx(idx, fprobs.tn[*itt][d] + down_val);
				}else if( t->getFromId() == 0 ) 
					down_msgs[d].addToIdx(idx, fprobs.tn[*itt][d]);
			} 
		}
	}
}