Exemple #1
0
//computing p(xn.....xN,zn)
void ocv::DHMM::backwardMatrix(vector<int> &sequence)
{
    _beta=MatrixXf(_nstates,sequence.size()+1);
    int len=sequence.size()+1;
    for(int i=len-1;i>=0;i--)
    {
        for(int j=0;j<_nstates;j++)
        {
            if(i==len-1)
            {
                _beta(j,i)=1;
            }
            else
            {
                float s=0;
                for(int k=0;k<_nstates;k++)
                    s=s+_beta(k,i+1)*_emission(k,sequence[i])*_transition(j,k);
                _beta(j,i)=s*_scale(0,i+1);

            }


        }

    }
}
Exemple #2
0
/**
 * @brief forwardMatrix : method computes probability //compute p(x1 ... xn,zn)
 *                        using the forward algorithm
 * @param sequence : is input observation sequence
 */
void ocv::CHMM::forwardMatrix(Mat &sequence)
{



    int len=sequence.rows;
    int dim=sequence.cols;
    _seqlen=len-1;

    for(int i=0;i<len;i++)
    {


        for(int j=0;j<_nstates;j++)
        {
            if(i==0)
            {
            _alpha(j,i)=_emission[j].Prob(sequence.row(i))*_initial(0,j);
            }
            else
            {
                float s=0;
                for(int k=0;k<_nstates;k++)
                s=s+_transition(k,j)*_alpha(k,i-1);

                //changed from sequence.row(i-1) to sequence.row(i)
                //need to verifyt the forward algorithm
                _alpha(j,i)=_emission[j].Prob(sequence.row(i))*s;
            }

    }



    float scale=0;
    for(int j=0;j<_nstates;j++)
    {
        scale=scale+_alpha(j,i);
    }
    //scale=scale+std::numeric_limits<float>::epsilon();
    if(scale==0)
        scale=std::numeric_limits<float>::epsilon();
    scale=1.f/(scale);
    //commented the below code need to verify
    //if(i==0)
//       _scale(0,i)=1;
  //  else
        _scale(0,i)=scale;

    for(int j=0;j<_nstates;j++)
    {
        _alpha(j,i)=scale*_alpha(j,i);
    }


    }



}
Exemple #3
0
/**
 *  Event Handling Function.
 *
 *  Handles the event at current state. Handles message at the current
 *  state. If the message/event is not handled at current state, then
 *  its passed to the parent and passed till 'top'. If its not
 *  handled, till top, then this method returns ERROR. else, the
 *  transition is handled. Transition, involves all exit method from
 *  bottom till LCA and execution of transition object function, and
 *  entry methods from LCA till the next state.
 *
 *  @param smThis Instance Object
 *  @param msg  Event that needs to be handled
 *
 *  @returns 
 *    CL_OK on CL_OK (successful transition) <br/>
 *    CL_SM_RC(CL_ERR_NULL_POINTER) on invalid/null instance/msg handle <br/>
 *    SM_ERR_EXIT_FAILED if the exit handler returned failure
 *    CL_SM_RC(CL_ERR_INVALID_STATE) unable to handle event at current state
 *
 */
ClRcT 
clHsmInstanceOnEvent(ClSmInstancePtrT smThis, 
                   ClSmEventPtrT msg
                   )
{
    ClSmStatePtrT curr;
    ClSmTransitionPtrT tO=0;
    ClRcT ret = CL_OK;
    
    CL_FUNC_ENTER();
    
    CL_ASSERT(smThis);
    CL_ASSERT(msg);
    
    if(!smThis || !smThis->sm || !smThis->current || !msg) 
      {
        ret = CL_SM_RC(CL_ERR_NULL_POINTER);
        CL_FUNC_EXIT();
        return ret;
      }

    for(curr=smThis->current;curr && !tO;)
      {
        
        /* check if the event is in event handler table
         */
        if(msg->eventId < (ClSmEventIdT)curr->maxEventTransitions && msg->eventId >= 0)
          {
            tO = curr->eventTransitionTable[msg->eventId].transition;
            break;
          }
        curr=curr->parent;
      }

    if(curr && tO)
      {
#ifdef DEBUG
        clLogTrace(HSM_LOG_AREA,HSM_LOG_CTX_EVENT,"StateMachine [%s] OnEvent [%d] in State [%d:%s]", 
                              smThis->name,
                              msg->eventId,
                              curr->type,
                              curr->name);
#else
        clLogTrace(HSM_LOG_AREA,HSM_LOG_CTX_EVENT,"OnEvent %d in state %d", 
                              msg->eventId,
                              curr->type);
#endif
        IGNORE_RETURN(_transition(smThis, tO, smThis->current, tO->nextState, msg));
      } 
    else
      {
        ret = CL_SM_RC(CL_ERR_INVALID_STATE);
      }
    
    CL_FUNC_EXIT();
    return ret;
}
Exemple #4
0
/**
 * @brief computeProbability : compute the probability that the sequence
 *        is generate from the markov chain
 * @param sequence : is a vector of integral sequence starting from 0
 * @return         : is probability
 */
