/
TableDrivenAgentProgram.cs
92 lines (78 loc) · 2.81 KB
/
TableDrivenAgentProgram.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using aima.core.agent;
using aima.core.agent.impl;
using aima.core.util;
namespace aima.core.agent.impl.aprog
{
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.7, page 47.<br>
* <br>
*
* <pre>
* 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
* </pre>
*
* 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 : AgentProgram
{
private List<Percept> percepts = new List<Percept>();
private Table<List<Percept>, System.String, Action> table;
private const System.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(Dictionary<List<Percept>, Action> perceptSequenceActions)
{
List<List<Percept>> rowHeaders = new List<List<Percept>>(perceptSequenceActions.Keys);
List<System.String> colHeaders = new List<System.String>();
colHeaders.Add(ACTION);
table = new Table<List<Percept>, System.String, Action>(rowHeaders, colHeaders);
foreach (List<Percept> row in rowHeaders)
{
table.set(row, ACTION, perceptSequenceActions[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;
}
}
}