forked from aimacode/aima-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleReflexAgentProgram.cs
88 lines (79 loc) · 2.39 KB
/
SimpleReflexAgentProgram.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
using System;
using System.Collections.Generic;
using aima.core.agent;
using aima.core.agent.impl;
using aima.core.agent.impl.aprog.simplerule;
namespace aima.core.agent.impl.aprog
{
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 2.10, page
* 49.<br>
* <br>
*
* <pre>
* function SIMPLE-RELEX-AGENT(percept) returns an action
* persistent: rules, a set of condition-action rules
*
* state <- INTERPRET-INPUT(percept);
* rule <- RULE-MATCH(state, rules);
* action <- rule.ACTION;
* return action
* </pre>
* Figure 2.10 A simple reflex agent. It acts according to a rule whose
* condition matches the current state, as defined by the percept.
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*
*/
public class SimpleReflexAgentProgram : AgentProgram
{
// persistent: rules, a set of condition-action rules
private HashSet<Rule> rules;
/**
* Constructs a SimpleReflexAgentProgram with a set of condition-action
* rules.
*
* @param ruleSet
* a set of condition-action rules
*/
public SimpleReflexAgentProgram(HashSet<Rule> ruleSet)
{
rules = ruleSet;
}
// START-AgentProgram
// function SIMPLE-RELEX-AGENT(percept) returns an action
public Action execute(Percept percept)
{
// state <- INTERPRET-INPUT(percept);
ObjectWithDynamicAttributes state = interpretInput(percept);
// rule <- RULE-MATCH(state, rules);
Rule rule = ruleMatch(state, rules);
// action <- rule.ACTION;
// return action
return ruleAction(rule);
}
// END-AgentProgram
// PROTECTED METHODS
protected ObjectWithDynamicAttributes interpretInput(Percept p)
{
return (DynamicPercept)p;
}
protected Rule ruleMatch(ObjectWithDynamicAttributes state,
HashSet<Rule> rulesSet)
{
foreach (Rule r in rulesSet)
{
if (r.evaluate(state))
{
return r;
}
}
return null;
}
protected Action ruleAction(Rule r)
{
return null == r ? NoOpAction.NO_OP : r.getAction();
}
}
}