Search in sources :

Example 1 with QueenAction

use of aima.core.environment.nqueens.QueenAction in project aima-java by aimacode.

the class NQueensSearchDemo method startExperiment.

public void startExperiment(SearchForActions<NQueensBoard, QueenAction> search) {
    search.addNodeListener(n -> notifyProgressTrackers(n.getState(), search.getMetrics()));
    Problem<NQueensBoard, QueenAction> problem;
    if (board.getNumberOfQueensOnBoard() == 0)
        problem = new GeneralProblem<>(board, NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    else
        problem = new GeneralProblem<>(board, NQueensFunctions::getCSFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    List<QueenAction> actions = search.findActions(problem);
    for (QueenAction action : actions) board = NQueensFunctions.getResult(board, action);
    notifyProgressTrackers(board, search.getMetrics());
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) QueenAction(aima.core.environment.nqueens.QueenAction) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem)

Example 2 with QueenAction

use of aima.core.environment.nqueens.QueenAction 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 3 with QueenAction

use of aima.core.environment.nqueens.QueenAction in project aima-java by aimacode.

the class NQueensFunctionsTest method testComplexBoardSuccessorGenerator.

@Test
public void testComplexBoardSuccessorGenerator() {
    List<QueenAction> actions = new ArrayList<>(actionsFn.apply(eightBoard));
    Assert.assertEquals(8, actions.size());
    NQueensBoard next = resultFn.apply(eightBoard, actions.get(0));
    Assert.assertEquals(1, next.getNumberOfQueensOnBoard());
    actions = new ArrayList<>(actionsFn.apply(next));
    Assert.assertEquals(6, actions.size());
}
Also used : QueenAction(aima.core.environment.nqueens.QueenAction) ArrayList(java.util.ArrayList) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) Test(org.junit.Test) GoalTest(aima.core.search.framework.problem.GoalTest)

Example 4 with QueenAction

use of aima.core.environment.nqueens.QueenAction in project aima-java by aimacode.

the class DepthFirstSearchTest method testDepthFirstSuccesfulSearch.

@Test
public void testDepthFirstSuccesfulSearch() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(8), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    SearchForActions<NQueensBoard, QueenAction> search = new DepthFirstSearch<>(new GraphSearch<>());
    List<QueenAction> actions = search.findActions(problem);
    assertCorrectPlacement(actions);
    Assert.assertEquals("113", search.getMetrics().get("nodesExpanded"));
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) QueenAction(aima.core.environment.nqueens.QueenAction) DepthFirstSearch(aima.core.search.uninformed.DepthFirstSearch) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test)

Example 5 with QueenAction

use of aima.core.environment.nqueens.QueenAction in project aima-java by aimacode.

the class DepthLimitedSearchTest method testFailure.

@Test
public void testFailure() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(3), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    DepthLimitedSearch<NQueensBoard, QueenAction> search = new DepthLimitedSearch<>(5);
    SearchAgent<NQueensBoard, QueenAction> agent = new SearchAgent<>(problem, search);
    List<Action> actions = agent.getActions();
    Assert.assertEquals(true, SearchUtils.isFailure(actions));
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) Action(aima.core.agent.Action) QueenAction(aima.core.environment.nqueens.QueenAction) QueenAction(aima.core.environment.nqueens.QueenAction) SearchAgent(aima.core.search.framework.SearchAgent) DepthLimitedSearch(aima.core.search.uninformed.DepthLimitedSearch) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test)

Aggregations

NQueensBoard (aima.core.environment.nqueens.NQueensBoard)13 QueenAction (aima.core.environment.nqueens.QueenAction)13 Test (org.junit.Test)12 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)11 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)11 Action (aima.core.agent.Action)5 SearchAgent (aima.core.search.framework.SearchAgent)5 GoalTest (aima.core.search.framework.problem.GoalTest)4 DepthLimitedSearch (aima.core.search.uninformed.DepthLimitedSearch)3 BreadthFirstSearch (aima.core.search.uninformed.BreadthFirstSearch)2 DepthFirstSearch (aima.core.search.uninformed.DepthFirstSearch)2 UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)2 ArrayList (java.util.ArrayList)2 IterativeDeepeningSearch (aima.core.search.uninformed.IterativeDeepeningSearch)1