Skip to content

Commit 305f57a

Browse files
committed
SP done. Submitting.
1 parent 957a33c commit 305f57a

File tree

5 files changed

+150
-62
lines changed

5 files changed

+150
-62
lines changed

src/ass02/Board.java

+101-42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.LinkedList;
5+
import java.util.List;
56
import java.util.PriorityQueue;
67

78
public class Board implements SlidingBlock {
@@ -11,11 +12,7 @@ public class Board implements SlidingBlock {
1112
int[] board;
1213
Node[] node;
1314
int startPosition;
14-
15-
16-
//PriorityQueue<Node> openSet;
17-
//ArrayList<Node> closedSet;
18-
15+
1916
public Board(int n){
2017
this.n = n;
2118
board = new int[n*n];
@@ -31,8 +28,7 @@ public int[] solve(int[] start, int[] goal, int maxMoves) {
3128
public int[] shortestPath(int startPosition) {
3229
this.startPosition = startPosition;
3330
initialiseSPNodes(startPosition);
34-
this.aStar(node[startPosition], node[0], Integer.MAX_VALUE);
35-
return null;
31+
return this.aStar(node[startPosition], node[0], Integer.MAX_VALUE);
3632
}
3733

3834
@Override
@@ -41,16 +37,7 @@ public void addWall(int positionI, int positionJ) {
4137

4238
}
4339

44-
public void initialiseBoard(int startPos){
45-
for(int i = 0; i < this.board.length; i++){
46-
if(i == startPos){
47-
this.board[i] = 0;
48-
} else{
49-
this.board[i] = 1;
50-
}
51-
}
52-
}
53-
40+
// Initalise a node represention of the input array
5441
public void initialiseSPNodes(int startPos){
5542
node = new SPNode[n*n];
5643
for(int i = 0; i < this.node.length; i++){
@@ -74,40 +61,69 @@ public void initialiseSPNodes(int startPos){
7461
node[0].setN(n);
7562
}
7663

64+
// prints the board as a matrix
7765
public void printBoardToConsole(){
7866
for(int i = 0; i < this.node.length; i++){
79-
System.out.print(this.node[i].location+" ");
67+
System.out.print(this.node[i].location+" ");
68+
if(this.node[i].location < 10) System.out.print(" ");
8069
if((i+1) % n == 0){
8170
System.out.print("\n");
8271
}
8372
}
8473
}
8574

75+
76+
/*
77+
* Return the node located above, if there is one
78+
*/
8679
public Node up(Node node){
8780
if(node.indexOfUp() < 0) return null;
8881
return this.node[node.indexOfUp()];
8982
}
83+
84+
/*
85+
* Return the node located below, if there is one
86+
*/
9087
public Node down(Node node){
9188
if(node.indexOfDown() < 0) return null;
9289
return this.node[node.indexOfDown()];
9390
}
91+
92+
/*
93+
* Return the node located on the left, if there is one
94+
*/
9495
public Node left(Node node){
9596
if(node.indexOfLeft() < 0) return null;
9697
return this.node[node.indexOfLeft()];
9798
}
99+
100+
/*
101+
* Return the node located on the right, if there is one
102+
*/
98103
public Node right(Node node){
99104
if(node.indexOfRight() < 0) return null;
100105
return this.node[node.indexOfRight()];
101106
}
102107

108+
/*
109+
* Return the node from the stated position
110+
*/
103111
public Node getNode(int pos){
104112
return node[pos];
105113
}
106114

115+
/*
116+
* For the shortest path algo
117+
*
118+
* Return true if the node is in the first row
119+
*/
107120
public boolean reachedGoal(Node node){
108121
return node.location < n;
109122
}
110123

124+
/*
125+
* Add directly attached nodes to a list for processing in A*
126+
*/
111127
public ArrayList<Node> getDirectlyAttachedNodes(Node n){
112128
ArrayList<Node> list = new ArrayList<Node>();
113129
if(this.up(n) != null) list.add(this.up(n));
@@ -117,56 +133,99 @@ public ArrayList<Node> getDirectlyAttachedNodes(Node n){
117133
return list;
118134
}
119135

120-
136+
/*
137+
* A* Algo from psuedo code from wikipedia
138+
* http://en.wikipedia.org/wiki/A*_search_algorithm
139+
*/
121140
private int[] aStar(Node startNode, Node goalNode, int maxLength) {
122141
ArrayList<Node> closedSet = new ArrayList<Node>();
123142
PriorityQueue<Node> openSet = new PriorityQueue<Node>();
124-
startNode.h = this.heuristicDistance(startNode, goalNode);
125-
int tempG;
143+
144+
// When handling SPNodes
145+
if(goalNode instanceof SPNode){
146+
goalNode = node[startNode.col()];
147+
}
148+
149+
int tentativeG;
126150
Node current;
127-
boolean yInOpenSet;
151+
boolean tentativeBetter = false;
152+
153+
//set details of start node
154+
startNode.g = 0;
155+
startNode.h = this.heuristicDistance(startNode, goalNode);
156+
startNode.f = startNode.h;
157+
158+
//add startNode to openset
159+
openSet.add(startNode);
128160

129161
while (!openSet.isEmpty()) {
130162
current = openSet.poll();
131163
if(this.reachedGoal(current)){
132-
System.out.print("done");
164+
reconstructPath(current);
165+
return this.toIntArray(this.path);
133166
}
134167
closedSet.add(current);
135168
if (current.g >= maxLength) continue;
136-
this.printBoardToConsole();
137169
for (Node attached : getDirectlyAttachedNodes(current)) {
138170
if (closedSet.contains(attached)) continue;
139-
tempG = current.g + 1;
140-
141-
yInOpenSet = false;
142-
for (Node thisState : openSet) {
143-
if (attached.equals(thisState)) {
144-
attached = thisState;
145-
yInOpenSet = true;
146-
}
147-
}
148-
if (!yInOpenSet || tempG < attached.g) {
149-
attached.parent = current;
150-
attached.g = tempG;
151-
attached.h = heuristicDistance(attached, goalNode);
152-
attached.f = attached.g + attached.h;
153-
}
154-
if (!yInOpenSet && attached.f <= maxLength) openSet.add(attached);
171+
tentativeG = current.g + 1;
172+
if(!(openSet.contains(attached))){
173+
tentativeBetter = true;
174+
}else if(tentativeG < attached.g){
175+
tentativeBetter = true;
176+
}else{
177+
tentativeBetter = false;
178+
}
179+
if(tentativeBetter){
180+
attached.parent = current;
181+
attached.g = tentativeG;
182+
attached.h = heuristicDistance(attached, goalNode);
183+
attached.f = attached.g + attached.h;
184+
openSet.add(attached);
185+
}
155186
}
156187
}
157-
158188
return null;
159189
}
160190

191+
/*
192+
* Return the distance away in terms of the sum of the row and col dofferences
193+
*/
161194
private int heuristicDistance(Node from, Node to){
162195
return(Math.abs(from.col() - to.col()) + Math.abs(from.row() - to.row()));
163196
}
164197

198+
/*
199+
* Reconstruct the path to the start, the data is kept in a list
200+
*/
165201
public void reconstructPath(Node node){
166202
if(node.parent != null){
167203
reconstructPath(node.parent);
168-
} else{
204+
}
169205
path.add(node.location);
206+
}
207+
208+
/*
209+
* Return the Integer List as a primitive int array
210+
* code found at taken from here http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
211+
*/
212+
int[] toIntArray(List<Integer> list) {
213+
int[] ret = new int[list.size()];
214+
int i = 0;
215+
for (Integer e : list)
216+
ret[i++] = e.intValue();
217+
return ret;
218+
}
219+
220+
/*
221+
* Return array contents as a string
222+
*/
223+
public String arrayToString(int[] theArray){
224+
if(theArray == null) return "";
225+
StringBuilder arrayAsString = new StringBuilder();
226+
for(int i = 0; i < theArray.length; i++){
227+
arrayAsString.append(theArray[i]+" ");
170228
}
229+
return arrayAsString.toString();
171230
}
172231
}

src/ass02/Node.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package ass02;
22

3-
import java.util.Comparator;
4-
5-
public abstract class Node implements Comparator<Node> {
3+
public abstract class Node implements Comparable<Node> {
64

5+
// the values needed for the A*
76
static int n;
87
int g;
98
int h;
109
int f;
1110
int location;
1211
Node parent;
13-
//boolean isHole;
1412

1513
public void setN(int nVal){
1614
n = nVal;
@@ -20,18 +18,24 @@ public int getN(){
2018
return n;
2119
}
2220

23-
2421
public abstract int indexOfLeft();
2522
public abstract int indexOfRight();
2623
public abstract int indexOfUp();
2724
public abstract int indexOfDown();
2825
public abstract void setIndexesToOtherNodes();
2926

27+
/*
28+
* Returns the conceptual row value
29+
*/
3030
public int row(){
3131
return (this.location - this.col()) / n;
3232
}
3333

34+
/*
35+
* Returns the conceptual col value
36+
*/
3437
public int col(){
3538
return this.location % n;
3639
}
40+
3741
}

src/ass02/SBPuzzle.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package ass02;
22

33
public class SBPuzzle {
4-
4+
55
/**
66
* @param args
77
*/
88
public static void main(String[] args) {
9-
Board b = new Board(3);
10-
b.initialiseBoard(8);
11-
b.shortestPath(8);
12-
b.printBoardToConsole();
13-
//int i = b.getNode(8).indexOfUp();
14-
//
15-
//System.out.print(i);
9+
Board b = new Board(4);
10+
System.out.println(b.arrayToString(b.shortestPath(15)));
1611
}
17-
1812
}

src/ass02/SPNode.java

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ass02;
22

3+
34
public class SPNode extends Node {
45
int up;
56
int down;
@@ -8,38 +9,51 @@ public class SPNode extends Node {
89

910
public SPNode(int dimensions){
1011
n = dimensions;
11-
}
12-
13-
@Override
14-
public int compare(Node n1, Node n2) {
15-
return n1.f - n2.f;
1612
}
1713

14+
/*
15+
* Finds the index of the left as if it was an actual matrix
16+
*/
1817
@Override
1918
public int indexOfLeft(){
2019
if(this.location % n == 0)
2120
return -1;
2221
return this.location - 1;
2322
}
23+
24+
/*
25+
* Finds the index of the right as if it was an actual matrix
26+
*/
2427
@Override
2528
public int indexOfRight(){
2629
if((this.location + 1) % n == 0)
2730
return -1;
2831
return this.location + 1;
2932
}
33+
34+
/*
35+
* Finds the index of the above as if it was an actual matrix
36+
*/
3037
@Override
3138
public int indexOfUp(){
3239
if(this.location - n < 0)
3340
return -1;
3441
return this.location - n;
3542
}
43+
44+
/*
45+
* Finds the index of the below as if it was an actual matrix
46+
*/
3647
@Override
3748
public int indexOfDown(){
3849
if(this.location + n >= (n*n))
3950
return -1;
4051
return this.location + n;
4152
}
4253

54+
/*
55+
* Sets this node to know what indexes are around in 4 directions.
56+
*/
4357
@Override
4458
public void setIndexesToOtherNodes() {
4559
this.down = indexOfDown();
@@ -51,6 +65,14 @@ public void setIndexesToOtherNodes() {
5165

5266
@Override
5367
public String toString(){
54-
return "#:"+this.location+" up:"+this.up+" down:"+this.down+" left:"+this.left+" right:"+this.right;
68+
return "\n#:"+this.location+" up:"+this.up+" down:"+this.down+" left:"+this.left+" right:"+this.right+" f:"+this.f+" g:"+this.g+" h:"+this.h;
69+
}
70+
71+
/*
72+
* Used for evaluation of PriorityQueue
73+
*/
74+
@Override
75+
public int compareTo(Node o1) {
76+
return this.f - o1.f;
5577
}
5678
}

0 commit comments

Comments
 (0)