001    package aima.test.probdecisiontest;
002    
003    import junit.framework.TestCase;
004    import aima.probability.Randomizer;
005    import aima.probability.decision.MDPTransitionModel;
006    import aima.probability.decision.MDPUtilityFunction;
007    import aima.probability.decision.cellworld.CellWorld;
008    import aima.probability.decision.cellworld.CellWorldPosition;
009    import aima.test.probabilitytest.MockRandomizer;
010    import aima.util.Pair;
011    
012    /**
013     * @author Ravi Mohan
014     * 
015     */
016    
017    public class CellWorldTest extends TestCase {
018    
019            private CellWorld cw;
020    
021            private Randomizer alwaysLessThanEightyPercent,
022                            betweenEightyAndNinetyPercent, greaterThanNinetyPercent;
023    
024            @Override
025            public void setUp() {
026                    cw = new CellWorld(3, 4, -0.04);
027    
028                    cw.markBlocked(2, 2);
029    
030                    cw.setTerminalState(2, 4);
031                    cw.setReward(2, 4, -1);
032    
033                    cw.setTerminalState(3, 4);
034                    cw.setReward(3, 4, 1);
035    
036                    alwaysLessThanEightyPercent = new MockRandomizer(new double[] { 0.7 });
037                    betweenEightyAndNinetyPercent = new MockRandomizer(
038                                    new double[] { 0.85 });
039                    greaterThanNinetyPercent = new MockRandomizer(new double[] { 0.95 });
040            }
041    
042            public void testMoveLeftIntoWallLeavesPositionUnchanged() {
043                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 1,
044                                    CellWorld.LEFT, alwaysLessThanEightyPercent);
045                    assertEquals(1, pos.getX());
046                    assertEquals(1, pos.getY());
047            }
048    
049            public void testMoveLeftIntoUnblockedCellChangesPositionCorrectly() {
050                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 2,
051                                    CellWorld.LEFT, alwaysLessThanEightyPercent);
052                    assertEquals(1, pos.getX());
053                    assertEquals(1, pos.getY());
054            }
055    
056            public void testMoveLeftIntoUnblockedCellActuallyMovesUpWhenProbabilityBetween80And90Percent() {
057                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 4,
058                                    CellWorld.LEFT, betweenEightyAndNinetyPercent);
059                    assertEquals(2, pos.getX());
060                    assertEquals(4, pos.getY());
061            }
062    
063            public void testMoveLeftIntoUnblockedCellActuallyMovesDownWhenProbabilityGreaterThan90Percent() {
064                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(3, 3,
065                                    CellWorld.LEFT, greaterThanNinetyPercent);
066                    assertEquals(2, pos.getX());
067                    assertEquals(3, pos.getY());
068            }
069    
070            public void testMoveLeftIntoBlockedCellLeavesPositionUnchanged() {
071                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(2, 3,
072                                    CellWorld.LEFT, alwaysLessThanEightyPercent);
073                    assertEquals(2, pos.getX());
074                    assertEquals(3, pos.getY());
075            }
076    
077            public void testMoveRightIntoWallLeavesPositionUnchanged() {
078                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 4,
079                                    CellWorld.RIGHT, alwaysLessThanEightyPercent);
080                    assertEquals(1, pos.getX());
081                    assertEquals(4, pos.getY());
082            }
083    
084            public void testMoveRightIntoUnblockedCellChangesPositionCorrectly() {
085                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 2,
086                                    CellWorld.RIGHT, alwaysLessThanEightyPercent);
087                    assertEquals(1, pos.getX());
088                    assertEquals(3, pos.getY());
089            }
090    
091            public void testMoveRightIntoBlockedCellLeavesPositionUnchanged() {
092                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(2, 1,
093                                    CellWorld.RIGHT, alwaysLessThanEightyPercent);
094                    assertEquals(2, pos.getX());
095                    assertEquals(1, pos.getY());
096            }
097    
098            public void testMoveRightIntoUnblockedCellActuallyMovesUpWhenProbabilityBetween80And90Percent() {
099                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 1,
100                                    CellWorld.RIGHT, betweenEightyAndNinetyPercent);
101                    assertEquals(2, pos.getX());
102                    assertEquals(1, pos.getY());
103            }
104    
105            public void testMoveRightIntoUnblockedCellActuallyMovesDownWhenProbabilityGreaterThan90Percent() {
106                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(3, 3,
107                                    CellWorld.RIGHT, greaterThanNinetyPercent);
108                    assertEquals(2, pos.getX());
109                    assertEquals(3, pos.getY());
110            }
111    
112            public void testMoveUpIntoWallLeavesPositionUnchanged() {
113                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(3, 1,
114                                    CellWorld.UP, alwaysLessThanEightyPercent);
115                    assertEquals(3, pos.getX());
116                    assertEquals(1, pos.getY());
117            }
118    
119            public void testMoveUpIntoUnblockedCellChangesPositionCorrectly() {
120                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 1,
121                                    CellWorld.UP, alwaysLessThanEightyPercent);
122                    assertEquals(2, pos.getX());
123                    assertEquals(1, pos.getY());
124            }
125    
126            public void testMoveUpIntoBlockedCellLeavesPositionUnchanged() {
127                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 2,
128                                    CellWorld.UP, alwaysLessThanEightyPercent);
129                    assertEquals(1, pos.getX());
130                    assertEquals(2, pos.getY());
131            }
132    
133            public void testMoveUpActuallyMovesLeftWhenProbabilityBetween80And90Percent() {
134                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 4,
135                                    CellWorld.UP, betweenEightyAndNinetyPercent);
136                    assertEquals(1, pos.getX());
137                    assertEquals(3, pos.getY());
138            }
139    
140            public void testMoveUpActuallyMovesRightWhenProbabilityGreaterThan90Percent() {
141                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 3,
142                                    CellWorld.UP, greaterThanNinetyPercent);
143                    assertEquals(1, pos.getX());
144                    assertEquals(4, pos.getY());
145            }
146    
147            public void testMoveDownIntoWallLeavesPositionUnchanged() {
148                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(1, 1,
149                                    CellWorld.DOWN, alwaysLessThanEightyPercent);
150                    assertEquals(1, pos.getX());
151                    assertEquals(1, pos.getY());
152            }
153    
154            public void testMoveDownIntoUnblockedCellChangesPositionCorrectly() {
155                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(2, 1,
156                                    CellWorld.DOWN, alwaysLessThanEightyPercent);
157                    assertEquals(1, pos.getX());
158                    assertEquals(1, pos.getY());
159            }
160    
161            public void testMoveDownIntoBlockedCellLeavesPositionUnchanged() {
162                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(3, 2,
163                                    CellWorld.UP, alwaysLessThanEightyPercent);
164                    assertEquals(3, pos.getX());
165                    assertEquals(2, pos.getY());
166            }
167    
168            public void testMoveDownActuallyMovesLeftWhenProbabilityBetween80And90Percent() {
169                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(3, 3,
170                                    CellWorld.DOWN, betweenEightyAndNinetyPercent);
171                    assertEquals(3, pos.getX());
172                    assertEquals(2, pos.getY());
173            }
174    
175            public void testMoveDownActuallyMovesRightWhenProbabilityGreaterThan90Percent() {
176                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(2, 3,
177                                    CellWorld.UP, greaterThanNinetyPercent);
178                    assertEquals(2, pos.getX());
179                    assertEquals(4, pos.getY());
180            }
181    
182            public void testNumberOfUnBlockedCells() {
183                    assertEquals(11, cw.unblockedCells().size());
184            }
185    
186            public void testMoveFromATerminalStateFailsForAllProbabilityValues() {
187                    CellWorldPosition pos = cw.moveProbabilisticallyFrom(2, 4,
188                                    CellWorld.UP, alwaysLessThanEightyPercent);
189                    assertEquals(2, pos.getX());
190                    assertEquals(4, pos.getY());
191            }
192    
193            public void testTransitionProbabilityCalculationWhenEndingPositionIsNextToStartingPositionButIsBlocked() {
194                    CellWorldPosition startingPosition = new CellWorldPosition(2, 1); // to
195                    // the
196                    // left
197                    // of
198                    // blocked
199                    // cell
200                    CellWorldPosition endingPosition = new CellWorldPosition(2, 2); // blocked
201                    // cell
202                    double transitionProb = cw.getTransitionProbability(startingPosition,
203                                    CellWorld.RIGHT, endingPosition);
204                    assertEquals(0.0, transitionProb);
205            }
206    
207            public void testTransitionProbabilityCalculationWhenEndingPositionCannotBeReachedUsingDesiredActionOrRightAngledSteps() {
208                    CellWorldPosition startingPosition = new CellWorldPosition(1, 3);
209                    CellWorldPosition endingPosition = new CellWorldPosition(3, 3);
210                    double transitionProb = cw.getTransitionProbability(startingPosition,
211                                    CellWorld.UP, endingPosition);
212                    assertEquals(0.0, transitionProb);
213            }
214    
215            public void testTransitionProbabilityCalculationWhenEndingPositionReachebleByExecutingSuggestedAction() {
216                    CellWorldPosition startingPosition = new CellWorldPosition(1, 1);
217                    CellWorldPosition endingPosition = new CellWorldPosition(2, 1);
218                    double transitionProb = cw.getTransitionProbability(startingPosition,
219                                    CellWorld.UP, endingPosition);
220                    assertEquals(0.8, transitionProb);
221    
222            }
223    
224            public void testTransitionProbabilityCalculationWhenBothRightAngledActiosnLeadToStartingPosition() {
225                    CellWorldPosition startingPosition = new CellWorldPosition(2, 1);
226                    CellWorldPosition endingPosition = new CellWorldPosition(2, 1);
227                    double transitionProb = cw.getTransitionProbability(startingPosition,
228                                    CellWorld.UP, endingPosition);
229                    assertEquals(0.2, transitionProb);
230    
231            }
232    
233            public void testTransitionModelCreation() {
234                    MDPTransitionModel<CellWorldPosition, String> mtm = cw
235                                    .getTransitionModel();
236                    CellWorldPosition startingPosition = new CellWorldPosition(1, 1);
237                    CellWorldPosition endingPosition = new CellWorldPosition(2, 1);
238                    assertEquals(0.8, mtm.getTransitionProbability(startingPosition,
239                                    CellWorld.UP, endingPosition));
240    
241                    CellWorldPosition endingPosition2 = new CellWorldPosition(1, 1);
242                    assertEquals(0.1, mtm.getTransitionProbability(startingPosition,
243                                    CellWorld.UP, endingPosition2));
244                    CellWorldPosition endingPosition3 = new CellWorldPosition(1, 2);
245                    assertEquals(0.1, mtm.getTransitionProbability(startingPosition,
246                                    CellWorld.UP, endingPosition3));
247    
248                    // Reward
249                    // for (CellWorldPosition cp : cw.unblockedPositions()){
250                    //                      
251                    // }
252                    //              
253                    // MDPTransition<CellWorldPosition, String> transition =
254                    // mtm.maxTransition(eew CellWorldPosition(1,1), uf);
255    
256            }
257    
258            public void testCannotTransitionFromFinalState() {
259                    MDPTransitionModel<CellWorldPosition, String> mtm = cw
260                                    .getTransitionModel();
261                    CellWorldPosition terminalOne = new CellWorldPosition(2, 4);
262                    CellWorldPosition terminalTwo = new CellWorldPosition(3, 4);
263                    assertEquals(0.0, mtm.getTransitionProbability(terminalOne,
264                                    CellWorld.UP, terminalTwo));
265                    assertEquals(0.0, mtm.getTransitionProbability(terminalTwo,
266                                    CellWorld.DOWN, terminalOne));
267    
268            }
269    
270            public void testMaximumTransitionDetection() { // aka policy extraction
271                    // given a utility function
272                    MDPTransitionModel<CellWorldPosition, String> mtm = cw
273                                    .getTransitionModel();
274    
275                    // create the Utility Function depicted in Fig 17.3
276                    MDPUtilityFunction<CellWorldPosition> uf = new MDPUtilityFunction<CellWorldPosition>();
277                    uf.setUtility(new CellWorldPosition(1, 1), 0.705);
278                    uf.setUtility(new CellWorldPosition(1, 2), 0.655);
279                    uf.setUtility(new CellWorldPosition(1, 3), 0.611);
280                    uf.setUtility(new CellWorldPosition(1, 4), 0.388);
281    
282                    uf.setUtility(new CellWorldPosition(2, 1), 0.762);
283                    uf.setUtility(new CellWorldPosition(2, 3), 0.660);
284                    uf.setUtility(new CellWorldPosition(2, 4), -1.0);
285    
286                    uf.setUtility(new CellWorldPosition(3, 1), 0.812);
287                    uf.setUtility(new CellWorldPosition(3, 2), 0.868);
288                    uf.setUtility(new CellWorldPosition(3, 3), 0.918);
289                    uf.setUtility(new CellWorldPosition(3, 4), 1.0);
290    
291                    assertPolicyReccomends(cw, uf, 1, 1, CellWorld.UP);
292                    assertPolicyReccomends(cw, uf, 1, 2, CellWorld.LEFT);
293                    assertPolicyReccomends(cw, uf, 1, 3, CellWorld.LEFT);
294                    assertPolicyReccomends(cw, uf, 1, 4, CellWorld.LEFT);
295    
296                    assertPolicyReccomends(cw, uf, 2, 1, CellWorld.UP);
297                    assertPolicyReccomends(cw, uf, 2, 3, CellWorld.UP);
298                    assertPolicyReccomends(cw, uf, 2, 4, null);
299    
300                    assertPolicyReccomends(cw, uf, 3, 1, CellWorld.RIGHT);
301                    assertPolicyReccomends(cw, uf, 3, 2, CellWorld.RIGHT);
302                    assertPolicyReccomends(cw, uf, 3, 3, CellWorld.RIGHT);
303                    assertPolicyReccomends(cw, uf, 3, 4, null);
304    
305            }
306    
307            private void assertPolicyReccomends(CellWorld cw,
308                            MDPUtilityFunction<CellWorldPosition> uf, int x, int y,
309                            String actionExpected) {
310                    Pair<String, Double> p = cw.getTransitionModel()
311                                    .getTransitionWithMaximumExpectedUtility(
312                                                    new CellWorldPosition(x, y), uf);
313                    // System.out.println(p);
314                    assertEquals(actionExpected, p.getFirst());
315            }
316    
317    }