Search in sources :

Example 1 with SearchAgent

use of aima.core.search.framework.SearchAgent in project aima-java by aimacode.

the class UniformCostSearchTest method testUniformCostSuccesfulSearch.

@Test
public void testUniformCostSuccesfulSearch() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(8), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    SearchForActions<NQueensBoard, QueenAction> search = new UniformCostSearch<>();
    SearchAgent<NQueensBoard, QueenAction> 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"));
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) Action(aima.core.agent.Action) QueenAction(aima.core.environment.nqueens.QueenAction) UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) QueenAction(aima.core.environment.nqueens.QueenAction) SearchAgent(aima.core.search.framework.SearchAgent) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 2 with SearchAgent

use of aima.core.search.framework.SearchAgent in project aima-java by aimacode.

the class SolutionTesterTest method testMultiGoalProblem.

@Test
public void testMultiGoalProblem() throws Exception {
    Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
    Problem<String, MoveToAction> problem = new GeneralProblem<String, MoveToAction>(SimplifiedRoadMapOfPartOfRomania.ARAD, MapFunctions.createActionsFunction(romaniaMap), MapFunctions.createResultFunction(), GoalTest.<String>isEqual(SimplifiedRoadMapOfPartOfRomania.BUCHAREST).or(GoalTest.isEqual(SimplifiedRoadMapOfPartOfRomania.HIRSOVA)), MapFunctions.createDistanceStepCostFunction(romaniaMap)) {

        @Override
        public boolean testSolution(Node<String, MoveToAction> node) {
            return testGoal(node.getState()) && node.getPathCost() > 550;
        // accept paths to goal only if their costs are above 550
        }
    };
    SearchForActions<String, MoveToAction> search = new UniformCostSearch<>(new GraphSearch<>());
    SearchAgent<String, MoveToAction> agent = new SearchAgent<>(problem, search);
    Assert.assertEquals("[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==RimnicuVilcea], Action[name==moveTo, location==Pitesti], Action[name==moveTo, location==Bucharest], Action[name==moveTo, location==Urziceni], Action[name==moveTo, location==Hirsova]]", agent.getActions().toString());
    Assert.assertEquals(6, agent.getActions().size());
    Assert.assertEquals("15", agent.getInstrumentation().getProperty("nodesExpanded"));
    Assert.assertEquals("1", agent.getInstrumentation().getProperty("queueSize"));
    Assert.assertEquals("5", agent.getInstrumentation().getProperty("maxQueueSize"));
}
Also used : UniformCostSearch(aima.core.search.uninformed.UniformCostSearch) SimplifiedRoadMapOfPartOfRomania(aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania) SearchAgent(aima.core.search.framework.SearchAgent) Node(aima.core.search.framework.Node) Map(aima.core.environment.map.Map) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) MoveToAction(aima.core.environment.map.MoveToAction) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 3 with SearchAgent

use of aima.core.search.framework.SearchAgent in project aima-java by aimacode.

the class AStarSearchTest method testAStarSearch.

@Test
public void testAStarSearch() {
    // Thoughtworks and Xin Lu of UCI
    try {
        // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
        // {2,0,5,6,4,8,3,7,1});
        // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
        // {0,8,7,6,5,4,3,2,1});
        EightPuzzleBoard board = new EightPuzzleBoard(new int[] { 7, 1, 8, 0, 4, 6, 2, 3, 5 });
        Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(board);
        SearchForActions<EightPuzzleBoard, Action> search = new AStarSearch<>(new GraphSearch<>(), EightPuzzleFunctions.createManhattanHeuristicFunction());
        SearchAgent<EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
        Assert.assertEquals(23, agent.getActions().size());
        // "926" GraphSearchReduced Frontier
        Assert.assertEquals(// "926" GraphSearchReduced Frontier
        "1133", agent.getInstrumentation().getProperty("nodesExpanded"));
        // "534" GraphSearchReduced Frontier
        Assert.assertEquals(// "534" GraphSearchReduced Frontier
        "676", agent.getInstrumentation().getProperty("queueSize"));
        // "535" GraphSearchReduced Frontier
        Assert.assertEquals(// "535" GraphSearchReduced Frontier
        "677", agent.getInstrumentation().getProperty("maxQueueSize"));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception thrown");
    }
}
Also used : AStarSearch(aima.core.search.informed.AStarSearch) Action(aima.core.agent.Action) EightPuzzleBoard(aima.core.environment.eightpuzzle.EightPuzzleBoard) SearchAgent(aima.core.search.framework.SearchAgent) BidirectionalEightPuzzleProblem(aima.core.environment.eightpuzzle.BidirectionalEightPuzzleProblem) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 4 with SearchAgent

use of aima.core.search.framework.SearchAgent in project aima-java by aimacode.

the class GreedyBestFirstSearchTest method testGreedyBestFirstSearch.

