Skip to content

Commit

Permalink
Merge pull request #2 from michael-kamel/problems/examples
Browse files Browse the repository at this point in the history
Finalized SaveWesteros
  • Loading branch information
michael-kamel authored Oct 11, 2018
2 parents f15c4db + 7fb93e2 commit 8fd4b2f
Show file tree
Hide file tree
Showing 41 changed files with 1,835 additions and 143 deletions.
145 changes: 145 additions & 0 deletions src/HardCodedExamples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
1
., O, ., ., .,
O, S, O, ., W,
O, W, O, ., W,
W, ., ., ., .,
., ., O, ., J,


2
O, ., O, ., ., .,
., ., W, ., W, O,
., O, O, W, W, O,
., O, S, ., ., .,
., ., ., ., O, .,
., ., O, ., ., J,


3
W, ., ., ., ., .,
., ., O, O, O, .,
W, O, O, O, O, .,
., ., ., S, O, .,
., O, O, O, O, W,
., ., ., ., ., J,


4
W, O, ., ., ., W,
O, ., O, ., ., .,
W, O, O, O, O, .,
., ., ., S, O, .,
., O, O, O, O, W,
., ., ., ., ., J,


5
., ., W, .,
., W, ., .,
O, ., W, O,
., O, S, J,


6
O, O, O, ., W, .,
O, O, O, ., O, .,
O, O, O, ., O, .,
O, O, O, W, O, .,
W, O, ., W, O, S,
W, W, ., W, W, J,


7
O, O, O, ., ., .,
O, O, O, W, O, .,
O, O, O, W, O, .,
O, O, O, W, O, W,
O, ., ., ., O, S,
O, W, ., W, W, J,


8
O, O, ., ., .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, ., ., O, S,
W, ., W, W, J,


9
O, O, ., ., .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, ., ., O, S,
W, ., W, W, J,


10
O, O, ., ., .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, O, W, O, .,
O, ., ., O, S,
W, ., W, W, J,


11
W, ., ., ., ., .,
., O, O, O, O, W,
., O, O, O, O, W,
W, O, O, O, O, .,
., ., O, O, O, S,
W, ., W, W, ., J,


12
S, ., ., ., ., .,
., O, O, O, O, W,
., O, O, O, O, W,
W, O, O, O, O, .,
., ., O, O, O, .,
W, ., W, W, ., J,


13
W, ., ., ., W, .,
O, W, O, O, O, .,
O, W, O, O, O, W,
W, ., O, O, O, S,
W, ., O, O, O, .,
O, ., O, O, O, .,
O, W, ., ., W, J,


14
W, ., ., W, S, ., ., ., ., ., ., ., ., ., ., ., ., ., ., .,
O, W, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, .,
O, W, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, .,
W, ., O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, .,
O, ., O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, .,
O, ., O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, .,
O, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., J,


15
O, ., O, ., ., .,
., ., W, ., W, O,
., O, O, W, W, O,
., O, S, ., ., .,
., ., ., ., O, .,
., ., O, ., ., J,







44 changes: 22 additions & 22 deletions src/searching/agents/SearchAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@

import java.util.Optional;

import searching.problems.SearchAction;
import searching.problems.SearchProblem;
import searching.structs.SearchAction;
import searching.structs.SearchProblemSolution;
import searching.structs.SearchTree;
import searching.structs.SearchTreeNode;
import searching.problems.SearchProblemSolution;
import searching.problems.SearchState;
import searching.strategies.SearchStrategy;

