/
ModelBasedReflexAgentProgram.cs
133 lines (116 loc) · 3.73 KB
/
ModelBasedReflexAgentProgram.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Collections.Generic;
using aima.core.agent;
using aima.core.agent.impl.aprog.simplerule;
namespace aima.core.agent.impl.aprog
{
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.12, page
* 51.<br>
* <br>
*
* <pre>
* function MODEL-BASED-REFLEX-AGENT(percept) returns an action
* persistent: state, the agent's current conception of the world state
* model, a description of how the next state depends on current state and action
* rules, a set of condition-action rules
* action, the most recent action, initially none
*
* state <- UPDATE-STATE(state, action, percept, model)
* rule <- RULE-MATCH(state, rules)
* action <- rule.ACTION
* return action
* </pre>
*
* Figure 2.12 A model-based reflex agent. It keeps track of the current state
* of the world using an internal model. It then chooses an action in the same
* way as the reflex agent.
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*
*/
public abstract class ModelBasedReflexAgentProgram : AgentProgram
{
// persistent: state, the agent's current conception of the world state
private DynamicState state = null;
// model, a description of how the next state depends on current state and
// action
private Model model = null;
// rules, a set of condition-action rules
private HashSet<Rule> rules = null;
// action, the most recent action, initially none
private Action action = null;
public ModelBasedReflexAgentProgram()
{
init();
}
/**
* Set the agent's current conception of the world state.
*
* @param state
* the agent's current conception of the world state.
*/
public void setState(DynamicState dstate)
{
state = dstate;
}
/**
* Set the program's description of how the next state depends on the state
* and action.
*
* @param model
* a description of how the next state depends on the current
* state and action.
*/
public void setModel(Model mod)
{
model = mod;
}
/**
* Set the program's condition-action rules
*
* @param ruleSet
* a set of condition-action rules
*/
public void setRules(HashSet<Rule> ruleSet)
{
rules = ruleSet;
}
//START-AgentProgram
// function MODEL-BASED-REFLEX-AGENT(percept) returns an action
public Action execute(Percept percept)
{
// state <- UPDATE-STATE(state, action, percept, model)
state = updateState(state, action, percept, model);
// rule <- RULE-MATCH(state, rules)
Rule rule = ruleMatch(state, rules);
// action <- rule.ACTION
action = ruleAction(rule);
// return action
return action;
}
// END-AgentProgram
// PROTECTED METHODS
/**
* Realizations of this class should implement the init() method so that it
* calls the setState(), setModel(), and setRules() method.
*/
protected abstract void init();
protected abstract DynamicState updateState(DynamicState state, Action action, Percept percept, Model model);
protected Rule ruleMatch(DynamicState state, HashSet<Rule> rules)
{
foreach (Rule r in rules)
{
if (r.evaluate(state))
{
return r;
}
}
return null;
}
protected Action ruleAction(Rule r)
{
return null == r ? NoOpAction.NO_OP : r.getAction();
}
}
}