Skip to content

Commit 633c15c

Browse files
committed
KBAgent added to propositional logic
1 parent 9a4de81 commit 633c15c

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

aima-csharp/aima-csharp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@
190190
<Compile Include="logic\fol\SubsumptionElimination.cs" />
191191
<Compile Include="logic\fol\Unifier.cs" />
192192
<Compile Include="logic\fol\VariableCollector.cs" />
193+
<Compile Include="logic\propositional\agent\KBAgent.cs" />
194+
<Compile Include="logic\propositional\kb\data\Clause.cs" />
195+
<Compile Include="logic\propositional\kb\data\ConjunctionOfClauses.cs" />
196+
<Compile Include="logic\propositional\kb\data\Literal.cs" />
197+
<Compile Include="logic\propositional\kb\data\Model.cs" />
198+
<Compile Include="logic\propositional\kb\KnowledgeBase.cs" />
193199
<Compile Include="obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs" />
194200
<Compile Include="obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs" />
195201
<Compile Include="obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs" />
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using aima.core.agent;
2+
using aima.core.agent.impl;
3+
using aima.core.logic.propositional.kb;
4+
using aima.core.logic.propositional.parsing.ast;
5+
6+
namespace aima.core.logic.propositional.agent
7+
{
8+
/**
9+
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 7.1, page
10+
* 236.<br>
11+
* <br>
12+
*
13+
* <pre>
14+
* function KB-AGENT(percept) returns an action
15+
* persistent: KB, a knowledge base
16+
* t, a counter, initially 0, indicating time
17+
*
18+
* TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
19+
* action &lt;- ASK(KB, MAKE-ACTION-QUERY(t))
20+
* TELL(KB, MAKE-ACTION-SENTENCE(action, t))
21+
* t &lt;- t + 1
22+
* return action
23+
*
24+
* </pre>
25+
*
26+
* Figure 7.1 A generic knowledge-based agent. Given a percept, the agent adds
27+
* the percept to its knowledge base, asks the knowledge base for the best
28+
* action, and tells the knowledge base that it has in fact taken that action.
29+
*
30+
* @author Ciaran O'Reilly
31+
*/
32+
public abstract class KBAgent : AbstractAgent
33+
{
34+
// persistent: KB, a knowledge base
35+
protected KnowledgeBase KB;
36+
// t, a counter, initially 0, indicating time
37+
private int t = 0;
38+
39+
public KBAgent(KnowledgeBase KB)
40+
{
41+
this.KB = KB;
42+
}
43+
44+
// function KB-AGENT(percept) returns an action
45+
public override Action execute(Percept percept)
46+
{
47+
// TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
48+
KB.tell(makePerceptSentence(percept, t));
49+
// action &lt;- ASK(KB, MAKE-ACTION-QUERY(t))
50+
Action action = ask(KB, makeActionQuery(t));
51+
52+
// TELL(KB, MAKE-ACTION-SENTENCE(action, t))
53+
KB.tell(makeActionSentence(action, t));
54+
// t &lt;- t + 1
55+
t = t + 1;
56+
// return action
57+
return action;
58+
}
59+
60+
/**
61+
* MAKE-PERCEPT-SENTENCE constructs a sentence asserting that the agent
62+
* perceived the given percent at the given time.
63+
*
64+
* @param percept
65+
* the given percept
66+
* @param t
67+
* the given time
68+
* @return a sentence asserting that the agent perceived the given percept
69+
* at the given time.
70+
*/
71+
// MAKE-PERCEPT-SENTENCE(percept, t)
72+
public abstract Sentence makePerceptSentence(Percept percept, int t);
73+
74+
/**
75+
* MAKE-ACTION-QUERY constructs a sentence that asks what action should be
76+
* done at the current time.
77+
*
78+
* @param t
79+
* the current time.
80+
* @return a sentence that asks what action should be done at the current
81+
* time.
82+
*/
83+
// MAKE-ACTION-QUERY(t)
84+
public abstract Sentence makeActionQuery(int t);
85+
86+
/**
87+
* MAKE-ACTION-SENTENCE constructs a sentence asserting that the chosen action was executed.
88+
* @param action
89+
* the chose action.
90+
* @param t
91+
* the time at which the action was executed.
92+
* @return a sentence asserting that the chosen action was executed.
93+
*/
94+
// MAKE-ACTION-SENTENCE(action, t)
95+
public abstract Sentence makeActionSentence(Action action, int t);
96+
97+
/**
98+
* A wrapper around the KB's ask() method which translates the action (in the form of
99+
* a sentence) determined by the KB into an allowed 'Action' object from the current
100+
* environment in which the KB-AGENT resides.
101+
*
102+
* @param KB
103+
* the KB to ask.
104+
* @param actionQuery
105+
* an action query.
106+
* @return the Action to be performed in response to the given query.
107+
*/
108+
// ASK(KB, MAKE-ACTION-QUERY(t))
109+
public abstract Action ask(KnowledgeBase KB, Sentence actionQuery);
110+
}
111+
}

0 commit comments

Comments
 (0)