Skip to content

Commit fb0de40

Browse files
committed
N-Queens App extended (more search strategies included).
1 parent f31e959 commit fb0de40

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

aima-gui/src/main/java/aima/gui/fx/demo/search/NQueensSearchApp.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import aima.core.search.framework.qsearch.GraphSearch;
88
import aima.core.search.framework.qsearch.TreeSearch;
99
import aima.core.search.informed.AStarSearch;
10+
import aima.core.search.informed.GreedyBestFirstSearch;
11+
import aima.core.search.uninformed.BreadthFirstSearch;
1012
import aima.core.search.uninformed.DepthFirstSearch;
13+
import aima.core.search.uninformed.IterativeDeepeningSearch;
1114
import aima.gui.fx.framework.IntegrableApplication;
1215
import aima.gui.fx.framework.Parameter;
1316
import aima.gui.fx.framework.SimulationPaneBuilder;
@@ -71,16 +74,22 @@ public Pane createRootPane() {
7174
builder.defineInitMethod(this::initialize);
7275
builder.defineSimMethod(this::simulate);
7376
simPaneCtrl = builder.getResultFor(root);
77+
simPaneCtrl.setParam(SimulationPaneCtrl.PARAM_SIM_SPEED, 1);
7478

7579
return root;
7680
}
7781

7882
protected Parameter[] createParameters() {
79-
Parameter p1 = new Parameter(PARAM_STRATEGY, "Depth-First Search", "A* search (attacking pair heuristic)",
80-
"Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
81-
Parameter p2 = new Parameter(PARAM_BOARD_SIZE, 8, 16, 32, 64);
83+
Parameter p1 = new Parameter(PARAM_STRATEGY, "Depth-First Search", "Breadth-First Search",
84+
"Iterative Deepening Search",
85+
"Greedy Best-First Search (attacking pair heuristic)", "A* search (attacking pair heuristic)",
86+
"Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
87+
Parameter p2 = new Parameter(PARAM_BOARD_SIZE, 4, 8, 16, 32, 64);
88+
p2.setDefaultValueIndex(1);
8289
Parameter p3 = new Parameter(PARAM_INIT_CONFIG, "FirstRow", "Random");
83-
p3.setDependency(PARAM_STRATEGY, "Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
90+
p3.setDependency(PARAM_STRATEGY, "Iterative Deepening Search",
91+
"Greedy Best-First Search (attacking pair heuristic)", "A* search (attacking pair heuristic)",
92+
"Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
8493
return new Parameter[] {p1, p2, p3};
8594
}
8695

@@ -90,7 +99,7 @@ public void initialize() {
9099
experiment.setBoardSize(simPaneCtrl.getParamAsInt(PARAM_BOARD_SIZE));
91100
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
92101
Config config;
93-
if (Arrays.asList("Depth-First Search", "A* search (attacking pair heuristic)", "Genetic Algorithm")
102+
if (Arrays.asList("Depth-First Search", "Breadth-First Search", "Genetic Algorithm")
94103
.contains(strategy))
95104
config = Config.EMPTY;
96105
else if (simPaneCtrl.getParamValue(PARAM_INIT_CONFIG).equals("Random"))
@@ -111,6 +120,12 @@ public void simulate() {
111120
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
112121
if (strategy.equals("Depth-First Search"))
113122
experiment.startExperiment(new DepthFirstSearch(new TreeSearch()));
123+
else if (strategy.equals("Breadth-First Search"))
124+
experiment.startExperiment(new BreadthFirstSearch(new TreeSearch()));
125+
else if (strategy.equals("Iterative Deepening Search"))
126+
experiment.startExperiment(new IterativeDeepeningSearch());
127+
else if (strategy.equals("Greedy Best-First Search (attacking pair heuristic)"))
128+
experiment.startExperiment(new GreedyBestFirstSearch(new GraphSearch(), new AttackingPairsHeuristic()));
114129
else if (strategy.equals("A* search (attacking pair heuristic)"))
115130
experiment.startExperiment(new AStarSearch(new GraphSearch(), new AttackingPairsHeuristic()));
116131
else if (strategy.equals("Hill Climbing"))

aima-gui/src/main/java/aima/gui/fx/demo/search/NQueensSearchProg.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ public void startExperiment(SearchForActions search) {
9292
search.getNodeExpander()
9393
.addNodeListener(n -> notifyProgressTracers((NQueensBoard) n.getState(), search.getMetrics()));
9494

95-
Problem problem = new Problem(board, NQueensFunctionFactory.getIActionsFunction(),
96-
NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest());
95+
Problem problem = null;
96+
if (board.getNumberOfQueensOnBoard() == 0)
97+
problem = new Problem(board, NQueensFunctionFactory.getIActionsFunction(),
98+
NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest());
99+
else
100+
problem = new Problem(board, NQueensFunctionFactory.getCActionsFunction(),
101+
NQueensFunctionFactory.getResultFunction(), new NQueensGoalTest());
97102
List<Action> actions = search.search(problem);
98103
for (Action action : actions)
99104
board = (NQueensBoard) NQueensFunctionFactory.getResultFunction().result(board, action);

0 commit comments

Comments
 (0)