001    package aima.test.probdecisiontest;
002    
003    import aima.probability.Randomizer;
004    import aima.probability.decision.MDP;
005    import aima.probability.decision.MDPFactory;
006    import aima.probability.decision.MDPPolicy;
007    import aima.probability.decision.MDPUtilityFunction;
008    import aima.probability.decision.cellworld.CellWorldPosition;
009    import junit.framework.TestCase;
010    
011    /**
012     * @author Ravi Mohan
013     * 
014     */
015    public class PolicyIterationTest extends TestCase {
016            private MDP<CellWorldPosition, String> fourByThreeMDP;
017    
018            private Randomizer alwaysLessThanEightyPercent;
019    
020            @Override
021            public void setUp() {
022                    fourByThreeMDP = MDPFactory.createFourByThreeMDP();
023    
024            }
025    
026            public void testPolicyEvaluation() {
027                    MDPPolicy<CellWorldPosition, String> policy = fourByThreeMDP
028                                    .randomPolicy();
029                    MDPUtilityFunction<CellWorldPosition> uf1 = fourByThreeMDP
030                                    .initialUtilityFunction();
031                    // System.out.println("before " +uf1);
032                    MDPUtilityFunction<CellWorldPosition> uf2 = fourByThreeMDP
033                                    .policyEvaluation(policy, uf1, 1, 3);
034                    // System.out.println("after " +uf2);
035    
036                    assertFalse(uf1.equals(uf2));
037    
038            }
039    
040            public void testPolicyIteration() {
041    
042                    MDPPolicy<CellWorldPosition, String> policy = fourByThreeMDP
043                                    .policyIteration(1);
044                    // check With Figure 17.2 (a)
045                    // System.out.println("policy after = " + policy);
046    
047                    assertEquals("up", policy.getAction(new CellWorldPosition(1, 1)));
048                    assertEquals("up", policy.getAction(new CellWorldPosition(2, 1)));
049                    assertEquals("right", policy.getAction(new CellWorldPosition(3, 1)));
050    
051                    assertEquals("left", policy.getAction(new CellWorldPosition(1, 2)));
052                    assertEquals("right", policy.getAction(new CellWorldPosition(3, 2)));
053    
054                    assertEquals("left", policy.getAction(new CellWorldPosition(1, 3)));
055                    assertEquals("up", policy.getAction(new CellWorldPosition(2, 3)));
056                    assertEquals("right", policy.getAction(new CellWorldPosition(3, 3)));
057    
058                    assertEquals("left", policy.getAction(new CellWorldPosition(1, 4)));
059    
060            }
061    
062    }