Skip to content

Commit 8e03cde

Browse files
committed
Code cleaned up.
1 parent b9d576f commit 8e03cde

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed

aima-core/src/main/java/aima/core/search/framework/qsearch/BidirectionalSearch.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class BidirectionalSearch<S, A> extends QueueSearch<S, A> {
4444
private boolean isReverseActionTestEnabled = true;
4545

4646
// index 0: original problem, index 1: reverse problem
47-
private List<Map<S, ExtendedNode<S, A>>> explored;
47+
private final List<Map<S, ExtendedNode<S, A>>> explored;
4848
private ExtendedNode<S, A> goalStateNode;
4949

5050
public BidirectionalSearch() {
@@ -78,7 +78,6 @@ public BidirectionalSearch(NodeFactory<S, A> nodeFactory) {
7878
* containing a single NoOp Action if already at the goal, or an
7979
* empty list if the goal could not be found.
8080
*/
81-
@SuppressWarnings("unchecked")
8281
public Optional<Node<S, A>> findNode(Problem<S, A> problem, Queue<Node<S, A>> frontier) {
8382
assert (problem instanceof BidirectionalProblem);
8483

@@ -103,7 +102,7 @@ public Optional<Node<S, A>> findNode(Problem<S, A> problem, Queue<Node<S, A>> fr
103102

104103
while (!isFrontierEmpty() && !Tasks.currIsCancelled()) {
105104
// choose a leaf node and remove it from the frontier
106-
ExtendedNode<S, A> node = (ExtendedNode) removeFromFrontier();
105+
ExtendedNode<S, A> node = (ExtendedNode<S, A>) removeFromFrontier();
107106
ExtendedNode<S, A> nodeFromOtherProblem;
108107

109108
// if the node contains a goal state then return the corresponding solution
@@ -156,8 +155,7 @@ protected void addToFrontier(Node<S, A> node) {
156155
* @return A node of a not yet explored state.
157156
*/
158157
protected Node<S, A> removeFromFrontier() {
159-
cleanUpFrontier(); // not really necessary because isFrontierEmpty
160-
// should be called before...
158+
cleanUpFrontier(); // not really necessary because isFrontierEmpty should be called before...
161159
Node<S, A> result = frontier.remove();
162160
updateMetrics(frontier.size());
163161
// add the node to the explored set of the corresponding problem
@@ -180,7 +178,7 @@ protected boolean isFrontierEmpty() {
180178
* head of the frontier.
181179
*/
182180
private void cleanUpFrontier() {
183-
while (!frontier.isEmpty() && isExplored(frontier.element()))
181+
while (!frontier.isEmpty() && isExplored(frontier.peek()))
184182
frontier.remove();
185183
}
186184

@@ -215,8 +213,7 @@ private Optional<Node<S, A>> getSolution(Problem<S, A> orgP, ExtendedNode<S, A>
215213

216214
/**
217215
* Returns the action which leads from the state of <code>node</code> to the
218-
* state of the node's parent, if such an action exists in problem
219-
* <code>orgP</code>.
216+
* state of the node's parent, if such an action exists in problem <code>orgP</code>.
220217
*/
221218
private A getReverseAction(Problem<S, A> orgP, Node<S, A> node) {
222219
S currState = node.getState();
@@ -230,15 +227,13 @@ private A getReverseAction(Problem<S, A> orgP, Node<S, A> node) {
230227
return null;
231228
}
232229

233-
@SuppressWarnings("unchecked")
234230
private boolean isExplored(Node<S, A> node) {
235-
ExtendedNode<S, A> eNode = (ExtendedNode) node;
231+
ExtendedNode<S, A> eNode = (ExtendedNode<S, A>) node;
236232
return explored.get(eNode.getProblemIndex()).containsKey(eNode.getState());
237233
}
238234

239-
@SuppressWarnings("unchecked")
240235
private void setExplored(Node<S, A> node) {
241-
ExtendedNode<S, A> eNode = (ExtendedNode) node;
236+
ExtendedNode<S, A> eNode = (ExtendedNode<S, A>) node;
242237
explored.get(eNode.getProblemIndex()).put(eNode.getState(), eNode);
243238
}
244239

aima-core/src/main/java/aima/core/search/framework/qsearch/GraphSearch4e.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private Node<S, A> removeFromFrontier() {
141141
* on all paths. It is assumed that the frontier contains at least one node.
142142
*/
143143
protected boolean canPossiblyBeImproved(Node<S, A> solution) {
144-
return isCheaper(frontier.element(), solution);
144+
return isCheaper(frontier.peek(), solution);
145145
}
146146

147147
/**

aima-core/src/main/java/aima/core/search/framework/qsearch/GraphSearchBFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
*/
4444
public class GraphSearchBFS<S, A> extends TreeSearch<S, A> {
4545

46-
private Set<S> explored = new HashSet<>();
47-
private Set<S> frontierStates = new HashSet<>();
46+
private final Set<S> explored = new HashSet<>();
47+
private final Set<S> frontierStates = new HashSet<>();
4848

4949
public GraphSearchBFS() {
5050
this(new NodeFactory<>());

aima-core/src/main/java/aima/core/search/framework/qsearch/QueueSearch.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ protected void updateMetrics(int queueSize) {
9292
}
9393

9494
protected Optional<Node<S, A>> asOptional(Node<S, A> node) {
95-
metrics.set(METRIC_PATH_COST, node.getPathCost());
96-
return Optional.of(node);
95+
if (node != null)
96+
metrics.set(METRIC_PATH_COST, node.getPathCost());
97+
return Optional.ofNullable(node);
9798
}
9899
}

aima-core/src/main/java/aima/core/search/uninformed/DepthLimitedSearch.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ public Optional<S> findState(Problem<S, A> p) {
8989

9090
public Optional<Node<S, A>> findNode(Problem<S, A> p) {
9191
clearMetrics();
92-
// return RECURSIVE-DLS(MAKE-NODE(INITIAL-STATE[problem]), problem,
93-
// limit)
92+
// return RECURSIVE-DLS(MAKE-NODE(INITIAL-STATE[problem]), problem, limit)
9493
Node<S, A> node = recursiveDLS(nodeFactory.createNode(p.getInitialState()), p, limit);
95-
return node != null ? Optional.of(node) : Optional.empty();
94+
return Optional.ofNullable(node);
9695
}
9796

9897
// function RECURSIVE-DLS(node, problem, limit) returns a solution, or

0 commit comments

Comments
 (0)