@Test
public void testGreedyBestFirstSearch() {
    try {
        // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
        // {2,0,5,6,4,8,3,7,1});
        // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
        // {0,8,7,6,5,4,3,2,1});
        EightPuzzleBoard board = new EightPuzzleBoard(new int[] { 7, 1, 8, 0, 4, 6, 2, 3, 5 });
        Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(board);
        SearchForActions<EightPuzzleBoard, Action> search = new GreedyBestFirstSearch<>(new GraphSearch<>(), EightPuzzleFunctions.createManhattanHeuristicFunction());
        SearchAgent<EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
        // GraphSearchReducedFrontier: "49"
        Assert.assertEquals(49, agent.getActions().size());
        // GraphSearchReducedFrontier: "197"
        Assert.assertEquals(// GraphSearchReducedFrontier: "197"
        "332", agent.getInstrumentation().getProperty("nodesExpanded"));
        // GraphSearchReducedFrontier: "140"
        Assert.assertEquals(// GraphSearchReducedFrontier: "140"
        "241", agent.getInstrumentation().getProperty("queueSize"));
        // GraphSearchReducedFrontier: "141"
        Assert.assertEquals(// GraphSearchReducedFrontier: "141"
        "242", agent.getInstrumentation().getProperty("maxQueueSize"));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception thrown.");
    }
}
Also used : Action(aima.core.agent.Action) MoveToAction(aima.core.environment.map.MoveToAction) EightPuzzleBoard(aima.core.environment.eightpuzzle.EightPuzzleBoard) SearchAgent(aima.core.search.framework.SearchAgent) BidirectionalEightPuzzleProblem(aima.core.environment.eightpuzzle.BidirectionalEightPuzzleProblem) GreedyBestFirstSearch(aima.core.search.informed.GreedyBestFirstSearch) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 5 with SearchAgent

use of aima.core.search.framework.SearchAgent in project aima-java by aimacode.

the class GreedyBestFirstSearchTest method testAIMA3eFigure3_23_using_GraphSearch.

@Test
public void testAIMA3eFigure3_23_using_GraphSearch() throws Exception {
    Map romaniaMap = new SimplifiedRoadMapOfPartOfRomania();
    Problem<String, MoveToAction> problem = new GeneralProblem<>(SimplifiedRoadMapOfPartOfRomania.ARAD, MapFunctions.createActionsFunction(romaniaMap), MapFunctions.createResultFunction(), GoalTest.isEqual(SimplifiedRoadMapOfPartOfRomania.BUCHAREST), MapFunctions.createDistanceStepCostFunction(romaniaMap));
    SearchForActions<String, MoveToAction> search = new GreedyBestFirstSearch<>(new GraphSearch<>(), MapFunctions.createSLDHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
    SearchAgent<String, MoveToAction> agent = new SearchAgent<>(problem, search);
    Assert.assertEquals("[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==Fagaras], Action[name==moveTo, location==Bucharest]]", agent.getActions().toString());
    Assert.assertEquals(3, agent.getActions().size());
    Assert.assertEquals("3", agent.getInstrumentation().getProperty("nodesExpanded"));
    Assert.assertEquals("4", agent.getInstrumentation().getProperty("queueSize"));
    Assert.assertEquals("5", agent.getInstrumentation().getProperty("maxQueueSize"));
}
Also used : SimplifiedRoadMapOfPartOfRomania(aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania) SearchAgent(aima.core.search.framework.SearchAgent) GreedyBestFirstSearch(aima.core.search.informed.GreedyBestFirstSearch) Map(aima.core.environment.map.Map) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) MoveToAction(aima.core.environment.map.MoveToAction) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Aggregations

SearchAgent (aima.core.search.framework.SearchAgent)11 Test (org.junit.Test)11 Action (aima.core.agent.Action)8 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)8 GoalTest (aima.core.search.framework.problem.GoalTest)8 MoveToAction (aima.core.environment.map.MoveToAction)5 NQueensBoard (aima.core.environment.nqueens.NQueensBoard)5 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)5 QueenAction (aima.core.environment.nqueens.QueenAction)5 GreedyBestFirstSearch (aima.core.search.informed.GreedyBestFirstSearch)4 BidirectionalEightPuzzleProblem (aima.core.environment.eightpuzzle.BidirectionalEightPuzzleProblem)3 EightPuzzleBoard (aima.core.environment.eightpuzzle.EightPuzzleBoard)3 Map (aima.core.environment.map.Map)3 SimplifiedRoadMapOfPartOfRomania (aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania)3 UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)3 Node (aima.core.search.framework.Node)1 AStarSearch (aima.core.search.informed.AStarSearch)1 BreadthFirstSearch (aima.core.search.uninformed.BreadthFirstSearch)1 DepthFirstSearch (aima.core.search.uninformed.DepthFirstSearch)1 DepthLimitedSearch (aima.core.search.uninformed.DepthLimitedSearch)1