-
Notifications
You must be signed in to change notification settings - Fork 1
/
GreedySearch.java
182 lines (156 loc) · 4.75 KB
/
GreedySearch.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.io.IOException;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class GreedySearch {
private Puzzle initialState;
private Puzzle goalState;
private ArrayList<State> solution = new ArrayList<State>();
private ArrayList<State> statesVisited = new ArrayList<State>();
private PriorityQueue<State> frontier;
private static String algo = "";
private int tc = 0, sc=0;
public static void main(String[] args) {
GreedySearch p;
try {
p = new GreedySearch();
p.run(algo);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String algo) throws IOException {
initialState = new Puzzle("puzzle.txt");
goalState = new Puzzle("puzzlegoal.txt");
if (algo.equals("Manhattan Distance")) {
solveManhattanDistance();
} else if (algo.equals("Misplaced Tiles")) {
solveMisplacedTiles();
}
printAlgorithmResult(algo);
}
public ArrayList<State> solveMisplacedTiles() {
boolean solutionFound = false, stateVisited = false;
State currentState = null;
ArrayList<State> children = new ArrayList<State>();
frontier = new PriorityQueue<State>();
currentState = new State(initialState, new Heuristic());
currentState.detMisplacedGreedyHeuristic(goalState);
frontier.add(currentState);
while (!frontier.isEmpty() && solutionFound != true) {
currentState = frontier.poll();
tc++;
if (currentState.getGrid().equalTo(goalState)) {
solutionFound = true;
break; // if solution is found, exit the frontier
}
children = currentState.generateSuccessors();
for (int i = 0; i < children.size(); i++) {
State child = children.get(i);
stateVisited = false;
for (State tempNode : statesVisited) {
if (child.equalTo(tempNode)) {
stateVisited = true;
break;
}
}
for (State tempNode : frontier) {
if (child.equalTo(tempNode)) {
stateVisited = true;
break;
}
}
if (stateVisited != true) {
sc++;
child.detMisplacedGreedyHeuristic(goalState);
frontier.add(child);
}
}
statesVisited.add(currentState);
}
// retrace the solution path
if (solutionFound == true) {
boolean pathFound = false;
solution.add(currentState);
while (pathFound == false) {
currentState = currentState.getParent();
solution.add(currentState);
if (currentState.getGrid().equalTo(initialState))
pathFound = true;
}
} else {// Solution not found
System.out.println("No solution");
}
return solution;
}
public ArrayList<State> solveManhattanDistance() {
boolean solutionFound = false, stateVisited = false;
State currentState = null;
ArrayList<State> children = new ArrayList<State>();
frontier = new PriorityQueue<State>();
currentState = new State(initialState, new Heuristic());
currentState.detManhattanGreedyHeuristic(goalState);
frontier.add(currentState);
while (!frontier.isEmpty() && solutionFound != true) {
currentState = frontier.poll();
sc = frontier.size();
tc++;
if (currentState.getGrid().equalTo(goalState)) {
solutionFound = true;
break; // if solution is found, exit the frontier
}
children = currentState.generateSuccessors();
for (int i = 0; i < children.size(); i++) {
State child = children.get(i);
stateVisited = false;
for (State tempNode : statesVisited) {
if (child.equalTo(tempNode)) {
stateVisited = true;
break;
}
}
for (State tempNode : frontier) {
if (child.equalTo(tempNode)) {
stateVisited = true;
break;
}
}
if (stateVisited != true) {
child.detManhattanGreedyHeuristic(goalState);
frontier.add(child);
sc = Math.max(sc, frontier.size());
}
}
statesVisited.add(currentState);
}
// retrace the solution path
if (solutionFound == true) {
boolean pathFound = false;
solution.add(currentState);
while (pathFound == false) {
currentState = currentState.getParent();
solution.add(currentState);
if (currentState.getGrid().equalTo(initialState))
pathFound = true;
}
} else {// Solution not found
System.out.println("No solution");
}
return solution;
}
public void printAlgorithmResult(String algo) {
int count = 1;
System.out.println("\n----------------------------------------");
System.out.println("Greedy Search: " + algo);
System.out.println("----------------------------------------");
System.out.println(" Solution Size: " + solution.size() + "\n Time Complexity: " + tc + "\nSpace Complexity: "
+ sc);
System.out.println();
System.out.println("===============================");
for (int i = solution.size() - 1; i >= 0; i--) {
System.out.print("[" + count + "]");
solution.get(i).getGrid().printPuzzle();
count++;
}
System.out.println("===============================");
}
}