Search in sources :

Example 1 with NQueensFunctions

use of aima.core.environment.nqueens.NQueensFunctions 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 NQueensFunctions

use of aima.core.environment.nqueens.NQueensFunctions 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 NQueensFunctions

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

the class NQueensFunctionsTest method setUp.

@Before
public void setUp() {
    oneBoard = new NQueensBoard(1);
    eightBoard = new NQueensBoard(8);
    board = new NQueensBoard(8);
    actionsFn = NQueensFunctions::getIFActions;
    resultFn = NQueensFunctions::getResult;
    goalTest = NQueensFunctions::testGoal;
}
Also used : NQueensFunctions(aima.core.environment.nqueens.NQueensFunctions) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) Before(org.junit.Before)

Example 4 with NQueensFunctions

use of aima.core.environment.nqueens.NQueensFunctions 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 NQueensFunctions

use of aima.core.environment.nqueens.NQueensFunctions 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)12 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)12 QueenAction (aima.core.environment.nqueens.QueenAction)11 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)11 Test (org.junit.Test)10 Action (aima.core.agent.Action)5 SearchAgent (aima.core.search.framework.SearchAgent)5 DepthLimitedSearch (aima.core.search.uninformed.DepthLimitedSearch)3 GoalTest (aima.core.search.framework.problem.GoalTest)2 BreadthFirstSearch (aima.core.search.uninformed.BreadthFirstSearch)2 DepthFirstSearch (aima.core.search.uninformed.DepthFirstSearch)2 UniformCostSearch (aima.core.search.uninformed.UniformCostSearch)2 IterativeDeepeningSearch (aima.core.search.uninformed.IterativeDeepeningSearch)1 Before (org.junit.Before)1