diff --git a/src/Problem.java b/src/Problem.java index 6f6df68..bbf2688 100644 --- a/src/Problem.java +++ b/src/Problem.java @@ -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 */ @@ -31,10 +32,10 @@ public State getInitialState() { abstract public int pathCost(List 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; } } diff --git a/src/Search.java b/src/Search.java index 65cd044..2f60e16 100644 --- a/src/Search.java +++ b/src/Search.java @@ -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 @@ -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); diff --git a/src/SearchAStar.java b/src/SearchAStar.java index f368b92..bc03594 100644 --- a/src/SearchAStar.java +++ b/src/SearchAStar.java @@ -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 diff --git a/src/SearchBFS.java b/src/SearchBFS.java index bdcd8e1..f76b0b7 100644 --- a/src/SearchBFS.java +++ b/src/SearchBFS.java @@ -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 diff --git a/src/SearchDLS.java b/src/SearchDLS.java index 3bee8df..5cf5cb7 100644 --- a/src/SearchDLS.java +++ b/src/SearchDLS.java @@ -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); @@ -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()); } @@ -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; - } } diff --git a/src/SearchGreedyBFS.java b/src/SearchGreedyBFS.java index d6faa4b..7fa9158 100644 --- a/src/SearchGreedyBFS.java +++ b/src/SearchGreedyBFS.java @@ -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 diff --git a/src/SearchUCS.java b/src/SearchUCS.java index fd54c89..7fbc848 100644 --- a/src/SearchUCS.java +++ b/src/SearchUCS.java @@ -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