Search in sources :

Example 1 with NQueensCSP

use of aima.core.search.csp.examples.NQueensCSP in project aima-java by aimacode.

the class NQueensCspDemo method main.

public static void main(String[] args) {
    int size = 64;
    CSP<Variable, Integer> csp = new NQueensCSP(size);
    CspListener.StepCounter<Variable, Integer> stepCounter = new CspListener.StepCounter<>();
    CspSolver<Variable, Integer> solver;
    System.out.println(size + "-Queens (Min-Conflicts)");
    solver = new MinConflictsSolver<>(1000);
    solver.addCspListener(stepCounter);
    stepCounter.reset();
    Assignment<Variable, Integer> sol = solver.solve(csp);
    System.out.println((sol.isSolution(csp) ? ":-) " : ":-( ") + sol);
    System.out.println(stepCounter.getResults() + "\n");
    System.out.println(size + "-Queens (Backtracking + MRV & DEG + LCV + AC3)");
    solver = new FlexibleBacktrackingSolver<Variable, Integer>().setAll();
    solver.addCspListener(stepCounter);
    stepCounter.reset();
    System.out.println(solver.solve(csp));
    System.out.println(stepCounter.getResults() + "\n");
    size = 16;
    csp = new NQueensCSP(size);
    System.out.println(size + "-Queens (Backtracking)");
    solver = new FlexibleBacktrackingSolver<>();
    solver.addCspListener(stepCounter);
    stepCounter.reset();
    System.out.println(solver.solve(csp));
    System.out.println(stepCounter.getResults() + "\n");
}
Also used : NQueensCSP(aima.core.search.csp.examples.NQueensCSP)

Example 2 with NQueensCSP

use of aima.core.search.csp.examples.NQueensCSP 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

NQueensCSP (aima.core.search.csp.examples.NQueensCSP)2 NQueensBoard (aima.core.environment.nqueens.NQueensBoard)1