-
Notifications
You must be signed in to change notification settings - Fork 790
/
EightPuzzleDemo.java
150 lines (133 loc) · 5.91 KB
/
EightPuzzleDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package aima.gui.demo.search;
import aima.core.agent.Action;
import aima.core.environment.eightpuzzle.*;
import aima.core.search.agent.SearchAgent;
import aima.core.search.framework.SearchForActions;
import aima.core.search.framework.problem.Problem;
import aima.core.search.framework.qsearch.GraphSearch;
import aima.core.search.informed.AStarSearch;
import aima.core.search.informed.GreedyBestFirstSearch;
import aima.core.search.local.SimulatedAnnealingSearch;
import aima.core.search.uninformed.DepthLimitedSearch;
import aima.core.search.uninformed.IterativeDeepeningSearch;
import java.util.List;
import java.util.Properties;
/**
* @author Ravi Mohan
* @author Ruediger Lunde
*
*/
public class EightPuzzleDemo {
private static EightPuzzleBoard boardWithThreeMoveSolution =
new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });
private static EightPuzzleBoard random1 =
new EightPuzzleBoard(new int[] { 1, 4, 2, 7, 5, 8, 3, 0, 6 });
// private static EightPuzzleBoard extreme =
// new EightPuzzleBoard(new int[] { 0, 8, 7, 6, 5, 4, 3, 2, 1 });
public static void main(String[] args) {
System.out.println("Initial State:\n" + boardWithThreeMoveSolution);
eightPuzzleDLSDemo();
eightPuzzleIDLSDemo();
eightPuzzleGreedyBestFirstDemo();
eightPuzzleGreedyBestFirstManhattanDemo();
eightPuzzleAStarDemo();
eightPuzzleAStarManhattanDemo();
eightPuzzleSimulatedAnnealingDemo();
}
private static void eightPuzzleDLSDemo() {
System.out.println("\nEightPuzzleDemo recursive DLS (9)");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(boardWithThreeMoveSolution);
SearchForActions<EightPuzzleBoard, Action> search = new DepthLimitedSearch<>(9);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleIDLSDemo() {
System.out.println("\nEightPuzzleDemo Iterative DLS");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(random1);
SearchForActions<EightPuzzleBoard, Action> search = new IterativeDeepeningSearch<>();
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstDemo() {
System.out.println("\nEightPuzzleDemo Greedy Best First Search (MisplacedTileHeursitic)");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(boardWithThreeMoveSolution);
SearchForActions<EightPuzzleBoard, Action> search = new GreedyBestFirstSearch<>
(new GraphSearch<>(), EightPuzzleFunctions::getNumberOfMisplacedTiles);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstManhattanDemo() {
System.out.println("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(boardWithThreeMoveSolution);
SearchForActions<EightPuzzleBoard, Action> search = new GreedyBestFirstSearch<>
(new GraphSearch<>(), EightPuzzleFunctions::getManhattanDistance);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarDemo() {
System.out.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(random1);
SearchForActions<EightPuzzleBoard, Action> search = new AStarSearch<>
(new GraphSearch<>(), EightPuzzleFunctions::getNumberOfMisplacedTiles);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleSimulatedAnnealingDemo() {
System.out.println("\nEightPuzzleDemo Simulated Annealing Search");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(random1);
SimulatedAnnealingSearch<EightPuzzleBoard, Action> search = new SimulatedAnnealingSearch<>
(EightPuzzleFunctions::getManhattanDistance);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
System.out.println("Final State:\n" + search.getLastState());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarManhattanDemo() {
System.out.println("\nEightPuzzleDemo AStar Search (ManhattanHeursitic)");
try {
Problem<EightPuzzleBoard, Action> problem = new BidirectionalEightPuzzleProblem(random1);
SearchForActions<EightPuzzleBoard, Action> search = new AStarSearch<>
(new GraphSearch<>(), EightPuzzleFunctions::getManhattanDistance);
SearchAgent<Object, EightPuzzleBoard, Action> agent = new SearchAgent<>(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printInstrumentation(Properties properties) {
properties.keySet().stream().map(key -> key + "=" + properties.get(key)).forEach(System.out::println);
}
private static void printActions(List<Action> actions) {
actions.forEach(System.out::println);
}
}