diff --git a/Lab1/lab1_workspace/project/tddc17/MyAgentProgram.class b/Lab1/lab1_workspace/project/tddc17/MyAgentProgram.class index 4c848dbc32b579b5812855b624077a5df6eaf5ad..528cf680de2ddbbf992e656023da0567ac68da9b 100644 Binary files a/Lab1/lab1_workspace/project/tddc17/MyAgentProgram.class and b/Lab1/lab1_workspace/project/tddc17/MyAgentProgram.class differ diff --git a/Lab1/lab1_workspace/project/tddc17/MyAgentState.class b/Lab1/lab1_workspace/project/tddc17/MyAgentState.class index c02a85397d730129cfb9c257030d802811847a35..e05247deb676a8445d214307125589b0e451bc12 100644 Binary files a/Lab1/lab1_workspace/project/tddc17/MyAgentState.class and b/Lab1/lab1_workspace/project/tddc17/MyAgentState.class differ diff --git a/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.class b/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.class index 13edb8714272dd0d9855c73eeabc94785155e9b3..31da853a6022d40c526c1f8bd0798769f03d9a2f 100644 Binary files a/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.class and b/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.class differ diff --git a/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.java b/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.java index 5b5b2a5bc2bc266826d135eefc1e4ae6eb41cf59..ff252afe9608a5a517190a21dc54ae96d7fefd86 100644 --- a/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.java +++ b/Lab1/lab1_workspace/project/tddc17/MyVacuumAgent.java @@ -1,6 +1,7 @@ package tddc17; import java.util.ArrayList; +import java.util.HashSet; import java.util.Deque; import java.util.LinkedList; import java.util.List; @@ -17,7 +18,7 @@ import java.util.Stack; class MyAgentState { - public int[][] world = new int[30][30]; + public int[][] world = new int[17][17]; public int initialized = 0; final int UNKNOWN = 0; final int WALL = 1; @@ -100,12 +101,94 @@ class MyAgentState } } - -class Node{ +class GridPos { int x; int y; - boolean visited; - List<Node> neighbours = new ArrayList<Node>(); + + public GridPos(int p_x, int p_y) { + x = p_x; + y = p_y; + } + + // To make hash sets work... + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GridPos other = (GridPos) obj; + if (x != other.x || y != other.y) + return false; + return true; + } + + + +} + + + +class Node{ + Node parent; + GridPos aState; + + public Boolean isRoot() { + if (parent == null) { + return true; + }else { + return false; + } + } + + public Node(GridPos cState, Node parentNode) { + parent = parentNode; + aState = cState; + } + + public Node(GridPos cState) { + aState = cState; + parent = null; + } + + // To make hash sets work + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((aState == null) ? 0 : aState.hashCode()); + return result; + } + + // To make hash sets work + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Node other = (Node) obj; + if (aState == null) { + if (other.aState != null) + return false; + } else if (!aState.equals(other.aState)) + return false; + return true; + } + } class MyAgentProgram implements AgentProgram { @@ -120,18 +203,12 @@ class MyAgentProgram implements AgentProgram { Queue<Action> queue = new LinkedList<Action> (); Action element; - Node nextPosition = new Node(); - Node currentNode = new Node(); - Boolean returnHome = false; - int nextDirection = 0; - Stack<Node> frontier = new Stack<Node>(); - Stack<Node> reached = new Stack<Node>(); - //////////// New DFS /////////////////// - Node startNode = new Node(); - boolean start = true; - - Stack<Node> stack = new Stack<Node>(); + Boolean start = true; + //////////////////////////////////////// + + public HashSet<GridPos> explored = new HashSet<GridPos>(); + public Stack<Node> frontier = new Stack<Node>(); //////////////////////////////////////// @@ -178,7 +255,6 @@ class MyAgentProgram implements AgentProgram { System.out.println("x=" + state.agent_x_position); System.out.println("y=" + state.agent_y_position); - System.out.println("nextDirection = " + nextDirection); iterationCounter--; @@ -228,51 +304,36 @@ class MyAgentProgram implements AgentProgram { } - if(start) { - startNode.x = state.agent_x_position; - startNode.y = state.agent_y_position; - start = false; - stack.push(startNode); + if(bump) { + Queue<Action> temp = queue; + queue = ifBump(); + queue.addAll(temp); } - - ///////////////////////////////////////////////////////////////////////////// - - while(!stack.isEmpty()) { - Node current = stack.pop(); - - current.visited = true; - System.out.println("Current x = " + current.x + " and y = " + current.y); - - - } - - ///////////////////////////////////////////////////////////////////////////// - - - nextDirection = deepFirstSearch(); - - if(nextDirection == state.agent_direction) { - element = LIUVacuumEnvironment.ACTION_MOVE_FORWARD; - queue.add(element); - } else { - switch(state.agent_direction-nextDirection) { - case -1: - case 3: - element = LIUVacuumEnvironment.ACTION_TURN_RIGHT; - queue.add(element); - break; - case 1: - case -3: - element = LIUVacuumEnvironment.ACTION_TURN_LEFT; - queue.add(element); - break; - + if(queue.size() > 0) { + + element = queue.remove(); + handleMoves(element); + return element; + } + + for(int i = 1; i<state.world.length;i++) { + for(int j = 1; j <state.world[i].length; j++) { + if(state.world[i][j] == state.UNKNOWN) { + queue.clear(); + queue = walkToNode(new Node(new GridPos(i, j))); + System.out.println("Goal node: x =" + i + " y= " + j ); + if(queue.size() > 0) { + + element = queue.remove(); + handleMoves(element); + return element; + } + + } } - element = LIUVacuumEnvironment.ACTION_MOVE_FORWARD; - queue.add(element); } - + if(queue.size() > 0) { @@ -281,222 +342,232 @@ class MyAgentProgram implements AgentProgram { return element; } - return NoOpAction.NO_OP; + return LIUVacuumEnvironment.ACTION_SUCK; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // HERE STARTS THE METHODS // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public void insertNeighbours(Stack<Node> stack) { - Node rightNode = new Node(); - Node leftNode = new Node(); - Node infrontNode = new Node(); - Node behindNode = new Node(); - + public Queue<Action> ifBump() { + Queue<Action> pathQueue = new LinkedList<Action>(); - switch(state.agent_direction) { - case MyAgentState.NORTH: - infrontNode.x=state.agent_x_position; - infrontNode.y=state.agent_y_position -1; - rightNode.x=state.agent_x_position +1; - rightNode.y=state.agent_y_position; - leftNode.x=state.agent_x_position -1; - leftNode.y=state.agent_y_position; - behindNode.x=state.agent_x_position; - behindNode.y=state.agent_y_position +1; - break; - case MyAgentState.SOUTH: - infrontNode.x=state.agent_x_position; - infrontNode.y=state.agent_y_position +1; - rightNode.x=state.agent_x_position -1; - rightNode.y=state.agent_y_position; - leftNode.x=state.agent_x_position +1; - leftNode.y=state.agent_y_position; - behindNode.x=state.agent_x_position; - behindNode.y=state.agent_y_position -1; - break; - case MyAgentState.EAST: - infrontNode.x=state.agent_x_position +1; - infrontNode.y=state.agent_y_position; - rightNode.x=state.agent_x_position; - rightNode.y=state.agent_y_position +1; - leftNode.x=state.agent_x_position; - leftNode.y=state.agent_y_position -1; - behindNode.x=state.agent_x_position-1; - behindNode.y=state.agent_y_position; - break; - case MyAgentState.WEST: - infrontNode.x=state.agent_x_position -1; - infrontNode.y=state.agent_y_position; - rightNode.x=state.agent_x_position; - rightNode.y=state.agent_y_position -1; - leftNode.x=state.agent_x_position; - leftNode.y=state.agent_y_position +1; - behindNode.x=state.agent_x_position +1; - behindNode.y=state.agent_y_position; - break; + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + pathQueue.add(LIUVacuumEnvironment.ACTION_MOVE_FORWARD); + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_LEFT); + pathQueue.add(LIUVacuumEnvironment.ACTION_MOVE_FORWARD); - } - - stack.push(behindNode); - stack.push(rightNode); - stack.push(leftNode); - stack.push(infrontNode); + return pathQueue; } + - public int deepFirstSearch() { - - Node temp = new Node(); - Node newNode = new Node(); - newNode.x = state.agent_x_position; - newNode.y = state.agent_y_position; + public int calculateDir(Node node) { + int xDirection = node.aState.x - state.agent_x_position; + int yDirection = node.aState.y - state.agent_y_position; - if(reached.empty()) { - reached.push(temp); + if(xDirection == 1) { + return MyAgentState.EAST; + } else if(xDirection == -1) { + return MyAgentState.WEST; + } else if (yDirection == 1) { + return MyAgentState.SOUTH; + } else { + return MyAgentState.NORTH; } - //Checks if the new node has already been reached - if(!newNode.equals(reached.peek())) { - reached.push(newNode); - Node rightNode = new Node(); - Node leftNode = new Node(); - Node infrontNode = new Node(); - Node behindNode = new Node(); - + + + } + + public Queue<Action> walkToNode(Node node){ + Queue<Action> pathQueue = new LinkedList<Action>(); + int diffX = state.agent_x_position-node.aState.x; + int diffY = state.agent_y_position-node.aState.y; + int nextDirection; + int directionFix = 0; + + if(diffX < 0) { - switch(state.agent_direction) { - case MyAgentState.NORTH: - infrontNode.x=state.agent_x_position; - infrontNode.y=state.agent_y_position -1; - rightNode.x=state.agent_x_position +1; - rightNode.y=state.agent_y_position; - leftNode.x=state.agent_x_position -1; - leftNode.y=state.agent_y_position; - behindNode.x=state.agent_x_position; - behindNode.y=state.agent_y_position +1; - break; - case MyAgentState.SOUTH: - infrontNode.x=state.agent_x_position; - infrontNode.y=state.agent_y_position +1; - rightNode.x=state.agent_x_position -1; - rightNode.y=state.agent_y_position; - leftNode.x=state.agent_x_position +1; - leftNode.y=state.agent_y_position; - behindNode.x=state.agent_x_position; - behindNode.y=state.agent_y_position -1; - break; - case MyAgentState.EAST: - infrontNode.x=state.agent_x_position +1; - infrontNode.y=state.agent_y_position; - rightNode.x=state.agent_x_position; - rightNode.y=state.agent_y_position +1; - leftNode.x=state.agent_x_position; - leftNode.y=state.agent_y_position -1; - behindNode.x=state.agent_x_position-1; - behindNode.y=state.agent_y_position; - break; - case MyAgentState.WEST: - infrontNode.x=state.agent_x_position -1; - infrontNode.y=state.agent_y_position; - rightNode.x=state.agent_x_position; - rightNode.y=state.agent_y_position -1; - leftNode.x=state.agent_x_position; - leftNode.y=state.agent_y_position +1; - behindNode.x=state.agent_x_position +1; - behindNode.y=state.agent_y_position; - break; + nextDirection = MyAgentState.EAST; + if(!(state.agent_direction == nextDirection)) { + switch(state.agent_direction-nextDirection) { + + case -1: + case 3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + directionFix = 1; + break; + case 1: + case -3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_LEFT); + directionFix = -1; + break; + default: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + + } } - - - if(state.world[rightNode.x][rightNode.y] == state.UNKNOWN) { - frontier.push(rightNode); - } - if(state.world[leftNode.x][leftNode.y] == state.UNKNOWN) { - frontier.push(leftNode); - } - if(state.world[infrontNode.x][infrontNode.y] == state.UNKNOWN) { - frontier.push(infrontNode); - } - - /*for (int i = 0; i <= 3; i++) { - if (state.world[state.agent_x_position + dirToX(i)][state.agent_y_position + dirToY(i)] == state.UNKNOWN) { - Node node = new Node(); - node.x = state.agent_x_position + dirToX(i); - node.y = state.agent_y_position + dirToY(i); - System.out.println("Counting = " + i); - - frontier.push(node); + System.out.println("Next Direction: "); + + } else if (diffX > 0) { + nextDirection = MyAgentState.WEST; + if(!(state.agent_direction == nextDirection)) { + switch(state.agent_direction-nextDirection) { + case -1: + case 3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + directionFix = 1; + + break; + case 1: + case -3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_LEFT); + directionFix = -1; + + break; + default: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + + } } - */ } - System.out.println("nextDirection = " + frontier); - /////////////////////////////////////////// + for(int i = 0; i < Math.abs(diffX); i++) { + pathQueue.add(LIUVacuumEnvironment.ACTION_MOVE_FORWARD); + + } + + int newDirection = (state.agent_direction+directionFix)%4; + if(newDirection < 0) { + newDirection = 3; + } - while (!frontier.isEmpty()) { - Node nextNode = frontier.pop(); + if(diffY < 0) { - if(checkAdjacent(nextNode)) { - nextPosition = nextNode; - } else { - - nextPosition = reached.pop(); - //If popped node is the same node we are currently in - if(nextPosition.x == state.agent_x_position && nextPosition.y == state.agent_y_position) { - nextPosition = reached.pop(); + nextDirection = MyAgentState.SOUTH; + if(!(newDirection == nextDirection)) { + switch(newDirection-nextDirection) { + case -1: + case 3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + break; + case 1: + case -3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_LEFT); + break; + default: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + + } + } + } else if (diffY > 0) { + nextDirection = MyAgentState.NORTH; + if(!(newDirection == nextDirection)) { - + switch(newDirection-nextDirection) { + case -1: + case 3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + break; + case 1: + case -3: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_LEFT); + break; + default: + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + pathQueue.add(LIUVacuumEnvironment.ACTION_TURN_RIGHT); + + + } } - - return calculateDir(nextPosition); - } - //The final return statement when frontier is empty, will signal going home - return -1; + for(int i = 0; i < Math.abs(diffY); i++) { + pathQueue.add(LIUVacuumEnvironment.ACTION_MOVE_FORWARD); + + } + + return pathQueue; } - public int calculateDir(Node node) { - int xDirection = node.x - state.agent_x_position; - int yDirection = node.y - state.agent_y_position; + public ArrayList<GridPos> insertNeighbours(GridPos aState) { - if(xDirection == 1) { - return MyAgentState.EAST; - } else if(xDirection == -1) { - return MyAgentState.WEST; - } else if (yDirection == 1) { - return MyAgentState.SOUTH; - } else { - return MyAgentState.NORTH; + GridPos rightNode; + GridPos leftNode; + GridPos infrontNode; + GridPos behindNode; + + + + switch(state.agent_direction) { + case MyAgentState.NORTH: + infrontNode = new GridPos(state.agent_x_position, state.agent_y_position -1); + rightNode = new GridPos(state.agent_x_position+1, state.agent_y_position); + leftNode = new GridPos(state.agent_x_position-1, state.agent_y_position); + behindNode = new GridPos(state.agent_x_position, state.agent_y_position +1); + break; + case MyAgentState.SOUTH: + infrontNode = new GridPos(state.agent_x_position, state.agent_y_position +1); + rightNode = new GridPos(state.agent_x_position-1, state.agent_y_position); + leftNode = new GridPos(state.agent_x_position+1, state.agent_y_position); + behindNode = new GridPos(state.agent_x_position, state.agent_y_position -1); + break; + case MyAgentState.EAST: + infrontNode = new GridPos(state.agent_x_position+1, state.agent_y_position); + rightNode = new GridPos(state.agent_x_position, state.agent_y_position+1); + leftNode = new GridPos(state.agent_x_position, state.agent_y_position-1); + behindNode = new GridPos(state.agent_x_position-1, state.agent_y_position); + break; + //West + default : + infrontNode = new GridPos(state.agent_x_position-1, state.agent_y_position); + rightNode = new GridPos(state.agent_x_position, state.agent_y_position-1); + leftNode = new GridPos(state.agent_x_position, state.agent_y_position+1); + behindNode = new GridPos(state.agent_x_position+1, state.agent_y_position); + } + ArrayList<GridPos> list = new ArrayList<GridPos>(); + ArrayList<GridPos> temp = new ArrayList<GridPos>(); - } - - //Checks if nextNode is adjacent to current position - public Boolean checkAdjacent(Node node) { - if(state.world[node.x][node.y] == state.WALL) { - return false; + list.add(behindNode); + list.add(leftNode); + list.add(rightNode); + list.add(infrontNode); + + + + + for(GridPos zeroCheck: list) { + if(zeroCheck.x == 0 || zeroCheck.y == 0) { + temp.add(zeroCheck); + } } - if((Math.abs(node.x-state.agent_x_position) == 1 || - Math.abs(node.x-state.agent_x_position) == 0) && - (Math.abs(node.y-state.agent_y_position) == 1 || - Math.abs(node.y-state.agent_y_position) == 0)) { - - - return true; + + for(GridPos remove: temp) { + list.remove(remove); } - return false; + + return list; + + } + + + public int dirToX(int dir) { switch (dir) { case 0: diff --git a/Lab1/lab1_workspace/project/tddc17/Node.class b/Lab1/lab1_workspace/project/tddc17/Node.class index ea0dd47cc8f1a7b656fd215b394666b36a27a118..f2f51e56ba956043814f1686cec041e8df8a7114 100644 Binary files a/Lab1/lab1_workspace/project/tddc17/Node.class and b/Lab1/lab1_workspace/project/tddc17/Node.class differ