示例#1
0
        public RandomVariable calculate_next_backward_message(
            RandomVariable forwardBelief,
            RandomVariable present_backward_message, String perception)
        {
            RandomVariable result = present_backward_message.duplicate();
            // System.Console.WriteLine("fb :-calculating new backward message");
            // System.Console.WriteLine("fb :-diagonal matrix from sens model = ");
            Matrix oMatrix = _sensorModel.asMatrix(perception);
            // System.Console.WriteLine(oMatrix);
            Matrix transitionMatrix = _transitionModel.asMatrix();// action
            // should
            // be
            // passed
            // in
            // here?
            // System.Console.WriteLine("fb :-present backward message = "
            // +present_backward_message);
            Matrix backwardMatrix = transitionMatrix.times(oMatrix
                                                           .times(present_backward_message.asMatrix()));
            Matrix resultMatrix = backwardMatrix.arrayTimes(forwardBelief
                                                            .asMatrix());

            result.updateFrom(resultMatrix);
            result.normalize();
            // System.Console.WriteLine("fb :-normalized new backward message = "
            // +result);
            return(result);
        }
示例#2
0
        public List <RandomVariable> forward_backward(List <String> perceptions)
        {
            RandomVariable[] forwardMessages = new RandomVariable[perceptions
                                                                  .Count + 1];
            RandomVariable backwardMessage = priorDistribution.createUnitBelief();

            RandomVariable[] smoothedBeliefs = new RandomVariable[perceptions
                                                                  .Count + 1];

            forwardMessages[0] = priorDistribution;
            smoothedBeliefs[0] = null;

            // populate forward messages
            for (int i = 0; i < perceptions.Count; i++)
            { // N.B i starts at 1,
                // not zero
                forwardMessages[i + 1] = forward(forwardMessages[i], perceptions[i]);
            }
            for (int i = perceptions.Count; i > 0; i--)
            {
                RandomVariable smoothed = priorDistribution.duplicate();
                smoothed.updateFrom(forwardMessages[i].asMatrix().arrayTimes(
                                        backwardMessage.asMatrix()));
                smoothed.normalize();
                smoothedBeliefs[i] = smoothed;
                backwardMessage    = calculate_next_backward_message(
                    forwardMessages[i], backwardMessage, perceptions[i - 1]);
            }

            return(new List <RandomVariable>(smoothedBeliefs));
        }
示例#3
0
        public RandomVariable smooth(String perception)
        {
            evidenceFromSmoothedStepToPresent.Add(perception);
            Matrix O_t = hmm.sensorModel().asMatrix(perception);
            Matrix transitionMatrix = hmm.transitionModel().asMatrix();

            if (time > timelag)
            {
                forwardMessage = hmm.forward(forwardMessage, perception); // This
                // seems
                // WRONG
                // I think this should be
                // forwardMessage = hmm.forward(forwardMessage,
                // evidenceFromSmoothedStepToPresent.get(0));
                // this the perception at t-d. the book's algorithm
                // uses the latest perception.
                evidenceFromSmoothedStepToPresent.RemoveAt(0);
                Matrix O_t_minus_d = hmm.sensorModel().asMatrix(
                    evidenceFromSmoothedStepToPresent[0]);

                B = O_t_minus_d.inverse().times(
                    transitionMatrix.inverse().times(
                        B.times(transitionMatrix.times(O_t))));
            }
            else
            {
                B = B.times(transitionMatrix.times(O_t));
            }
            time += 1;
            if (time > timelag)
            {
                Matrix         one             = hmm.prior().createUnitBelief().asMatrix();
                Matrix         forwardMatrix   = forwardMessage.asMatrix();
                RandomVariable result          = hmm.prior().duplicate();
                Matrix         backwardMessage = (B.times(one));

                result.updateFrom(forwardMatrix.arrayTimes(backwardMessage));

                result.normalize();
                return(result);
            }
            else
            {
                return(null);
            }
        }
示例#4
0
        public RandomVariable perceptionUpdate(RandomVariable aBelief,
                                               String perception)
        {
            RandomVariable newBelief = aBelief.duplicate();

            // one way - use matrices
            Matrix beliefMatrix = aBelief.asMatrix();
            Matrix o_matrix     = _sensorModel.asMatrix(perception);
            Matrix updated      = o_matrix.times(beliefMatrix);

            newBelief.updateFrom(updated);
            newBelief.normalize();
            return(newBelief);

            // alternate way of doing this. clearer in intent.
            // for (String state : aBelief.states()){
            // double probabilityOfPerception= sensorModel.get(state,perception);
            // newBelief.setProbabilityOf(state,probabilityOfPerception *
            // aBelief.getProbabilityOf(state));
            // }
        }