-
Notifications
You must be signed in to change notification settings - Fork 0
/
PuzzleGraph.java
151 lines (126 loc) · 5.28 KB
/
PuzzleGraph.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
import javax.swing.JTextArea;
import java.util.*;
import java.util.function.Function;
public class PuzzleGraph {
private PuzzleBoard initialState;
public PuzzleGraph(PuzzleBoard initialState) {
this.initialState = initialState;
}
public void setInitialState(PuzzleBoard initialState) {
this.initialState = initialState;
}
// Manhattan Distance Heuristic
public static final Function<PuzzleBoard, Integer> manhattanDistance = board -> {
int distance = 0;
int size = board.getSize();
int[][] tiles = board.getTiles();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int value = tiles[i][j];
if (value != 0) {
int targetX = (value - 1) / size;
int targetY = (value - 1) % size;
distance += Math.abs(i - targetX) + Math.abs(j - targetY);
}
}
}
return distance;
};
// zeroFunction
public static final Function<PuzzleBoard, Integer> zeroFunction = board -> 0;
// Inadmissible Heuristic
public static final Function<PuzzleBoard, Integer> nonAdmissibleHeuristic = board -> {
int distance = 0;
int size = board.getSize();
int[][] tiles = board.getTiles();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int value = tiles[i][j];
if (value != 0) {
int targetX = (value - 1) / size;
int targetY = (value - 1) % size;
distance += Math.abs(i - targetX) + Math.abs(j - targetY);
}
}
}
return distance * 2;
};
public void solveUsingBFS(JTextArea textArea) {
long startTime = System.nanoTime();
int statesProcessed = 0;
Queue<PuzzleBoard> queue = new LinkedList<>();
Set<PuzzleBoard> visited = new HashSet<>();
initialState.setCost(0);
queue.add(initialState);
visited.add(initialState);
while (!queue.isEmpty()) {
PuzzleBoard current = queue.poll();
statesProcessed++;
if (current.isSolved()) {
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1_000_000;
int movementsCount = calculateMovements(current);
textArea.append("BFS -- Solution found! --\n");
current.printBoard(textArea);
textArea.append("Time taken: " + duration + " ms\n");
textArea.append("States processed: " + statesProcessed + "\n");
textArea.append("Movements made: " + movementsCount + "\n");
textArea.append("-------------------------\n");
return;
}
for (PuzzleBoard neighbor : current.generateNeighbors()) {
if (!visited.contains(neighbor)) {
queue.add(neighbor);
visited.add(neighbor);
neighbor.setParent(current);
}
}
}
textArea.append("No solution found.\n");
}
public void solveUsingAStar(Function<PuzzleBoard, Integer> heuristicFunction, JTextArea textArea) {
long startTime = System.nanoTime();
int statesProcessed = 0;
PriorityQueue<PuzzleBoard> openSet = new PriorityQueue<>(
Comparator.comparingInt(board -> board.getCost() + heuristicFunction.apply(board)));
Map<PuzzleBoard, Integer> costSoFar = new HashMap<>();
initialState.setCost(0);
openSet.add(initialState);
costSoFar.put(initialState, 0);
while (!openSet.isEmpty()) {
PuzzleBoard current = openSet.poll();
statesProcessed++;
if (current.isSolved()) {
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1_000_000;
int movementsCount = calculateMovements(current);
textArea.append("AStar -- Solution found! --\n");
current.printBoard(textArea);
textArea.append("Time taken: " + duration + " ms\n");
textArea.append("States processed: " + statesProcessed + "\n");
textArea.append("Movements made: " + movementsCount + "\n");
textArea.append("-------------------------\n");
return;
}
for (PuzzleBoard neighbor : current.generateNeighbors()) {
int newCost = costSoFar.get(current) + 1;
if (!costSoFar.containsKey(neighbor) || newCost < costSoFar.get(neighbor)) {
neighbor.setCost(newCost);
costSoFar.put(neighbor, newCost);
openSet.add(neighbor);
neighbor.setParent(current);
}
}
}
textArea.append("No solution found.\n");
}
private int calculateMovements(PuzzleBoard solutionState) {
int movementsCount = 0;
PuzzleBoard currentState = solutionState;
while (currentState != null && currentState.getParent() != null) {
movementsCount++;
currentState = currentState.getParent();
}
return movementsCount;
}
}