001 package aima.test.search.online; 002 003 import junit.framework.TestCase; 004 import aima.basic.BasicEnvironmentView; 005 import aima.search.map.BidirectionalMapProblem; 006 import aima.search.map.ExtendableMap; 007 import aima.search.map.MapEnvironment; 008 import aima.search.online.OnlineDFSAgent; 009 010 public class OnlineDFSAgentTest extends TestCase { 011 012 ExtendableMap aMap; 013 014 StringBuffer envChanges; 015 016 @Override 017 public void setUp() { 018 aMap = new ExtendableMap(); 019 aMap.addBidirectionalLink("A", "B", 5.0); 020 aMap.addBidirectionalLink("A", "C", 6.0); 021 aMap.addBidirectionalLink("B", "D", 4.0); 022 aMap.addBidirectionalLink("B", "E", 7.0); 023 aMap.addBidirectionalLink("D", "F", 4.0); 024 aMap.addBidirectionalLink("D", "G", 8.0); 025 026 envChanges = new StringBuffer(); 027 } 028 029 public void testAlreadyAtGoal() { 030 MapEnvironment me = new MapEnvironment(aMap); 031 OnlineDFSAgent agent = new OnlineDFSAgent(new BidirectionalMapProblem( 032 me.getMap(), "A", "A")); 033 me.addAgent(agent, "A"); 034 me.registerView(new BasicEnvironmentView() { 035 @Override 036 public void envChanged(String command) { 037 envChanges.append(command).append("->"); 038 } 039 }); 040 me.stepUntilDone(); 041 042 assertEquals("NoOP->", envChanges.toString()); 043 } 044 045 public void testNormalSearch() { 046 MapEnvironment me = new MapEnvironment(aMap); 047 OnlineDFSAgent agent = new OnlineDFSAgent(new BidirectionalMapProblem( 048 me.getMap(), "A", "G")); 049 me.addAgent(agent, "A"); 050 me.registerView(new BasicEnvironmentView() { 051 @Override 052 public void envChanged(String command) { 053 envChanges.append(command).append("->"); 054 } 055 }); 056 me.stepUntilDone(); 057 058 assertEquals("C->A->B->E->B->D->G->NoOP->", envChanges.toString()); 059 } 060 061 public void testNoPath() { 062 // TODO - The OnlineDFSAgent as it is currently written 063 // goes into a never ending loop if there is not goal! 064 // Need to fix. 065 // aMap = new Map(new String[] { "A", "B" }); 066 // aMap.addBidirectionalLink("A", "B", 1); 067 // MapEnvironment me = new MapEnvironment(aMap); 068 // OnlineDFSAgent agent = new OnlineDFSAgent(new 069 // BidirectionalMapProblem( 070 // me.getMap(), "A", "X")); 071 // me.addAgent(agent, "A"); 072 // me.registerView(new BasicEnvironmentView() { 073 // @Override 074 // public void envChanged(String command) { 075 // envChanges.append(command).append("->"); 076 // } 077 // }); 078 // me.stepUntilNoOp(); 079 // 080 // assertEquals("B->A->", envChanges.toString()); 081 } 082 083 public void testFig4_18() { 084 aMap = new ExtendableMap(); 085 aMap.addBidirectionalLink("1,1", "1,2", 1.0); 086 aMap.addBidirectionalLink("1,1", "2,1", 1.0); 087 aMap.addBidirectionalLink("1,2", "1,3", 1.0); 088 aMap.addBidirectionalLink("1,2", "2,2", 1.0); 089 aMap.addBidirectionalLink("1,3", "2,3", 1.0); 090 aMap.addBidirectionalLink("2,2", "3,2", 1.0); 091 aMap.addBidirectionalLink("2,3", "3,3", 1.0); 092 aMap.addBidirectionalLink("3,1", "3,2", 1.0); 093 094 MapEnvironment me = new MapEnvironment(aMap); 095 OnlineDFSAgent agent = new OnlineDFSAgent(new BidirectionalMapProblem( 096 me.getMap(), "1,1", "3,3")); 097 me.addAgent(agent, "1,1"); 098 me.registerView(new BasicEnvironmentView() { 099 @Override 100 public void envChanged(String command) { 101 envChanges.append(command).append("->"); 102 } 103 }); 104 me.stepUntilDone(); 105 106 assertEquals( 107 "2,1->1,1->1,2->2,2->3,2->3,1->3,2->2,2->1,2->1,3->2,3->3,3->NoOP->", 108 envChanges.toString()); 109 } 110 }