001    package aima.test.probreasoningtest;
002    
003    import aima.probability.reasoning.HMMAgent;
004    import aima.probability.reasoning.HMMFactory;
005    import aima.probability.reasoning.HmmConstants;
006    import junit.framework.TestCase;
007    
008    /**
009     * @author Ravi Mohan
010     * 
011     */
012    public class HMMAgentTest extends TestCase {
013            private static final double TOLERANCE = 0.001;
014    
015            private HMMAgent robot, rainman;
016    
017            @Override
018            public void setUp() {
019    
020                    robot = new HMMAgent(HMMFactory.createRobotHMM());
021                    rainman = new HMMAgent(HMMFactory.createRainmanHMM());
022            }
023    
024            public void testRobotInitialization() {
025                    assertEquals(0.5, robot.belief().getProbabilityOf(
026                                    HmmConstants.DOOR_OPEN));
027                    assertEquals(0.5, robot.belief().getProbabilityOf(
028                                    HmmConstants.DOOR_CLOSED));
029            }
030    
031            public void testRobotHMMPredictionAndMeasurementUpdateStepsModifyBeliefCorrectly() {
032    
033                    assertEquals(0.5, robot.belief().getProbabilityOf(
034                                    HmmConstants.DOOR_OPEN));
035                    assertEquals(0.5, robot.belief().getProbabilityOf(
036                                    HmmConstants.DOOR_CLOSED));
037    
038                    robot.act(HmmConstants.DO_NOTHING);
039                    assertEquals(0.5, robot.belief().getProbabilityOf(
040                                    HmmConstants.DOOR_OPEN));
041                    assertEquals(0.5, robot.belief().getProbabilityOf(
042                                    HmmConstants.DOOR_CLOSED));
043    
044                    robot.perceive(HmmConstants.SEE_DOOR_OPEN);
045                    assertEquals(0.75, robot.belief().getProbabilityOf(
046                                    HmmConstants.DOOR_OPEN), TOLERANCE);
047                    assertEquals(0.25, robot.belief().getProbabilityOf(
048                                    HmmConstants.DOOR_CLOSED), TOLERANCE);
049    
050                    robot.act(HmmConstants.PUSH_DOOR);
051                    assertEquals(0.95, robot.belief().getProbabilityOf(
052                                    HmmConstants.DOOR_OPEN));
053                    assertEquals(0.05, robot.belief().getProbabilityOf(
054                                    HmmConstants.DOOR_CLOSED));
055    
056                    robot.perceive(HmmConstants.SEE_DOOR_OPEN);
057                    assertEquals(0.983, robot.belief().getProbabilityOf(
058                                    HmmConstants.DOOR_OPEN), TOLERANCE);
059                    assertEquals(0.017, robot.belief().getProbabilityOf(
060                                    HmmConstants.DOOR_CLOSED), TOLERANCE);
061    
062            }
063    
064            public void testRainmanInitialization() {
065                    assertEquals(0.5, rainman.belief().getProbabilityOf(
066                                    HmmConstants.RAINING));
067                    assertEquals(0.5, rainman.belief().getProbabilityOf(
068                                    HmmConstants.NOT_RAINING));
069            }
070    
071            public void testRainmanHMMPredictionAndMeasurementUpdateStepsModifyBeliefCorrectly() {
072                    assertEquals(0.5, rainman.belief().getProbabilityOf(
073                                    HmmConstants.RAINING));
074                    assertEquals(0.5, rainman.belief().getProbabilityOf(
075                                    HmmConstants.NOT_RAINING));
076    
077                    rainman.waitWithoutActing();
078                    assertEquals(0.5, rainman.belief().getProbabilityOf(
079                                    HmmConstants.RAINING));
080                    assertEquals(0.5, rainman.belief().getProbabilityOf(
081                                    HmmConstants.NOT_RAINING));
082    
083                    rainman.perceive(HmmConstants.SEE_UMBRELLA);
084                    assertEquals(0.818, rainman.belief().getProbabilityOf(
085                                    HmmConstants.RAINING), TOLERANCE);
086                    assertEquals(0.182, rainman.belief().getProbabilityOf(
087                                    HmmConstants.NOT_RAINING), TOLERANCE);
088    
089                    rainman.waitWithoutActing();
090                    assertEquals(0.627, rainman.belief().getProbabilityOf(
091                                    HmmConstants.RAINING), TOLERANCE);
092                    assertEquals(0.373, rainman.belief().getProbabilityOf(
093                                    HmmConstants.NOT_RAINING), TOLERANCE);
094    
095                    rainman.perceive(HmmConstants.SEE_UMBRELLA);
096                    assertEquals(0.883, rainman.belief().getProbabilityOf(
097                                    HmmConstants.RAINING), TOLERANCE);
098                    assertEquals(0.117, rainman.belief().getProbabilityOf(
099                                    HmmConstants.NOT_RAINING), TOLERANCE);
100    
101            }
102    
103    }