public abstract class SearchAgent {
private SearchTree stateTree;
private int maxTreeNodes;
public class SearchAgent<T extends SearchState, V extends SearchAction> {
//sets an upper-bound to the number of expanded nodes to avoid running forever
private int maxTreeNodes;

public SearchAgent(int maxTreeNodes) {
this.maxTreeNodes = maxTreeNodes;
}

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

searchStrategy.addNode(rootNode);
int count = 0; //number of _expanded_ nodes, so far

SearchTreeNode rootNode = new SearchTreeNode(Optional.empty(), 0, problem.getInitialState(), SearchAction.NoAction());
stateTree.append(rootNode);
int count = 0;
while(count <= maxTreeNodes) {
Optional<SearchTreeNode> nodeToCheck = searchStrategy.getNext();

if(!nodeToCheck.isPresent())
return SearchProblemSolution.Failure();

//gets the next node; dequeuing/popping (the data structure is strategy-dependent)
Optional<SearchTreeNode<T, V>> nodeToCheck = searchStrategy.getNext();

if(problem.getGoalTest().isGoal(nodeToCheck.get().getCurrentState()))
return new SearchProblemSolution(Optional.of(nodeToCheck.get()));
if(!nodeToCheck.isPresent()) //emptied the data structure; no more nodes to expand
return SearchProblemSolution.NoSolution(problem, count);

Iterable<SearchTreeNode> nodesToAdd = problem.expand(nodeToCheck.get());
searchStrategy.addNodes(nodesToAdd);
if(problem.goalTest(nodeToCheck.get().getCurrentState()))
return new SearchProblemSolution<T, V>(problem, Optional.of(nodeToCheck.get()), count);

count++;
searchStrategy.addNodes(problem.expand(nodeToCheck.get()));//pushes/queues nodes
count++;
}

throw new UnsupportedOperationException();//change this
return SearchProblemSolution.Bottom(problem, count); //when resources are exhausted
}
}
10 changes: 0 additions & 10 deletions src/searching/agents/SearchStrategy.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/searching/agents/SearchTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package searching.agents;

import java.util.Optional;

import searching.problems.SearchAction;
import searching.problems.SearchState;

public class SearchTreeNode<T extends SearchState, V extends SearchAction> implements Comparable<SearchTreeNode<T, V>> {
private final Optional<SearchTreeNode<T, V>> parent; //Parent of node
private final long cost; // Path cost from the root of the tree until this node
private final T state; // The current state of the node
private final Optional<V> action; // The action made to reach this node from its parent
private final int depth; //depth from root to this node; root depth==0

public SearchTreeNode(Optional<SearchTreeNode<T, V>> parent, long cost, T state, Optional<V> action, int depth) {
this.parent = parent;
this.cost = cost;
this.state = state;
this.action = action;
this.depth = depth;
}

public Optional<SearchTreeNode<T, V>> getParent() {
return parent;
}

public long getCost() {
return cost;
}

public T getCurrentState() {
return state;
}

public Optional<V> getAction() {
return action;
}

public int getDepth() {
return depth;
}

@Override
public int compareTo(SearchTreeNode<T, V> node) {
return this.cost > node.cost ? 1 : this.cost == node.cost ? 0 : -1;
}

}
12 changes: 12 additions & 0 deletions src/searching/exceptions/GameConstructionConstraintsViolation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package searching.exceptions;
/*
* used for handling _problem-specific_ constraints
* */
public class GameConstructionConstraintsViolation extends SearchProblemException {
private static final long serialVersionUID = 1L;

public GameConstructionConstraintsViolation(String message) {
super(message);
}

}
9 changes: 9 additions & 0 deletions src/searching/exceptions/SearchProblemException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package searching.exceptions;

public abstract class SearchProblemException extends SearchingException {
private static final long serialVersionUID = 1L;

public SearchProblemException(String message) {
super(message);
}
}
9 changes: 9 additions & 0 deletions src/searching/exceptions/SearchingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package searching.exceptions;

public abstract class SearchingException extends Exception {
private static final long serialVersionUID = 1L;

public SearchingException(String message) {
super(message);
}
}
9 changes: 9 additions & 0 deletions src/searching/exceptions/UnknownGameObjectException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package searching.exceptions;

public class UnknownGameObjectException extends SearchProblemException {
private static final long serialVersionUID = 1L;

public UnknownGameObjectException(String message) {
super(message);
}
}
18 changes: 18 additions & 0 deletions src/searching/problems/HeuristicFunctions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package searching.problems;

import searching.strategies.HeuristicFunction;

public class HeuristicFunctions {
/**
* a function that calculates the dominant of two heuristic functions
* @param first
* @param second
* @return {@link HeuristicFunction}
*/
public static <T extends SearchState> HeuristicFunction<T> dominating(HeuristicFunction<T> first,
HeuristicFunction<T> second) {
return state -> {
return Math.max(first.expectedCostToSolution(state), second.expectedCostToSolution(state));
};
}
}
3 changes: 3 additions & 0 deletions src/searching/problems/SearchAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package searching.problems;

public interface SearchAction {}
Loading

0 comments on commit 8fd4b2f

Please sign in to comment.