Skip to content

Commit

Permalink
add more detail to javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
aliarjomandbigdeli committed Dec 31, 2018
1 parent a44deff commit e34cb7f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public State getInitialState() {

/**
* The goal test, which determines whether a given state is a goal state
*
* @param state given state
* @return a boolean which determines whether a given state is a goal state
*/
Expand All @@ -31,10 +32,10 @@ public State getInitialState() {
abstract public int pathCost(List<Integer> path);

/**
* heuristic function
* heuristic function, the cost to get from the node to the goal
* you should override it based on your problem to use AStar search
*/
public int h(State state){
public int h(State state) {
return 0;
}
}
6 changes: 4 additions & 2 deletions src/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* this class is the parent of all searches you want to define with this interface
* you can use all searches in both tree or graph implementation by determining isGraph parameter
*
* @author Ali ArjomandBigdeli
* @since 12.27.2018
Expand Down Expand Up @@ -57,9 +58,10 @@ public Problem getProblem() {

abstract public void execute();

public void search(){}
public void search() {
}

protected void createSolutionPath(State state){
protected void createSolutionPath(State state) {
State temp = state;
while (temp != null) {
path.add(temp.act);
Expand Down
2 changes: 2 additions & 0 deletions src/SearchAStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/**
* A* search algorithm
* It evaluates nodes by combining the cost to reach the node(pathCost),
* and h(n), the cost to get from the node to the goal.
*
* @author Ali ArjomandBigdeli
* @since 12.27.2018
Expand Down
2 changes: 2 additions & 0 deletions src/SearchBFS.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* Breath First Search(BFS) algorithm
* Breadth-first search is a simple strategy in which the root node is expanded first,
* then all the successors of the root node are expanded next, then their successors, and so on.
*
* @author Ali ArjomandBigdeli
* @since 12.27.2018
Expand Down
38 changes: 14 additions & 24 deletions src/SearchDLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public void search() {
* @return 1 assigns to result, 0 assigns to cutoff and -1 assigns to failure
*/
public int search(State node, int limit) {
if (!isGraph && cycleDetection(node)) {
System.out.println("cycle detected, algorithm can't solve this problem in tree mode");
return -1;
}

if (problem.goalTest(node)) {
answer = node;
createSolutionPath(node);
Expand All @@ -55,17 +50,23 @@ public int search(State node, int limit) {
State child = problem.nextState(node, action);
nodeSeen++;
if (isGraph) {
if (e.contains(child)) {
continue;
if (!e.contains(child)) {
e.add(node);
int result = search(child, limit - 1);
if (result == 0) {
cutoffOccurred = true;
} else if (result != -1) {
return result;
}
}
e.add(node);
} else {
int result = search(child, limit - 1);
if (result == 0)
cutoffOccurred = true;
else if (result != -1)
return result;
}

int result = search(child, limit - 1);
if (result == 0)
cutoffOccurred = true;
else if (result != -1)
return result;
maxNodeKeptInMemory = Integer.max(maxNodeKeptInMemory, e.size());

}
Expand All @@ -81,15 +82,4 @@ else if (result != -1)
}

}


private boolean cycleDetection(State node) {
State temp = node;
while (temp != null) {
temp = temp.parent;
if (temp != null && temp.equals(node))
return true;
}
return false;
}
}
3 changes: 3 additions & 0 deletions src/SearchGreedyBFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/**
* greedy best first search algorithm
* Greedy best-first search tries to expand the node that is closest to the goal,
* on the grounds that this is likely to lead to a solution quickly.
* Thus, it evaluates nodes by using just the heuristic function(h).
*
* @author Ali ArjomandBigdeli
* @since 12.27.2018
Expand Down
1 change: 1 addition & 0 deletions src/SearchUCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* uniform cost search algorithm
* it sort frontier list by pathCost and an extra check in case a shorter path to a frontier state is discovered
*
* @author Ali ArjomandBigdeli
* @since 12.27.2018
Expand Down

0 comments on commit e34cb7f

Please sign in to comment.