float markovChain::computeProbability(vector<int> sequence)
{
    float res=0;
    float init=_initial(0,sequence[0]);
    res=init;
    for(int i=0;i<sequence.size()-1;i++)
    {
        res=res*_transition(sequence[i],sequence[i+1]);
    }
    return res;

}
Exemple #5
0
/**
 * @brief forwardMatrix : method computes probability //compute p(x1 ... xn,zn)
 *                        using the forward algorithm
 * @param sequence : is input observation sequence
 */
void ocv::DHMM::forwardMatrix(vector<int> &sequence)
{

    int len=sequence.size()+1;
    _seqlen=len-1;
    _alpha=MatrixXf(_nstates,_seqlen+1);
    _scale=MatrixXf(1,_seqlen+1);
    for(int i=0;i<len;i++)
    {
    for(int j=0;j<_nstates;j++)
    {
        if(i==0)
            _alpha(j,i)=_emission(j,sequence[i])*_initial(0,j);
        else
        {
            float s=0;
            for(int k=0;k<_nstates;k++)
            s=s+_transition(k,j)*_alpha(k,i-1);
            _alpha(j,i)=_emission(j,sequence[i-1])*s;
        }

    }
    float scale=0;
    for(int j=0;j<_nstates;j++)
    {
        scale=scale+_alpha(j,i);
    }
    scale=1.f/scale;
    if(i==0)
        _scale(0,i)=1;
    else
        _scale(0,i)=scale;

    for(int j=0;j<_nstates;j++)
    {
        _alpha(j,i)=scale*_alpha(j,i);

    }


    }


}
Exemple #6
0
/**=
 * @brief forwardMatrix : method computes probability //compute p(x1 ... xn,zn)
 *                        using the forward algorithm
 * @param sequence : is input observation sequence
 */
void ocv::CHMM::forwardMatrixIterative(Mat &sequence,bool init,int i)
{



    int dim=sequence.cols;



        for(int j=0;j<_nstates;j++)
        {
            if(init==true||i==0)
            {
            _alpha(j,0)=_emission[j].Prob(sequence)+(_initial(0,j));
            }
            else
            {
                float s=0;
                vector<float> work;
                for(int k=0;k<_nstates;k++)
                work.push_back((_transition(k,j))+_alpha(k,i-1));

\
                float r=EigenUtils::logsumexp(work);
                //changed from sequence.row(i-1) to sequence.row(i)
                //need to verifyt the forward algorithm
                _alpha(j,i)=_emission[j].Prob(sequence)+r;
            }
            //cerr << _emission[j].Prob(sequence) << endl;


        }


    float scale=0;
    vector<float> alpha;
    for(int j=0;j<_nstates;j++)
    {
        if(_alpha(j,i)<=-1e200)
            _alpha(j,i)=-1*std::numeric_limits<float>::infinity();
        alpha.push_back(_alpha(j,i));
    }
    scale=EigenUtils::logsumexp(alpha);
    //cerr << scale << ":" ;
    //if(scale==0)
    //    scale=std::numeric_limits<float>::epsilon();
    //scale=1.f/(scale);



    //commented the below code need to verify
    //if(i==0)
//       _scale(0,i)=1;
  //  else
    _scale(0,i)=scale;

    /*for(int j=0;j<_nstates;j++)
    {
        _alpha(j,i)=scale*_alpha(j,i);
    }*/



}