001    package aima.test.search.online;
002    
003    import junit.framework.TestCase;
004    import aima.basic.BasicEnvironmentView;
005    import aima.search.framework.HeuristicFunction;
006    import aima.search.map.BidirectionalMapProblem;
007    import aima.search.map.ExtendableMap;
008    import aima.search.map.MapEnvironment;
009    import aima.search.online.LRTAStarAgent;
010    
011    public class LRTAStarAgentTest extends TestCase {
012            ExtendableMap aMap;
013    
014            StringBuffer envChanges;
015    
016            HeuristicFunction hf;
017    
018            @Override
019            public void setUp() {
020                    aMap = new ExtendableMap();
021                    aMap.addBidirectionalLink("A", "B", 4.0);
022                    aMap.addBidirectionalLink("B", "C", 4.0);
023                    aMap.addBidirectionalLink("C", "D", 4.0);
024                    aMap.addBidirectionalLink("D", "E", 4.0);
025                    aMap.addBidirectionalLink("E", "F", 4.0);
026                    hf = new HeuristicFunction() {
027                            public double getHeuristicValue(Object state) {
028                                    return 1;
029                            }
030                    };
031    
032                    envChanges = new StringBuffer();
033            }
034    
035            public void testAlreadyAtGoal() {
036                    MapEnvironment me = new MapEnvironment(aMap);
037                    LRTAStarAgent agent = new LRTAStarAgent(new BidirectionalMapProblem(me
038                                    .getMap(), "A", "A", hf));
039                    me.addAgent(agent, "A");
040                    me.registerView(new BasicEnvironmentView() {
041                            @Override
042                            public void envChanged(String command) {
043                                    envChanges.append(command).append("->");
044                            }
045                    });
046                    me.stepUntilDone();
047    
048                    assertEquals("NoOP->", envChanges.toString());
049            }
050    
051            public void testNormalSearch() {
052                    MapEnvironment me = new MapEnvironment(aMap);
053                    LRTAStarAgent agent = new LRTAStarAgent(new BidirectionalMapProblem(me
054                                    .getMap(), "A", "F", hf));
055                    me.addAgent(agent, "A");
056                    me.registerView(new BasicEnvironmentView() {
057                            @Override
058                            public void envChanged(String command) {
059                                    envChanges.append(command).append("->");
060                            }
061                    });
062                    me.stepUntilDone();
063    
064                    assertEquals("B->A->B->C->B->C->D->C->D->E->D->E->F->NoOP->",
065                                    envChanges.toString());
066            }
067    
068            public void testNoPath() {
069                    // Note: Will search forever if no path is possible.
070                    // MapEnvironment me = new MapEnvironment(aMap);
071                    // LRTAStarAgent agent = new LRTAStarAgent(new
072                    // BidirectionalMapProblem(me
073                    // .getMap(), "A", "G", hf));
074                    // me.addAgent(agent, "A");
075                    // me.registerView(new BasicEnvironmentView() {
076                    // @Override
077                    // public void envChanged(String command) {
078                    // envChanges.append(command).append("->");
079                    // }
080                    // });
081                    // me.stepUntilNoOp();
082                    //
083                    // assertEquals("B->A->B->C->B->C->D->C->D->E->D->E->F->NoOP->",
084                    // envChanges.toString());
085            }
086    }