Skip to content

Commit

Permalink
Merge pull request #3 from michael-kamel/refinement/grid
Browse files Browse the repository at this point in the history
Refinement/grid
  • Loading branch information
michael-kamel authored Nov 22, 2018
2 parents 8fd4b2f + 5cfc26d commit 5538964
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 115 deletions.
3 changes: 2 additions & 1 deletion src/searching/agents/SearchAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public SearchAgent(int maxTreeNodes) {
this.maxTreeNodes = maxTreeNodes;
}

public SearchProblemSolution<T, V> search(SearchProblem<T, V> problem, SearchStrategy<T, V> searchStrategy) {
public <W extends SearchProblem<T, V, W>>
SearchProblemSolution<T, V> search(SearchProblem<T, V, W> problem, SearchStrategy<T, V> searchStrategy) {
SearchTreeNode<T, V> rootNode = new SearchTreeNode<T, V>(Optional.empty(), 0,
problem.getInitialState(), Optional.empty(), 0);

Expand Down
13 changes: 13 additions & 0 deletions src/searching/exceptions/VisualizationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package searching.exceptions;

public class VisualizationException extends Exception {

public VisualizationException(Throwable e) {
super(e);
}

/**
*
*/
private static final long serialVersionUID = 1L;
}
20 changes: 11 additions & 9 deletions src/searching/problems/SearchProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
import java.util.function.Consumer;

import searching.agents.SearchTreeNode;
import searching.exceptions.SearchProblemException;
import searching.visualizers.Visualizer;
import searching.exceptions.GameConstructionConstraintsViolation;
import searching.exceptions.VisualizationException;
import searching.visualizers.StateVisualizer;

public abstract class SearchProblem<T extends SearchState, V extends SearchAction> {
public abstract class SearchProblem<T extends SearchState, V extends SearchAction,
X extends SearchProblem<T, V, X>> {
private final Iterable<V> possibleActions;
private Visualizer visualizer;
private StateVisualizer<X, T> visualizer;

public SearchProblem(Iterable<V> possibleActions, Visualizer visualizer) throws SearchProblemException {
public SearchProblem(Iterable<V> possibleActions, StateVisualizer<X, T> visualizer) throws GameConstructionConstraintsViolation {
this.possibleActions = possibleActions;
this.visualizer = visualizer;
}

public Visualizer getVisualizer() {
public StateVisualizer<X, T> getVisualizer() {
return this.visualizer;
}

public void setVisualizer(Visualizer visualizer) {
public void setVisualizer(StateVisualizer<X, T> visualizer) {
this.visualizer = visualizer;
}

Expand Down Expand Up @@ -74,9 +76,9 @@ public void accept(V action) {

public abstract boolean canTransition(T state, V action);

public abstract void visualize(); //visualizes problem itself
public abstract void visualize() throws VisualizationException; //visualizes problem itself

public abstract void visualize(T state); //visualizes one state
public abstract void visualize(T state) throws VisualizationException; //visualizes one state

protected abstract long getActionCost(T state, V action);
}
32 changes: 17 additions & 15 deletions src/searching/problems/SearchProblemSolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import java.util.LinkedList;
import java.util.Optional;
import searching.agents.SearchTreeNode;
import searching.exceptions.VisualizationException;

public class SearchProblemSolution<T extends SearchState, V extends SearchAction> {

private final Optional<SearchTreeNode<T, V>> node;
private final SearchProblem<T, V> problem;
private final SearchProblem<T, V, ?> problem;
private final int expandedNodesCount;
private final LinkedList<SearchTreeNode<T, V>> nodeSequence;

public SearchProblemSolution(SearchProblem<T, V> problem, Optional<SearchTreeNode<T, V>> node,
public SearchProblemSolution(SearchProblem<T, V, ?> problem, Optional<SearchTreeNode<T, V>> node,
int expandedNodesCount) {
this.problem = problem;
this.node = node;
Expand All @@ -23,7 +24,7 @@ public Optional<SearchTreeNode<T, V>> getNode() {
return this.node; //goalNode
}

public SearchProblem<T, V> getProblem() {
public SearchProblem<T, V, ?> getProblem() {
return this.problem;
}

Expand All @@ -46,28 +47,29 @@ public LinkedList<SearchTreeNode<T, V>> getNodeSequence() {
return list;
}

public void visualizeSolution(boolean actionsOnly, boolean showActionCost) {
public void visualizeSolution(boolean actionsOnly, boolean showActionCost) throws VisualizationException {

Iterable<SearchTreeNode<T, V>> solution = this.getNodeSequence();
solution.forEach(node -> {
if(node.getParent().isPresent())
for(SearchTreeNode<T, V> node : solution) {
if(node.getParent().isPresent()) {
this.problem.getVisualizer().visualizeLine(node.getAction().get() +
(showActionCost ? ": " + problem.getActionCost(
node.getParent().get().getCurrentState(), node.getAction().get()) : ""));

}

if(!actionsOnly)
this.problem.visualize(node.getCurrentState());
});
}
}

private void showNode(SearchTreeNode<T, V> node) {
private void showNode(SearchTreeNode<T, V> node) throws VisualizationException {
if(node.getParent().isPresent())
showNode(node.getParent().get());

this.problem.getVisualizer().visualizeLine(node.getCurrentState().toString());
this.problem.visualize(node.getCurrentState());
}

public void showNodeSequence() {//sequence of any nodes (not necessarily a solution)
public void showNodeSequence() throws VisualizationException {//sequence of any nodes (not necessarily a solution)
if(this.node.isPresent()) {
showNode(this.node.get());
} else {
Expand All @@ -81,28 +83,28 @@ public int getSolutionStepCount() {

public static final class FailedSearchProblemSolution<T extends SearchState, V extends SearchAction>
extends SearchProblemSolution<T, V> {
public FailedSearchProblemSolution(SearchProblem<T, V> problem, Optional<SearchTreeNode<T, V>>
public FailedSearchProblemSolution(SearchProblem<T, V, ?> problem, Optional<SearchTreeNode<T, V>>
node, int expandedNodesCount) {
super(problem, node, expandedNodesCount);
}
}

public static final class BottomProblemSolution<T extends SearchState, V extends SearchAction>
extends SearchProblemSolution<T, V> {
public BottomProblemSolution(SearchProblem<T, V> problem, Optional<SearchTreeNode<T, V>>
public BottomProblemSolution(SearchProblem<T, V, ?> problem, Optional<SearchTreeNode<T, V>>
node, int expandedNodesCount) {
super(problem, node, expandedNodesCount);
}
}

public static <T extends SearchState, V extends SearchAction> SearchProblemSolution<T, V> NoSolution(
SearchProblem<T, V> problem, int expandedNodesCount) {
SearchProblem<T, V, ?> problem, int expandedNodesCount) {
return new FailedSearchProblemSolution<T, V>(problem, Optional.empty(), expandedNodesCount);
}


public static <T extends SearchState, V extends SearchAction> SearchProblemSolution<T, V> Bottom(
SearchProblem<T, V> problem, int expandedNodesCount) {
SearchProblem<T, V, ?> problem, int expandedNodesCount) {
return new BottomProblemSolution<T, V>(problem, Optional.empty(), expandedNodesCount);
}

Expand Down
4 changes: 2 additions & 2 deletions src/searching/problems/examples/westeros/GOTSearchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import searching.problems.SearchAction;

public enum GOTSearchAction implements SearchAction {
STAB,
MOVE_UP,
MOVE_DOWN,
MOVE_LEFT,
MOVE_RIGHT,
STAB;
MOVE_RIGHT;

public static Iterable<GOTSearchAction> getAll() {
return Arrays.asList(GOTSearchAction.values());
Expand Down
116 changes: 56 additions & 60 deletions src/searching/problems/examples/westeros/SaveWesteros.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
import searching.exceptions.SearchProblemException;
import searching.exceptions.GameConstructionConstraintsViolation;
import searching.exceptions.UnknownGameObjectException;
import searching.exceptions.VisualizationException;
import searching.problems.SearchProblem;
import searching.problems.examples.westeros.GOTSearchState.GOTSearchStateBuilder;
import searching.utils.Geomtry;
import searching.utils.Tuple;
import searching.visualizers.Visualizer;
import searching.visualizers.StateVisualizer;
import searching.utils.ObjectUtils;

public class SaveWesteros extends SearchProblem<GOTSearchState, GOTSearchAction> {
public class SaveWesteros extends SearchProblem<GOTSearchState, GOTSearchAction, SaveWesteros> {
private GOTGameObject[][] grid;
private int gridRows;
private int gridColumns;
private ArrayList<Tuple<Integer,Integer>> whiteWalkerLocations;
private ArrayList<Tuple<Integer,Integer>> obstacleLocations;
private ArrayList<Tuple<Integer, Integer>> whiteWalkerLocations;
private ArrayList<Tuple<Integer, Integer>> obstacleLocations;
private Tuple<Integer, Integer> dragonStoneLocation;
private int whiteWalkersCount;
private int maxDragonGlass;
Expand All @@ -27,7 +28,7 @@ public class SaveWesteros extends SearchProblem<GOTSearchState, GOTSearchAction>
private int obstacleCount;

public SaveWesteros(int width, int height, int whiteWalkersCount, int obstacleCount,
int maxDragonGlass, Visualizer visualizer)
int maxDragonGlass, StateVisualizer<SaveWesteros, GOTSearchState> visualizer)
throws SearchProblemException {
super(GOTSearchAction.getAll(), visualizer);
this.gridRows = height;
Expand All @@ -44,7 +45,7 @@ public SaveWesteros(int width, int height, int whiteWalkersCount, int obstacleCo

public SaveWesteros(int width, int height, ArrayList<Tuple<Integer,Integer>> whiteWalkerLocations,
ArrayList<Tuple<Integer,Integer>> obstacleLocations, Tuple<Integer, Integer> dragonStoneLocation,
int maxDragonStones, Visualizer visualizer) throws SearchProblemException {
int maxDragonStones, StateVisualizer<SaveWesteros, GOTSearchState> visualizer) throws SearchProblemException {
super(GOTSearchAction.getAll(), visualizer);
this.gridRows = height;
this.gridColumns = width;
Expand All @@ -59,14 +60,51 @@ public SaveWesteros(int width, int height, ArrayList<Tuple<Integer,Integer>> whi
calculateLongestPathCost();
}

public SaveWesteros(char[][] grid, int maxDragonGlass, Visualizer visualizer) throws SearchProblemException {
public SaveWesteros(char[][] grid, int maxDragonGlass,
StateVisualizer<SaveWesteros, GOTSearchState> visualizer) throws GameConstructionConstraintsViolation, UnknownGameObjectException {
super(GOTSearchAction.getAll(), visualizer);
this.maxDragonGlass = maxDragonGlass;
this.fromGrid(grid);
calculateLowerBounds();
calculateLongestPathCost();
}

public GOTGameObject[][] getGrid() {
return this.grid;
}

public Tuple<Integer, Integer> getDragonStoneLocation() {
return this.dragonStoneLocation;
}

public int getGridRows() {
return gridRows;
}

public int getGridColumns() {
return gridColumns;
}

public ArrayList<Tuple<Integer, Integer>> getWhiteWalkerLocations() {
return whiteWalkerLocations;
}

public int getWhiteWalkersCount() {
return whiteWalkersCount;
}

public int getMaxDragonGlass() {
return maxDragonGlass;
}

public int getRowLowerBound() {
return rowLowerBound;
}

public ArrayList<Tuple<Integer, Integer>> getObstacleLocations() {
return this.obstacleLocations;
}

public int getObstacleCount() {
return this.obstacleCount;
}
Expand Down Expand Up @@ -122,10 +160,10 @@ private void fromGrid(char[][] grid) throws GameConstructionConstraintsViolation
}

private void initGrid(int width, int height) throws GameConstructionConstraintsViolation {
if(width < 4)
throw new GameConstructionConstraintsViolation("Grid must have a width of 4 units or more");
if(height < 4)
throw new GameConstructionConstraintsViolation("Grid must have a height of 4 units or more");
if(width < 3)
throw new GameConstructionConstraintsViolation("Grid must have a width of 3 units or more");
if(height < 3)
throw new GameConstructionConstraintsViolation("Grid must have a height of 3 units or more");

int gridCellCount = width*height;
int requiredGridCellCount = 2 + whiteWalkersCount + obstacleCount;
Expand Down Expand Up @@ -545,62 +583,20 @@ public boolean goalTest(GOTSearchState state) {
/**
* visualizes the problem before searching begins
* initial grid generation pickedUpDragonGlass boolean
* @throws VisualizationException
*/
public void visualize() {
for(int i = 0; i < this.gridRows; i++) {
for(int j = 0; j < this.gridColumns; j++)
this.getVisualizer().visualize(this.grid[i][j].toChar() + ", ");

this.getVisualizer().visualizeLine("");
}
public void visualize() throws VisualizationException {
this.visualize(getInitialState());
}


/**
* visualizes grid according to the current state
* + number of dragon-glasses carried
* + number
* @throws VisualizationException
*/
public void visualize(GOTSearchState state) {
Character[][] stateGrid = new Character[this.gridRows][this.gridColumns];

state.getWhiteWalkerStatus().forEach(status -> {
if(status.getRight())
stateGrid[status.getLeft().getLeft()][status.getLeft().getRight()] =
GOTGameObject.DEAD_WHITE_WALKER.toChar();
else
stateGrid[status.getLeft().getLeft()][status.getLeft().getRight()] =
GOTGameObject.WHITE_WALKER.toChar();
});

stateGrid[this.dragonStoneLocation.getLeft()][this.dragonStoneLocation.getRight()] =
GOTGameObject.DRAGON_STONE.toChar();

Tuple<Integer, Integer> jonSnowLocation = state.getLocation();
stateGrid[jonSnowLocation.getLeft()][jonSnowLocation.getRight()] = GOTGameObject.JON_SNOW.toChar();

this.obstacleLocations.forEach(location -> {
stateGrid[location.getLeft()][location.getRight()] = GOTGameObject.OBSTACLE.toChar();
});

/*for(int i = 0; i < state.getCurrentlyExplored().length; i++)
for(int j = 0; j < state.getCurrentlyExplored()[i].length; j++)
if(state.getCurrentlyExplored()[i][j] != null && state.getCurrentlyExplored()[i][j])
stateGrid[i][j] = 'T';*/

for(int i = 0; i < gridRows; i++)
for(int j = 0; j < gridColumns; j++)
if(stateGrid[i][j] == null)
stateGrid[i][j] = GOTGameObject.EMPTY.toChar();

for(int i = 0; i < gridRows; i++) {
for(int j = 0; j < gridColumns; j++)
this.getVisualizer().visualize(stateGrid[i][j] + ", ");
this.getVisualizer().visualizeLine("");
}

this.getVisualizer().visualizeLine("Dragon Glass Count: " + state.getDragonGlassCarried());
this.getVisualizer().visualizeLine("Picked Up Dragon Glass: " + state.getPickedUpDragonGlass());
this.getVisualizer().visualizeLine("");

public void visualize(GOTSearchState state) throws VisualizationException {
this.getVisualizer().visualize(this, state);
}
}
Loading

0 comments on commit 5538964

Please sign in to comment.