Search in sources :

Example 11 with NQueensBoard

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

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

Example 13 with NQueensBoard

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

the class DepthLimitedSearchTest method testSuccessfulDepthLimitedSearch.

@Test
public void testSuccessfulDepthLimitedSearch() throws Exception {
    Problem<NQueensBoard, QueenAction> problem = new GeneralProblem<>(new NQueensBoard(8), NQueensFunctions::getIFActions, NQueensFunctions::getResult, NQueensFunctions::testGoal);
    SearchForActions<NQueensBoard, QueenAction> search = new DepthLimitedSearch<>(8);
    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) DepthLimitedSearch(aima.core.search.uninformed.DepthLimitedSearch) NQueensBoard(aima.core.environment.nqueens.NQueensBoard) GeneralProblem(aima.core.search.framework.problem.GeneralProblem) Test(org.junit.Test)

Example 14 with NQueensBoard

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

the class NQueensFunctionsTest method testSimpleBoardSuccessorGenerator.

@Test
public void testSimpleBoardSuccessorGenerator() {
    List<QueenAction> actions = new ArrayList<>(actionsFn.apply(oneBoard));
    Assert.assertEquals(1, actions.size());
    NQueensBoard next = resultFn.apply(oneBoard, actions.get(0));
    Assert.assertEquals(1, next.getNumberOfQueensOnBoard());
}
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 15 with NQueensBoard

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

the class NQueensCspApp method initialize.

/**
     * Displays the initialized board on the state view.
     */
@Override
public void initialize() {
    csp = new NQueensCSP(simPaneCtrl.getParamAsInt(PARAM_BOARD_SIZE));
    Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
    if (strategy.equals("Backtracking")) {
        FlexibleBacktrackingSolver<Variable, Integer> bSolver = new FlexibleBacktrackingSolver<>();
        switch((String) simPaneCtrl.getParamValue(PARAM_VAR_SELECT)) {
            case "MRV":
                bSolver.set(CspHeuristics.mrv());
                break;
            case "DEG":
                bSolver.set(CspHeuristics.deg());
                break;
            case "MRV&DEG":
                bSolver.set(CspHeuristics.mrvDeg());
                break;
        }
        switch((String) simPaneCtrl.getParamValue(PARAM_VAL_SELECT)) {
            case "LCV":
                bSolver.set(CspHeuristics.lcv());
                break;
        }
        switch((String) simPaneCtrl.getParamValue(PARAM_INFERENCE)) {
            case "Forward Checking":
                bSolver.set(new ForwardCheckingStrategy<>());
                break;
            case "AC3":
                bSolver.set(new AC3Strategy<>());
                break;
        }
        solver = bSolver;
    } else if (strategy.equals("Min-Conflicts")) {
        solver = new MinConflictsSolver<>(1000);
    }
    solver.addCspListener(stepCounter);
    solver.addCspListener((csp, assign, var) -> {
        if (assign != null)
            updateStateView(getBoard(assign));
    });
    stepCounter.reset();
    stateViewCtrl.update(new NQueensBoard(csp.getVariables().size()));
    simPaneCtrl.setStatus("");
}
Also used : NQueensCSP(aima.core.search.csp.examples.NQueensCSP) NQueensBoard(aima.core.environment.nqueens.NQueensBoard)

Aggregations

NQueensBoard (aima.core.environment.nqueens.NQueensBoard)23 Test (org.junit.Test)16 QueenAction (aima.core.environment.nqueens.QueenAction)13 NQueensFunctions (aima.core.environment.nqueens.NQueensFunctions)12 GeneralProblem (aima.core.search.framework.problem.GeneralProblem)11 GoalTest (aima.core.search.framework.problem.GoalTest)6 Action (aima.core.agent.Action)5 SearchAgent (aima.core.search.framework.SearchAgent)5 XYLocation (aima.core.util.datastructure.XYLocation)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 NQueensCSP (aima.core.search.csp.examples.NQueensCSP)1 IterativeDeepeningSearch (aima.core.search.uninformed.IterativeDeepeningSearch)1 Before (org.junit.Before)1