@Test public void testCheckFrontierPathCost() throws Exception { ExtendableMap map = new ExtendableMap(); map.addBidirectionalLink("start", "b", 2.5); map.addBidirectionalLink("start", "c", 1.0); map.addBidirectionalLink("b", "d", 2.0); map.addBidirectionalLink("c", "d", 4.0); map.addBidirectionalLink("c", "e", 1.0); map.addBidirectionalLink("d", "goal", 1.0); map.addBidirectionalLink("e", "goal", 5.0); Problem problem = new Problem( "start", MapFunctionFactory.getActionsFunction(map), MapFunctionFactory.getResultFunction(), new DefaultGoalTest("goal"), new MapStepCostFunction(map)); Search search = new UniformCostSearch(); SearchAgent agent = new SearchAgent(problem, search); List<Action> actions = agent.getActions(); Assert.assertEquals( "[Action[name==moveTo, location==b], Action[name==moveTo, location==d], Action[name==moveTo, location==goal]]", actions.toString()); Assert.assertEquals("5.5", search.getMetrics().get(QueueSearch.METRIC_PATH_COST)); }
private static void nQueensWithDepthFirstSearch() { System.out.println("\nNQueensDemo DFS -->"); try { Problem problem = new Problem( new NQueensBoard(_boardSize), NQueensFunctionFactory.getIActionsFunction(), NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest()); Search search = new DepthFirstSearch(new GraphSearch()); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); } catch (Exception e) { e.printStackTrace(); } }
private static void nQueensWithBreadthFirstSearch() { try { System.out.println("\nNQueensDemo BFS -->"); Problem problem = new Problem( new NQueensBoard(_boardSize), NQueensFunctionFactory.getIActionsFunction(), NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest()); Search search = new BreadthFirstSearch(new TreeSearch()); SearchAgent agent2 = new SearchAgent(problem, search); printActions(agent2.getActions()); printInstrumentation(agent2.getInstrumentation()); } catch (Exception e1) { e1.printStackTrace(); } }
@Test public void testUniformCostSuccesfulSearch() throws Exception { Problem problem = new Problem( new NQueensBoard(8), NQueensFunctionFactory.getIActionsFunction(), NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest()); Search search = new UniformCostSearch(); SearchAgent agent = new SearchAgent(problem, search); List<Action> actions = agent.getActions(); Assert.assertEquals(8, actions.size()); Assert.assertEquals("1965", agent.getInstrumentation().getProperty("nodesExpanded")); Assert.assertEquals("8.0", agent.getInstrumentation().getProperty("pathCost")); }
@Test public void testAIMA3eFigure3_15() throws Exception { Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania(); Problem problem = new Problem( SimplifiedRoadMapOfPartOfRomania.SIBIU, MapFunctionFactory.getActionsFunction(romaniaMap), MapFunctionFactory.getResultFunction(), new DefaultGoalTest(SimplifiedRoadMapOfPartOfRomania.BUCHAREST), new MapStepCostFunction(romaniaMap)); Search search = new UniformCostSearch(); SearchAgent agent = new SearchAgent(problem, search); List<Action> actions = agent.getActions(); Assert.assertEquals( "[Action[name==moveTo, location==RimnicuVilcea], Action[name==moveTo, location==Pitesti], Action[name==moveTo, location==Bucharest]]", actions.toString()); Assert.assertEquals("278.0", search.getMetrics().get(QueueSearch.METRIC_PATH_COST)); }
private static void nQueensHillClimbingSearch() { System.out.println("\nNQueensDemo HillClimbing -->"); try { Problem problem = new Problem( new NQueensBoard(_boardSize), NQueensFunctionFactory.getIActionsFunction(), NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest()); HillClimbingSearch search = new HillClimbingSearch(new AttackingPairsHeuristic()); SearchAgent agent = new SearchAgent(problem, search); System.out.println(); printActions(agent.getActions()); System.out.println("Search Outcome=" + search.getOutcome()); System.out.println("Final State=\n" + search.getLastSearchState()); printInstrumentation(agent.getInstrumentation()); } catch (Exception e) { e.printStackTrace(); } }