001 package aima.test.search.searches; 002 003 import java.util.List; 004 005 import junit.framework.TestCase; 006 007 import aima.search.framework.Problem; 008 import aima.search.framework.Search; 009 import aima.search.framework.SearchAgent; 010 import aima.search.framework.GraphSearch; 011 import aima.search.nqueens.NQueensBoard; 012 import aima.search.nqueens.NQueensGoalTest; 013 import aima.search.nqueens.NQueensSuccessorFunction; 014 import aima.search.uninformed.UniformCostSearch; 015 016 /** 017 * @author Ciaran O'Reilly 018 * 019 */ 020 public class UniformCostSearchTest extends TestCase { 021 022 public void testUniformCostSuccesfulSearch() throws Exception { 023 Problem problem = new Problem(new NQueensBoard(8), 024 new NQueensSuccessorFunction(), new NQueensGoalTest()); 025 Search search = new UniformCostSearch(new GraphSearch()); 026 SearchAgent agent = new SearchAgent(problem, search); 027 028 List actions = agent.getActions(); 029 030 assertEquals(8, actions.size()); 031 032 assertEquals("1965", agent.getInstrumentation().getProperty( 033 "nodesExpanded")); 034 035 assertEquals("8.0", agent.getInstrumentation().getProperty("pathCost")); 036 } 037 038 public void testUniformCostUnSuccesfulSearch() throws Exception { 039 Problem problem = new Problem(new NQueensBoard(3), 040 new NQueensSuccessorFunction(), new NQueensGoalTest()); 041 Search search = new UniformCostSearch(new GraphSearch()); 042 SearchAgent agent = new SearchAgent(problem, search); 043 044 List actions = agent.getActions(); 045 046 assertEquals(0, actions.size()); 047 048 assertEquals("6", agent.getInstrumentation().getProperty( 049 "nodesExpanded")); 050 051 // Will be 0 as did not reach goal state. 052 assertEquals("0", agent.getInstrumentation().getProperty("pathCost")); 053 } 054 }