aima.core.agent.impl.aprog.TableDrivenAgentProgram Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.agent.impl.aprog;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import aima.core.agent.Action;
import aima.core.agent.AgentProgram;
import aima.core.agent.Percept;
import aima.core.agent.impl.NoOpAction;
import aima.core.util.datastructure.Table;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.7, page 47.
*
*
*
* function TABLE-DRIVEN-AGENT(percept) returns an action
* persistent: percepts, a sequence, initially empty
* table, a table of actions, indexed by percept sequences, initially fully specified
*
* append percept to end of percepts
* action <- LOOKUP(percepts, table)
* return action
*
*
* Figure 2.7 The TABLE-DRIVEN-AGENT program is invoked for each new percept and
* returns an action each time. It retains the complete percept sequence in
* memory.
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*
*/
public class TableDrivenAgentProgram implements AgentProgram {
private List percepts = new ArrayList();
private Table, String, Action> table;
private static final String ACTION = "action";
// persistent: percepts, a sequence, initially empty
// table, a table of actions, indexed by percept sequences, initially fully
// specified
/**
* Constructs a TableDrivenAgentProgram with a table of actions, indexed by
* percept sequences.
*
* @param perceptSequenceActions
* a table of actions, indexed by percept sequences
*/
public TableDrivenAgentProgram(
Map, Action> perceptSequenceActions) {
List> rowHeaders = new ArrayList>(
perceptSequenceActions.keySet());
List colHeaders = new ArrayList();
colHeaders.add(ACTION);
table = new Table, String, Action>(rowHeaders, colHeaders);
for (List row : rowHeaders) {
table.set(row, ACTION, perceptSequenceActions.get(row));
}
}
//
// START-AgentProgram
// function TABLE-DRIVEN-AGENT(percept) returns an action
public Action execute(Percept percept) {
// append percept to end of percepts
percepts.add(percept);
// action <- LOOKUP(percepts, table)
// return action
return lookupCurrentAction();
}
// END-AgentProgram
//
//
// PRIVATE METHODS
//
private Action lookupCurrentAction() {
Action action = null;
action = table.get(percepts, ACTION);
if (null == action) {
action = NoOpAction.NO_OP;
}
return action;
}
}