001    package aima.probability.reasoning;
002    
003    import java.util.Arrays;
004    import java.util.List;
005    
006    import aima.probability.RandomVariable;
007    
008    /**
009     * @author Ravi Mohan
010     * 
011     */
012    public class HMMFactory {
013    
014            public static HiddenMarkovModel createRobotHMM() {
015    
016                    // Example adopted from Sebastian Thrun's "Probabilistic Robotics"
017                    // Chapter 2
018                    // A robot faces a door. The state of the door (open or closed)
019                    // constitutes the "hidden state"
020                    // The robot's sensor detects a "closed" or "open" state.
021                    // (Perception may be out of synch with reality because sensors are
022                    // probabilistic).
023                    // The robot can either "do nothing" or "push" the door. These are the
024                    // possible actions.
025    
026                    List<String> states = Arrays.asList(new String[] {
027                                    HmmConstants.DOOR_OPEN, HmmConstants.DOOR_CLOSED });
028                    List<String> actions = Arrays.asList(new String[] {
029                                    HmmConstants.DO_NOTHING, HmmConstants.PUSH_DOOR });
030                    List<String> perceptions = Arrays.asList(new String[] {
031                                    HmmConstants.SEE_DOOR_OPEN, HmmConstants.SEE_DOOR_CLOSED });
032    
033                    RandomVariable prior = new RandomVariable(states);
034                    TransitionModel tm = new TransitionModel(states, actions);
035                    // tm.setTransitionProbability(start_state, action, end_state,
036                    // probability)
037                    // given a start state and an action the probability of the end state is
038                    // probability
039                    tm.setTransitionProbability(HmmConstants.DOOR_OPEN,
040                                    HmmConstants.DO_NOTHING, HmmConstants.DOOR_OPEN, 1.0);
041                    tm.setTransitionProbability(HmmConstants.DOOR_OPEN,
042                                    HmmConstants.DO_NOTHING, HmmConstants.DOOR_OPEN, 1.0);
043                    tm.setTransitionProbability(HmmConstants.DOOR_OPEN,
044                                    HmmConstants.DO_NOTHING, HmmConstants.DOOR_CLOSED, 0.0);
045                    tm.setTransitionProbability(HmmConstants.DOOR_CLOSED,
046                                    HmmConstants.DO_NOTHING, HmmConstants.DOOR_CLOSED, 1.0);
047                    tm.setTransitionProbability(HmmConstants.DOOR_CLOSED,
048                                    HmmConstants.DO_NOTHING, HmmConstants.DOOR_OPEN, 0.0);
049    
050                    tm.setTransitionProbability(HmmConstants.DOOR_OPEN,
051                                    HmmConstants.PUSH_DOOR, HmmConstants.DOOR_OPEN, 1.0);
052                    tm.setTransitionProbability(HmmConstants.DOOR_OPEN,
053                                    HmmConstants.PUSH_DOOR, HmmConstants.DOOR_CLOSED, 0.0);
054                    tm.setTransitionProbability(HmmConstants.DOOR_CLOSED,
055                                    HmmConstants.PUSH_DOOR, HmmConstants.DOOR_CLOSED, 0.2);
056                    tm.setTransitionProbability(HmmConstants.DOOR_CLOSED,
057                                    HmmConstants.PUSH_DOOR, HmmConstants.DOOR_OPEN, 0.8);
058    
059                    SensorModel sm = new SensorModel(states, perceptions);
060                    // sm.setSensingProbaility(state,perception,p); given a state the
061                    // probability of a perception is p
062                    sm.setSensingProbability(HmmConstants.DOOR_OPEN,
063                                    HmmConstants.SEE_DOOR_CLOSED, 0.4);
064                    sm.setSensingProbability(HmmConstants.DOOR_OPEN,
065                                    HmmConstants.SEE_DOOR_OPEN, 0.6);
066                    sm.setSensingProbability(HmmConstants.DOOR_CLOSED,
067                                    HmmConstants.SEE_DOOR_OPEN, 0.2);
068                    sm.setSensingProbability(HmmConstants.DOOR_CLOSED,
069                                    HmmConstants.SEE_DOOR_CLOSED, 0.8);
070    
071                    HiddenMarkovModel hmm = new HiddenMarkovModel(prior, tm, sm);
072                    return hmm;
073            }
074    
075            public static HiddenMarkovModel createRainmanHMM() {
076                    List<String> states = Arrays.asList(new String[] {
077                                    HmmConstants.RAINING, HmmConstants.NOT_RAINING });
078                    // no actions because the observer has no way of changing the hidden
079                    // state and i spassive
080                    List<String> perceptions = Arrays.asList(new String[] {
081                                    HmmConstants.SEE_UMBRELLA, HmmConstants.SEE_NO_UMBRELLA });
082    
083                    RandomVariable prior = new RandomVariable(states);
084    
085                    TransitionModel tm = new TransitionModel(states);
086                    // tm.setTransitionModelValue(start_state, action, end_state,
087                    // probability);
088                    // given a start state and an action the probability of the end state is
089                    // probability
090                    tm.setTransitionProbability(HmmConstants.RAINING, HmmConstants.RAINING,
091                                    0.7);
092                    tm.setTransitionProbability(HmmConstants.RAINING,
093                                    HmmConstants.NOT_RAINING, 0.3);
094                    tm.setTransitionProbability(HmmConstants.NOT_RAINING,
095                                    HmmConstants.RAINING, 0.3);
096                    tm.setTransitionProbability(HmmConstants.NOT_RAINING,
097                                    HmmConstants.NOT_RAINING, 0.7);
098    
099                    SensorModel sm = new SensorModel(states, perceptions);
100                    // sm.setSensingProbaility(state,perception,p); given a state the
101                    // probability of a perception is p
102                    sm.setSensingProbability(HmmConstants.RAINING,
103                                    HmmConstants.SEE_UMBRELLA, 0.9);
104                    sm.setSensingProbability(HmmConstants.RAINING,
105                                    HmmConstants.SEE_NO_UMBRELLA, 0.1);
106                    sm.setSensingProbability(HmmConstants.NOT_RAINING,
107                                    HmmConstants.SEE_UMBRELLA, 0.2);
108                    sm.setSensingProbability(HmmConstants.NOT_RAINING,
109                                    HmmConstants.SEE_NO_UMBRELLA, 0.8);
110    
111                    HiddenMarkovModel hmm = new HiddenMarkovModel(prior, tm, sm);
112    
113                    // hmm.setSensorModelValue(state,perception,p); given a state the
114                    // probability of a perception is p
115    
116                    return hmm;
117    
118            }
119    
120    }