Skip to content

Commit

Permalink
Special AIMA Queue interface replaced by standard Java Queue interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruediger.lunde@gmail.com committed Jun 6, 2016
1 parent 6cca13f commit 6540389
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 209 deletions.
Expand Up @@ -107,24 +107,24 @@ public Action execute(Percept percept) {
Object currentStep = this.stack.peek(); Object currentStep = this.stack.peek();
// push... // push...
if (currentStep instanceof Action) { if (currentStep instanceof Action) {
return (Action) this.stack.pop(); return (Action) this.stack.remove();
} // case: next step is a plan } // case: next step is a plan
else if (currentStep instanceof Plan) { else if (currentStep instanceof Plan) {
Plan newPlan = (Plan) currentStep; Plan newPlan = (Plan) currentStep;
if (newPlan.size() > 0) { if (newPlan.size() > 0) {
this.stack.push(newPlan.removeFirst()); this.stack.push(newPlan.removeFirst());
} else { } else {
this.stack.pop(); this.stack.remove();
} }
return this.execute(percept); return this.execute(percept);
} // case: next step is an if-then } // case: next step is an if-then
else if (currentStep instanceof IfStateThenPlan) { else if (currentStep instanceof IfStateThenPlan) {
IfStateThenPlan conditional = (IfStateThenPlan) this.stack.pop(); IfStateThenPlan conditional = (IfStateThenPlan) this.stack.remove();
this.stack.push(conditional.ifStateMatches(percept)); this.stack.push(conditional.ifStateMatches(percept));
return this.execute(percept); return this.execute(percept);
} // case: ignore next step if null } // case: ignore next step if null
else if (currentStep == null) { else if (currentStep == null) {
this.stack.pop(); this.stack.remove();
return this.execute(percept); return this.execute(percept);
} else { } else {
throw new RuntimeException("Unrecognized contingency plan step."); throw new RuntimeException("Unrecognized contingency plan step.");
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Set; import java.util.Set;


import aima.core.agent.Action; import aima.core.agent.Action;
Expand All @@ -23,7 +24,6 @@
import aima.core.search.informed.AStarSearch; import aima.core.search.informed.AStarSearch;
import aima.core.util.SetOps; import aima.core.util.SetOps;
import aima.core.util.datastructure.FIFOQueue; import aima.core.util.datastructure.FIFOQueue;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): page 270.<br> * Artificial Intelligence A Modern Approach (3rd Edition): page 270.<br>
Expand Down Expand Up @@ -154,7 +154,7 @@ public Action execute(Percept percept) {
plan.add(new Climb()); plan.add(new Climb());
} }
// action <- POP(plan) // action <- POP(plan)
Action action = plan.pop(); Action action = plan.remove();
// TELL(KB, MAKE-ACTION-SENTENCE(action, t)) // TELL(KB, MAKE-ACTION-SENTENCE(action, t))
kb.makeActionSentence(action, t); kb.makeActionSentence(action, t);
// t <- t+1 // t <- t+1
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.Set; import java.util.Set;


import aima.core.logic.propositional.kb.KnowledgeBase; import aima.core.logic.propositional.kb.KnowledgeBase;
Expand All @@ -11,7 +12,6 @@
import aima.core.logic.propositional.visitors.ConvertToConjunctionOfClauses; import aima.core.logic.propositional.visitors.ConvertToConjunctionOfClauses;
import aima.core.logic.propositional.visitors.SymbolCollector; import aima.core.logic.propositional.visitors.SymbolCollector;
import aima.core.util.datastructure.FIFOQueue; import aima.core.util.datastructure.FIFOQueue;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): page 258.<br> * Artificial Intelligence A Modern Approach (3rd Edition): page 258.<br>
Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean plfcEntails(KnowledgeBase kb, PropositionSymbol q) {
// while agenda is not empty do // while agenda is not empty do
while (!agenda.isEmpty()) { while (!agenda.isEmpty()) {
// p <- Pop(agenda) // p <- Pop(agenda)
PropositionSymbol p = agenda.pop(); PropositionSymbol p = agenda.remove();
// if p = q then return true // if p = q then return true
if (p.equals(q)) { if (p.equals(q)) {
return true; return true;
Expand Down
Expand Up @@ -83,7 +83,7 @@ public DomainRestoreInfo reduceDomains(Variable var, Object value, CSP csp) {
private void reduceDomains(FIFOQueue<Variable> queue, CSP csp, private void reduceDomains(FIFOQueue<Variable> queue, CSP csp,
DomainRestoreInfo info) { DomainRestoreInfo info) {
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Variable var = queue.pop(); Variable var = queue.remove();
for (Constraint constraint : csp.getConstraints(var)) { for (Constraint constraint : csp.getConstraints(var)) {
if (constraint.getScope().size() == 2) { if (constraint.getScope().size() == 2) {
Variable neighbor = csp.getNeighbor(var, constraint); Variable neighbor = csp.getNeighbor(var, constraint);
Expand Down
Expand Up @@ -2,10 +2,10 @@


import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.PriorityQueue;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.qsearch.QueueSearch; import aima.core.search.framework.qsearch.QueueSearch;
import aima.core.util.datastructure.PriorityQueue;


/** /**
* Maintains a {@link QueueSearch} implementation and a node comparator. Search * Maintains a {@link QueueSearch} implementation and a node comparator. Search
Expand Down
Expand Up @@ -4,14 +4,14 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.BidirectionalProblem; import aima.core.search.framework.BidirectionalProblem;
import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.search.framework.SearchUtils; import aima.core.search.framework.SearchUtils;
import aima.core.util.CancelableThread; import aima.core.util.CancelableThread;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): page 90.<br> * Artificial Intelligence A Modern Approach (3rd Edition): page 90.<br>
Expand Down Expand Up @@ -136,7 +136,7 @@ public void setReverseActionTestEnabled(boolean state) {
@Override @Override
protected void insertIntoFrontier(Node node) { protected void insertIntoFrontier(Node node) {
if (!isExplored(node)) { if (!isExplored(node)) {
frontier.insert(node); frontier.add(node);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
} }
Expand All @@ -149,7 +149,7 @@ protected void insertIntoFrontier(Node node) {
*/ */
@Override @Override
protected Node popNodeFromFrontier() { protected Node popNodeFromFrontier() {
Node result = frontier.pop(); Node result = frontier.remove();
// add the node to the explored set of the corresponding problem // add the node to the explored set of the corresponding problem
setExplored(result); setExplored(result);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
Expand All @@ -163,7 +163,7 @@ protected Node popNodeFromFrontier() {
@Override @Override
protected boolean isFrontierEmpty() { protected boolean isFrontierEmpty() {
while (!frontier.isEmpty() && isExplored(frontier.peek())) { while (!frontier.isEmpty() && isExplored(frontier.peek())) {
frontier.pop(); frontier.remove();
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
return frontier.isEmpty(); return frontier.isEmpty();
Expand Down
Expand Up @@ -2,12 +2,12 @@


import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Set; import java.util.Set;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77. * Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77.
Expand Down Expand Up @@ -63,7 +63,7 @@ public List<Action> search(Problem problem, Queue<Node> frontier) {
@Override @Override
protected void insertIntoFrontier(Node node) { protected void insertIntoFrontier(Node node) {
if (!explored.contains(node.getState())) { if (!explored.contains(node.getState())) {
frontier.insert(node); frontier.add(node);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
} }
Expand All @@ -78,7 +78,7 @@ protected void insertIntoFrontier(Node node) {
*/ */
@Override @Override
protected Node popNodeFromFrontier() { protected Node popNodeFromFrontier() {
Node result = frontier.pop(); Node result = frontier.remove();
// add the node to the explored set // add the node to the explored set
explored.add(result.getState()); explored.add(result.getState());
updateMetrics(frontier.size()); updateMetrics(frontier.size());
Expand All @@ -92,7 +92,7 @@ protected Node popNodeFromFrontier() {
@Override @Override
protected boolean isFrontierEmpty() { protected boolean isFrontierEmpty() {
while (!frontier.isEmpty() && explored.contains(frontier.peek().getState())) while (!frontier.isEmpty() && explored.contains(frontier.peek().getState()))
frontier.pop(); frontier.remove();
updateMetrics(frontier.size()); updateMetrics(frontier.size());
return frontier.isEmpty(); return frontier.isEmpty();
} }
Expand Down
Expand Up @@ -2,12 +2,12 @@


import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Set; import java.util.Set;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77. * Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77.
Expand Down Expand Up @@ -62,7 +62,7 @@ public List<Action> search(Problem problem, Queue<Node> frontier) {
@Override @Override
protected void insertIntoFrontier(Node node) { protected void insertIntoFrontier(Node node) {
if (!explored.contains(node.getState()) && !frontierStates.contains(node.getState())) { if (!explored.contains(node.getState()) && !frontierStates.contains(node.getState())) {
frontier.insert(node); frontier.add(node);
frontierStates.add(node.getState()); frontierStates.add(node.getState());
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
Expand All @@ -76,7 +76,7 @@ protected void insertIntoFrontier(Node node) {
*/ */
@Override @Override
protected Node popNodeFromFrontier() { protected Node popNodeFromFrontier() {
Node result = frontier.pop(); Node result = frontier.remove();
explored.add(result.getState()); explored.add(result.getState());
frontierStates.remove(result.getState()); frontierStates.remove(result.getState());
updateMetrics(frontier.size()); updateMetrics(frontier.size());
Expand Down
Expand Up @@ -5,13 +5,13 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set; import java.util.Set;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.util.datastructure.PriorityQueue;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77. * Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77.
Expand Down Expand Up @@ -82,7 +82,7 @@ protected void insertIntoFrontier(Node node) {
if (null == frontierNode) { if (null == frontierNode) {
if (!explored.contains(node.getState())) { if (!explored.contains(node.getState())) {
// child.STATE is not in frontier and not yet explored // child.STATE is not in frontier and not yet explored
frontier.insert(node); frontier.add(node);
frontierState.put(node.getState(), node); frontierState.put(node.getState(), node);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
Expand All @@ -91,7 +91,7 @@ protected void insertIntoFrontier(Node node) {
// replace that frontier node with child // replace that frontier node with child
if (frontier.remove(frontierNode)) if (frontier.remove(frontierNode))
frontierState.remove(frontierNode.getState()); frontierState.remove(frontierNode.getState());
frontier.insert(node); frontier.add(node);
frontierState.put(node.getState(), node); frontierState.put(node.getState(), node);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }
Expand All @@ -105,7 +105,7 @@ protected void insertIntoFrontier(Node node) {
*/ */
@Override @Override
protected Node popNodeFromFrontier() { protected Node popNodeFromFrontier() {
Node result = frontier.pop(); Node result = frontier.remove();
// add the node to the explored set // add the node to the explored set
explored.add(result.getState()); explored.add(result.getState());
frontierState.remove(result.getState()); frontierState.remove(result.getState());
Expand Down
@@ -1,14 +1,14 @@
package aima.core.search.framework.qsearch; package aima.core.search.framework.qsearch;


import java.util.List; import java.util.List;
import java.util.Queue;


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.search.framework.Metrics; import aima.core.search.framework.Metrics;
import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.search.framework.SearchUtils; import aima.core.search.framework.SearchUtils;
import aima.core.util.CancelableThread; import aima.core.util.CancelableThread;
import aima.core.util.datastructure.Queue;


/** /**
* Base class for queue-based search implementations, especially for {@link TreeSearch}, * Base class for queue-based search implementations, especially for {@link TreeSearch},
Expand Down
@@ -1,8 +1,9 @@
package aima.core.search.framework.qsearch; package aima.core.search.framework.qsearch;


import java.util.Queue;

import aima.core.search.framework.Node; import aima.core.search.framework.Node;
import aima.core.search.framework.Problem; import aima.core.search.framework.Problem;
import aima.core.util.datastructure.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77. * Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.7, page 77.
Expand Down Expand Up @@ -36,7 +37,7 @@ public class TreeSearch extends QueueSearch {
*/ */
@Override @Override
protected void insertIntoFrontier(Node node) { protected void insertIntoFrontier(Node node) {
frontier.insert(node); frontier.add(node);
updateMetrics(frontier.size()); updateMetrics(frontier.size());
} }


Expand All @@ -47,7 +48,7 @@ protected void insertIntoFrontier(Node node) {
*/ */
@Override @Override
protected Node popNodeFromFrontier() { protected Node popNodeFromFrontier() {
Node result = frontier.pop(); Node result = frontier.remove();
updateMetrics(frontier.size()); updateMetrics(frontier.size());
return result; return result;
} }
Expand Down
Expand Up @@ -88,8 +88,8 @@ List<Action> searchOutOfOrder(Problem p) throws Exception {


Node opNode = new Node(op.getInitialState()); Node opNode = new Node(op.getInitialState());
Node rpNode = new Node(rp.getInitialState()); Node rpNode = new Node(rp.getInitialState());
opFrontier.insert(opNode); opFrontier.add(opNode);
rpFrontier.insert(rpNode); rpFrontier.add(rpNode);


setQueueSize(opFrontier.size() + rpFrontier.size()); setQueueSize(opFrontier.size() + rpFrontier.size());
//setNodesExpanded(ogs.getNodesExpanded() + rgs.getNodesExpanded()); // TODO //setNodesExpanded(ogs.getNodesExpanded() + rgs.getNodesExpanded()); // TODO
Expand All @@ -99,15 +99,15 @@ List<Action> searchOutOfOrder(Problem p) throws Exception {
// in preparation for testing whether or not the two // in preparation for testing whether or not the two
// searches meet or one or other is at the GOAL. // searches meet or one or other is at the GOAL.
if (!opFrontier.isEmpty()) { if (!opFrontier.isEmpty()) {
opNode = opFrontier.pop(); opNode = opFrontier.remove();
// opFrontier.addAll(ogs.getResultingNodesToAddToFrontier(opNode, // opFrontier.addAll(ogs.getResultingNodesToAddToFrontier(opNode,
// // TODO // // TODO
// op)); // op));
} else { } else {
opNode = null; opNode = null;
} }
if (!rpFrontier.isEmpty()) { if (!rpFrontier.isEmpty()) {
rpNode = rpFrontier.pop(); rpNode = rpFrontier.remove();
// rpFrontier.addAll(rgs.getResultingNodesToAddToFrontier(rpNode, // rpFrontier.addAll(rgs.getResultingNodesToAddToFrontier(rpNode,
// // TODO // // TODO
// rp)); // rp));
Expand Down Expand Up @@ -417,8 +417,8 @@ public Node getNodeBasedOn(Object state) {


// //
// START-Queue // START-Queue
public E pop() { public E remove() {
E popped = super.pop(); E popped = super.remove();
cachedState.remove(((Node) popped).getState()); cachedState.remove(((Node) popped).getState());
return popped; return popped;
} }
Expand Down
Expand Up @@ -2,15 +2,17 @@


import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue;


/** /**
* Artificial Intelligence A Modern Approach (3rd Edition): pg 80.<br> * Artificial Intelligence A Modern Approach (3rd Edition): pg 80.<br>
* <br> * <br>
* First-in, first-out or FIFO queue, which pops the oldest element of the * First-in, first-out or FIFO queue, which pops the oldest element of the
* queue; * queue. This implementation is in fact a renamed LinkedList.
* *
* @author Ravi Mohan * @author Ravi Mohan
* @author Ciaran O'Reilly * @author Ciaran O'Reilly
* @author Ruediger Lunde
*/ */
public class FIFOQueue<E> extends LinkedList<E> implements Queue<E> { public class FIFOQueue<E> extends LinkedList<E> implements Queue<E> {
private static final long serialVersionUID = 1; private static final long serialVersionUID = 1;
Expand All @@ -22,27 +24,4 @@ public FIFOQueue() {
public FIFOQueue(Collection<? extends E> c) { public FIFOQueue(Collection<? extends E> c) {
super(c); super(c);
} }

//
// START-Queue
public boolean isEmpty() {
return 0 == size();
}

public E pop() {
return poll();
}

public void push(E element) {
this.addLast(element);
}

public Queue<E> insert(E element) {
if (offer(element)) {
return this;
}
return null;
}
// END-Queue
//
} }

0 comments on commit 6540389

Please sign in to comment.