Skip to content

Commit 85970bd

Browse files
authored
Add files via upload
1 parent 1ba5fee commit 85970bd

File tree

2 files changed

+331
-331
lines changed

2 files changed

+331
-331
lines changed
Lines changed: 132 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,132 @@
1-
package aima.gui.fx.demo.search;
2-
3-
import aima.core.environment.nqueens.NQueensBoard;
4-
import aima.core.environment.nqueens.NQueensBoard.Config;
5-
import aima.core.search.framework.Metrics;
6-
import aima.core.search.framework.qsearch.TreeSearch;
7-
import aima.core.search.uninformed.DepthFirstSearch;
8-
import aima.gui.fx.framework.IntegrableApplication;
9-
import aima.gui.fx.framework.Parameter;
10-
import aima.gui.fx.framework.SimulationPaneBuilder;
11-
import aima.gui.fx.framework.SimulationPaneCtrl;
12-
import aima.gui.fx.views.NQueensViewCtrl;
13-
import javafx.application.Platform;
14-
import javafx.scene.layout.BorderPane;
15-
import javafx.scene.layout.Pane;
16-
import javafx.scene.layout.StackPane;
17-
18-
19-
/**
20-
* Integrable application which demonstrates how different search strategies
21-
* solve the N-Queens problem.
22-
*
23-
* @author Ruediger Lunde
24-
*
25-
*/
26-
public class NQueensSearchApp extends IntegrableApplication {
27-
28-
public static void main(String[] args) {
29-
launch(args);
30-
}
31-
32-
public final static String PARAM_STRATEGY = "strategy";
33-
public final static String PARAM_BOARD_SIZE = "boardSize";
34-
public final static String PARAM_INIT_CONFIG = "initConfig";
35-
36-
private NQueensViewCtrl stateViewCtrl;
37-
private SimulationPaneCtrl simPaneCtrl;
38-
private NQueensSearchProg experiment;
39-
40-
public NQueensSearchApp() {
41-
experiment = new NQueensSearchProg();
42-
experiment.addProgressTracer(this::updateStateView);
43-
}
44-
45-
@Override
46-
public String getTitle() {
47-
return "N-Queens Search App";
48-
}
49-
50-
/**
51-
* Defines state view, parameters, and call-back functions and calls the
52-
* simulation pane builder to create layout and controller objects.
53-
*/
54-
@Override
55-
public Pane createRootPane() {
56-
BorderPane root = new BorderPane();
57-
58-
StackPane stateView = new StackPane();
59-
stateViewCtrl = new NQueensViewCtrl(stateView);
60-
61-
Parameter[] params = createParameters();
62-
63-
SimulationPaneBuilder builder = new SimulationPaneBuilder();
64-
builder.defineParameters(params);
65-
builder.defineStateView(stateView);
66-
builder.defineInitMethod(this::initialize);
67-
builder.defineSimMethod(this::simulate);
68-
simPaneCtrl = builder.getResultFor(root);
69-
70-
return root;
71-
}
72-
73-
protected Parameter[] createParameters() {
74-
Parameter p1 = new Parameter(PARAM_STRATEGY, "Depth-First Search", "Hill Climbing", "Simulated Annealing",
75-
"Genetic Algorithm");
76-
Parameter p2 = new Parameter(PARAM_BOARD_SIZE, 8, 16, 32, 64);
77-
Parameter p3 = new Parameter(PARAM_INIT_CONFIG, "FirstRow", "Random");
78-
p3.setDependency(PARAM_STRATEGY, "Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
79-
return new Parameter[] {p1, p2, p3};
80-
}
81-
82-
/** Displays the selected function on the state view. */
83-
@Override
84-
public void initialize() {
85-
experiment.setBoardSize(simPaneCtrl.getParamAsInt(PARAM_BOARD_SIZE));
86-
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
87-
Config config;
88-
if (strategy.equals("Depth-First Search") || strategy.equals("Genetic Algorithm"))
89-
config = Config.EMPTY;
90-
else if (simPaneCtrl.getParamValue(PARAM_INIT_CONFIG).equals("Random"))
91-
config = Config.QUEEN_IN_EVERY_COL;
92-
else
93-
config = Config.QUEENS_IN_FIRST_ROW;
94-
experiment.initExperiment(config);
95-
stateViewCtrl.updateBoard(experiment.getBoard());
96-
}
97-
98-
@Override
99-
public void finalize() {
100-
simPaneCtrl.cancelSimulation();
101-
}
102-
103-
/** Starts the experiment. */
104-
public void simulate() {
105-
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
106-
if (strategy.equals("Depth-First Search"))
107-
experiment.startExperiment(new DepthFirstSearch(new TreeSearch()));
108-
else if (strategy.equals("Hill Climbing"))
109-
experiment.startHillClimbingExperiment();
110-
else if (strategy.equals("Simulated Annealing"))
111-
experiment.startSimulatedAnnealingExperiment();
112-
else if (strategy.equals("Genetic Algorithm"))
113-
experiment.startGenAlgoExperiment(simPaneCtrl.getParamValue(PARAM_INIT_CONFIG).equals("Random"));
114-
}
115-
116-
/**
117-
* Caution: While the background thread should be slowed down, updates of
118-
* the GUI have to be done in the GUI thread!
119-
*/
120-
private void updateStateView(NQueensBoard board, Metrics metrics) {
121-
Platform.runLater(() -> updateStateViewLater(board, metrics));
122-
simPaneCtrl.waitAfterStep();
123-
}
124-
125-
/**
126-
* Must be called by the GUI thread!
127-
*/
128-
private void updateStateViewLater(NQueensBoard board, Metrics metrics) {
129-
stateViewCtrl.updateBoard(board);
130-
simPaneCtrl.setStatus(metrics.toString());
131-
}
132-
}
1+
package aima.gui.fx.demo.search;
2+
3+
import aima.core.environment.nqueens.NQueensBoard;
4+
import aima.core.environment.nqueens.NQueensBoard.Config;
5+
import aima.core.search.framework.Metrics;
6+
import aima.core.search.framework.qsearch.TreeSearch;
7+
import aima.core.search.uninformed.DepthFirstSearch;
8+
import aima.gui.fx.framework.IntegrableApplication;
9+
import aima.gui.fx.framework.Parameter;
10+
import aima.gui.fx.framework.SimulationPaneBuilder;
11+
import aima.gui.fx.framework.SimulationPaneCtrl;
12+
import aima.gui.fx.views.NQueensViewCtrl;
13+
import javafx.application.Platform;
14+
import javafx.scene.layout.BorderPane;
15+
import javafx.scene.layout.Pane;
16+
import javafx.scene.layout.StackPane;
17+
18+
19+
/**
20+
* Integrable application which demonstrates how different search strategies
21+
* solve the N-Queens problem.
22+
*
23+
* @author Ruediger Lunde
24+
*
25+
*/
26+
public class NQueensSearchApp extends IntegrableApplication {
27+
28+
public static void main(String[] args) {
29+
launch(args);
30+
}
31+
32+
public final static String PARAM_STRATEGY = "strategy";
33+
public final static String PARAM_BOARD_SIZE = "boardSize";
34+
public final static String PARAM_INIT_CONFIG = "initConfig";
35+
36+
private NQueensViewCtrl stateViewCtrl;
37+
private SimulationPaneCtrl simPaneCtrl;
38+
private NQueensSearchProg experiment;
39+
40+
public NQueensSearchApp() {
41+
experiment = new NQueensSearchProg();
42+
experiment.addProgressTracer(this::updateStateView);
43+
}
44+
45+
@Override
46+
public String getTitle() {
47+
return "N-Queens Search App";
48+
}
49+
50+
/**
51+
* Defines state view, parameters, and call-back functions and calls the
52+
* simulation pane builder to create layout and controller objects.
53+
*/
54+
@Override
55+
public Pane createRootPane() {
56+
BorderPane root = new BorderPane();
57+
58+
StackPane stateView = new StackPane();
59+
stateViewCtrl = new NQueensViewCtrl(stateView);
60+
61+
Parameter[] params = createParameters();
62+
63+
SimulationPaneBuilder builder = new SimulationPaneBuilder();
64+
builder.defineParameters(params);
65+
builder.defineStateView(stateView);
66+
builder.defineInitMethod(this::initialize);
67+
builder.defineSimMethod(this::simulate);
68+
simPaneCtrl = builder.getResultFor(root);
69+
70+
return root;
71+
}
72+
73+
protected Parameter[] createParameters() {
74+
Parameter p1 = new Parameter(PARAM_STRATEGY, "Depth-First Search", "Hill Climbing", "Simulated Annealing",
75+
"Genetic Algorithm");
76+
Parameter p2 = new Parameter(PARAM_BOARD_SIZE, 8, 16, 32, 64);
77+
Parameter p3 = new Parameter(PARAM_INIT_CONFIG, "FirstRow", "Random");
78+
p3.setDependency(PARAM_STRATEGY, "Hill Climbing", "Simulated Annealing", "Genetic Algorithm");
79+
return new Parameter[] {p1, p2, p3};
80+
}
81+
82+
/** Displays the selected function on the state view. */
83+
@Override
84+
public void initialize() {
85+
experiment.setBoardSize(simPaneCtrl.getParamAsInt(PARAM_BOARD_SIZE));
86+
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
87+
Config config;
88+
if (strategy.equals("Depth-First Search") || strategy.equals("Genetic Algorithm"))
89+
config = Config.EMPTY;
90+
else if (simPaneCtrl.getParamValue(PARAM_INIT_CONFIG).equals("Random"))
91+
config = Config.QUEEN_IN_EVERY_COL;
92+
else
93+
config = Config.QUEENS_IN_FIRST_ROW;
94+
experiment.initExperiment(config);
95+
stateViewCtrl.updateBoard(experiment.getBoard());
96+
}
97+
98+
@Override
99+
public void finalize() {
100+
simPaneCtrl.cancelSimulation();
101+
}
102+
103+
/** Starts the experiment. */
104+
public void simulate() {
105+
Object strategy = simPaneCtrl.getParamValue(PARAM_STRATEGY);
106+
if (strategy.equals("Depth-First Search"))
107+
experiment.startExperiment(new DepthFirstSearch(new TreeSearch()));
108+
else if (strategy.equals("Hill Climbing"))
109+
experiment.startHillClimbingExperiment();
110+
else if (strategy.equals("Simulated Annealing"))
111+
experiment.startSimulatedAnnealingExperiment();
112+
else if (strategy.equals("Genetic Algorithm"))
113+
experiment.startGenAlgoExperiment(simPaneCtrl.getParamValue(PARAM_INIT_CONFIG).equals("Random"));
114+
}
115+
116+
/**
117+
* Caution: While the background thread should be slowed down, updates of
118+
* the GUI have to be done in the GUI thread!
119+
*/
120+
private void updateStateView(NQueensBoard board, Metrics metrics) {
121+
Platform.runLater(() -> updateStateViewLater(board, metrics));
122+
simPaneCtrl.waitAfterStep();
123+
}
124+
125+
/**
126+
* Must be called by the GUI thread!
127+
*/
128+
private void updateStateViewLater(NQueensBoard board, Metrics metrics) {
129+
stateViewCtrl.updateBoard(board);
130+
simPaneCtrl.setStatus(metrics.toString());
131+
}
132+
}

0 commit comments

Comments
 (0)