Example #1
0
        public RandomVariable CalculateNextBackwardMessage(
            RandomVariable forwardBelief,
            RandomVariable presentBackwardMessage, string perception)
        {
            RandomVariable result = presentBackwardMessage.Duplicate();
            // System.out.println("fb :-calculating new backward message");
            // System.out.println("fb :-diagonal matrix from sens model = ");
            Matrix oMatrix = this.SensorModel.AsMatrix(perception);
            // System.out.println(oMatrix);
            Matrix transitionMatrix = this.TransitionModel.AsMatrix();// action
            // should
            // be
            // passed
            // in
            // here?
            // System.out.println("fb :-present backward message = "
            // +present_backward_message);
            Matrix backwardMatrix = transitionMatrix.Times(oMatrix
                                                           .Times(presentBackwardMessage.AsMatrix()));
            Matrix resultMatrix = backwardMatrix.ArrayTimes(forwardBelief
                                                            .AsMatrix());

            result.UpdateFrom(resultMatrix);
            result.Normalize();
            // System.out.println("fb :-normalized new backward message = "
            // +result);
            return(result);
        }
Example #2
0
        public List <RandomVariable> ForwardBackward(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] = this.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    = this.CalculateNextBackwardMessage(
                    forwardMessages[i], backwardMessage, perceptions[i - 1]);
            }

            return(smoothedBeliefs.ToList());
        }
        public RandomVariable Smooth(String perception)
        {
            evidenceFromSmoothedStepToPresent.Add(perception);
            Matrix oT = 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 oTMinusD = hmm.SensorModel.AsMatrix(
                    evidenceFromSmoothedStepToPresent[0]);

                B = oTMinusD.Inverse().Times(
                    transitionMatrix.Inverse().Times(
                        B.Times(transitionMatrix.Times(oT))));
            }
            else
            {
                B = B.Times(transitionMatrix.Times(oT));
            }
            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);
            }
        }
Example #4
0
        public RandomVariable PerceptionUpdate(RandomVariable aBelief,
                                               string perception)
        {
            RandomVariable newBelief = aBelief.Duplicate();

            // one way - use matrices
            Matrix beliefMatrix = aBelief.AsMatrix();
            Matrix oMatrix      = this.SensorModel.AsMatrix(perception);
            Matrix updated      = oMatrix.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));
            // }
        }