Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 581beb8

Browse files
committed
Stopped subclassing Agent just for the sake of defining an agent program.
1 parent da67590 commit 581beb8

File tree

4 files changed

+69
-85
lines changed

4 files changed

+69
-85
lines changed

agents.py

Lines changed: 48 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -99,53 +99,44 @@ def new_program(percept):
9999

100100
#______________________________________________________________________________
101101

102-
class TableDrivenAgent(Agent):
102+
def TableDrivenAgentProgram(table):
103103
"""This agent selects an action based on the percept sequence.
104104
It is practical only for tiny domains.
105-
To customize it you provide a table to the constructor. [Fig. 2.7]"""
106-
107-
def __init__(self, table):
108-
"Supply as table a dictionary of all {percept_sequence:action} pairs."
109-
percepts = []
110-
def program(percept):
111-
percepts.append(percept)
112-
action = table.get(tuple(percepts))
113-
return action
114-
Agent.__init__(self, program)
115-
105+
To customize it, provide as table a dictionary of all
106+
{percept_sequence:action} pairs. [Fig. 2.7]"""
107+
percepts = []
108+
def program(percept):
109+
percepts.append(percept)
110+
action = table.get(tuple(percepts))
111+
return action
112+
return program
116113

117-
class RandomAgent(Agent):
114+
def RandomAgentProgram(actions):
118115
"An agent that chooses an action at random, ignoring all percepts."
119-
120-
def __init__(self, actions):
121-
def program(percept):
122-
return random.choice(actions)
123-
Agent.__init__(self, program)
116+
def program(percept):
117+
return random.choice(actions)
118+
return program
124119

125120
#______________________________________________________________________________
126121

127-
class SimpleReflexAgent(Agent):
122+
def SimpleReflexAgentProgram(rules, interpret_input):
128123
"This agent takes action based solely on the percept. [Fig. 2.10]"
124+
def program(percept):
125+
state = interpret_input(percept)
126+
rule = rule_match(state, rules)
127+
action = rule.action
128+
return action
129+
return program
129130

130-
def __init__(self, rules, interpret_input):
131-
def program(percept):
132-
state = interpret_input(percept)
133-
rule = rule_match(state, rules)
134-
action = rule.action
135-
return action
136-
Agent.__init__(self, program)
137-
138-
class ModelBasedReflexAgent(Agent):
131+
def ModelBasedReflexAgentProgram(rules, update_state):
139132
"This agent takes action based on the percept and state. [Fig. 2.12]"
140-
141-
def __init__(self, rules, update_state):
142-
def program(percept):
143-
program.state = update_state(program.state, program.action, percept)
144-
rule = rule_match(program.state, rules)
145-
action = rule.action
146-
return action
147-
program.state = program.action = None
148-
Agent.__init__(self, program)
133+
def program(percept):
134+
program.state = update_state(program.state, program.action, percept)
135+
rule = rule_match(program.state, rules)
136+
action = rule.action
137+
return action
138+
program.state = program.action = None
139+
return program
149140

150141
def rule_match(state, rules):
151142
"Find the first rule that matches state."
@@ -157,20 +148,10 @@ def rule_match(state, rules):
157148

158149
loc_A, loc_B = (0, 0), (1, 0) # The two locations for the Vacuum world
159150

160-
class ReflexVacuumAgent(Agent):
161-
"A reflex agent for the two-state vacuum environment. [Fig. 2.8]"
162-
163-
def __init__(self):
164-
def program((location, status)):
165-
if status == 'Dirty': return 'Suck'
166-
elif location == loc_A: return 'Right'
167-
elif location == loc_B: return 'Left'
168-
Agent.__init__(self, program)
169-
170151

171152
def RandomVacuumAgent():
172153
"Randomly choose one of the actions from the vacuum environment."
173-
return RandomAgent(['Right', 'Left', 'Suck', 'NoOp'])
154+
return Agent(RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp']))
174155

175156

176157
def TableDrivenVacuumAgent():
@@ -186,22 +167,28 @@ def TableDrivenVacuumAgent():
186167
((loc_A, 'Clean'), (loc_A, 'Clean'), (loc_A, 'Dirty')): 'Suck',
187168
# ...
188169
}
189-
return TableDrivenAgent(table)
170+
return Agent(TableDrivenAgentProgram(table))
190171

191172

192-
class ModelBasedVacuumAgent(Agent):
193-
"An agent that keeps track of what locations are clean or dirty."
173+
def ReflexVacuumAgent():
174+
"A reflex agent for the two-state vacuum environment. [Fig. 2.8]"
175+
def program((location, status)):
176+
if status == 'Dirty': return 'Suck'
177+
elif location == loc_A: return 'Right'
178+
elif location == loc_B: return 'Left'
179+
return Agent(program)
194180

195-
def __init__(self):
196-
model = {loc_A: None, loc_B: None}
197-
def program((location, status)):
198-
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
199-
model[location] = status ## Update the model here
200-
if model[loc_A] == model[loc_B] == 'Clean': return 'NoOp'
201-
elif status == 'Dirty': return 'Suck'
202-
elif location == loc_A: return 'Right'
203-
elif location == loc_B: return 'Left'
204-
Agent.__init__(self, program)
181+
def ModelBasedVacuumAgent():
182+
"An agent that keeps track of what locations are clean or dirty."
183+
model = {loc_A: None, loc_B: None}
184+
def program((location, status)):
185+
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
186+
model[location] = status ## Update the model here
187+
if model[loc_A] == model[loc_B] == 'Clean': return 'NoOp'
188+
elif status == 'Dirty': return 'Suck'
189+
elif location == loc_A: return 'Right'
190+
elif location == loc_B: return 'Left'
191+
return Agent(program)
205192

206193
#______________________________________________________________________________
207194

logic.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,16 @@ def retract(self, sentence):
9393

9494
#______________________________________________________________________________
9595

96-
class KB_Agent(agents.Agent):
97-
"""A generic logical knowledge-based agent. [Fig. 7.1]"""
98-
def __init__(self, KB):
99-
steps = itertools.count()
100-
def program(percept):
101-
t = steps.next()
102-
KB.tell(self.make_percept_sentence(percept, t))
103-
action = KB.ask(self.make_action_query(t))[expr('action')]
104-
KB.tell(self.make_action_sentence(action, t))
105-
return action
106-
agents.Agent.__init__(self, program)
96+
def KB_AgentProgram(KB):
97+
"""A generic logical knowledge-based agent program. [Fig. 7.1]"""
98+
steps = itertools.count()
99+
100+
def program(percept):
101+
t = steps.next()
102+
KB.tell(make_percept_sentence(percept, t))
103+
action = KB.ask(make_action_query(t))
104+
KB.tell(make_action_sentence(action, t))
105+
return action
107106

108107
def make_percept_sentence(self, percept, t):
109108
return Expr("Percept")(percept, t)
@@ -112,7 +111,9 @@ def make_action_query(self, t):
112111
return expr("ShouldDo(action, %d)" % t)
113112

114113
def make_action_sentence(self, action, t):
115-
return Expr("Did")(action, t)
114+
return Expr("Did")(action[expr('action')], t)
115+
116+
return program
116117

117118
#______________________________________________________________________________
118119

probability.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33

44
from utils import *
55
from logic import extend
6-
import agents
76
from random import random, seed
87

98
#______________________________________________________________________________
109

11-
class DTAgent(agents.Agent):
10+
def DTAgentProgram(belief_state):
1211
"A decision-theoretic agent. [Fig. 13.1]"
13-
14-
def __init__(self, belief_state):
15-
def program(percept):
16-
belief_state.observe(program.action, percept)
17-
program.action = argmax(belief_state.actions(),
18-
belief_state.expected_outcome_utility)
19-
return program.action
20-
program.action = None
21-
agents.Agent.__init__(self, program)
12+
def program(percept):
13+
belief_state.observe(program.action, percept)
14+
program.action = argmax(belief_state.actions(),
15+
belief_state.expected_outcome_utility)
16+
return program.action
17+
program.action = None
18+
return program
2219

2320
#______________________________________________________________________________
2421

search.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
functions."""
66

77
from utils import *
8-
import agents
98
import math, random, sys, time, bisect, string
109

1110
#______________________________________________________________________________

0 commit comments

Comments
 (0)