-
Notifications
You must be signed in to change notification settings - Fork 1
/
agents.rb
365 lines (291 loc) · 9.86 KB
/
agents.rb
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Implement Agents and Environments (Chapters 1-2).
#
# The class hierarchies are as follows:
#
# Object ## A physical object that can exist in an environment
# Agent
# Wumpus
# RandomAgent
# ReflexVacuumAgent
# ...
# Dirt
# Wall
# ...
#
# Environment ## An environment holds objects, runs simulations
# XYEnvironment
# VacuumEnvironment
# WumpusEnvironment
#
# EnvFrame ## A graphical representation of the Environment
=begin
## python stuff ##
from utils import *
import random, copy
=end
#########################################################################
class E_Object
# This represents any physical object that can appear in an Environment.
# You subclass Object to get the objects you want. Each object can have a
# .__name__ slot (used for output only).
def to_s
"<#{@name}:#{self.class}>" if self.respond_to? :name
end
def is_alive?
# Objects that are 'alive' should return true.
@alive if self.respond_to? :alive
end
def display(canvas, x, y, width, height)
# Display an image of this Object on the canvas.
end
end
class Agent < E_Object
# An Agent is a subclass of Object with one required slot,
# .program, which should hold a function that takes one argument, the
# percept, and returns an action. (What counts as a percept or action
# will depend on the specific environment in which the agent exists.)
# Note that 'program' is a slot, not a method. If it were a method,
# then the program could 'cheat' and look at aspects of the agent.
# It's not supposed to do that: the program can only look at the
# percepts. An agent program that needs a model of the world (and of
# the agent itself) will have to build and maintain its own model.
# There is an optional slots, .performance, which is a number giving
# the performance measure of the agent in its environment.
=begin
attr_accessor :program
=end
def initialize
def program(percept)
print "Percept=#{percept}; action? "
gets.strip!
end
=begin
@program = method :program
=end ## <<<<--- I dont use it
@alive = true
end
end
=begin
def traceAgent(agent)
# Wrap the agent's program to print its input and output. This will let
# you see what the agent is doing in the environment.
$agent = agent
$old_program = agent.method :program
def new_program(percept)
action = $old_program.call(percept)
puts "#{$agent} perceives #{percept} and does #{action}"
return action
end
agent.program = method :new_program
return agent
end
=end ## <<<<----- Changend for something more ruby
def traceAgent(agent)
# Wrap the agent's program to print its input and output. This will let
# you see what the agent is doing in the environment.
agent.instance_eval do
def program(percept)
action = super
puts "#{self} perceives #{percept} and does #{action}"
return action
end
end
return agent
end
#########################################################################
=begin
class TableDrivenAgent < Agent
# This agent selects an action based on the percept sequence.
# It is practical only for tiny domains.
# To customize it you provide a table to the constructor. [Fig. 2.7]
def initialize(table)
# Supply as table a dictionary of all {percept_sequence:action} pairs.
# ## The agent program could in principle be a function, but because
# ## it needs to store state, we make it a callable instance of a class.
super
$table = table
$percepts = []
def program(percept)
$percepts << percept
action = $table[$percepts]
return action
end
@program = method :program
end
end
=end ## <<<<----- Changend for something more ruby
class TableDrivenAgent < Agent
# This agent selects an action based on the percept sequence.
# It is practical only for tiny domains.
# To customize it you provide a table to the constructor. [Fig. 2.7]
def initialize(table)
# Supply as table a dictionary of all {percept_sequence:action} pairs.
# ## The agent program could in principle be a function, but because
# ## it needs to store state, we make it a callable instance of a class.
@table = table
@percepts = []
def program(percept)
@percepts << percept
action = @table[@percepts]
return action
end
=begin
@program = method :program
=end ## <<<<---- I don't use it
@alive = true
end
end
=begin
class RandomAgent < Agent
# An agent that chooses an action at random, ignoring all percepts.
def initialize(actions)
super
@actions = actions
@program = lambda {|percept| @actions[rand(@actions.length)]}
end
end
=end ## <<<--- Doesnt work
class RandomAgent < Agent
# An agent that chooses an action at random, ignoring all percepts.
def initialize(actions)
@actions = actions
def program(percept)
@actions[rand(@actions.length)]
end
@alive = true
end
end
#########################################################################
# loc_A, loc_B = [0, 0], [1, 0] # The two locations for the Vacuum world
class ReflexVacuumAgent < Agent
# A reflex agent for the two-state vacuum environment. [Fig. 2.8]
def initialize
super
def program(hash={})
loc_A, loc_B = [0, 0], [1, 0] # The two locations for the Vacuum world
if hash[:status] == 'Dirty'
'Suck'
elsif hash[:location] == loc_A
'Right'
elsif hash[:location] == loc_B
'Left'
end
end
=begin
@program = method :program
=end ## <<--- I dont use it!
end
end
def randomVacuumAgent
# Randomly choose one of the actions from the vaccum environment.
RandomAgent.new ['Right', 'Left', 'Suck', 'NoOp']
end
def tableDrivenVacuumAgent
# [Fig. 2.3]
loc_A, loc_B = [0, 0], [1, 0] # The two locations for the Vacuum world
table = {
[[loc_A, 'Clean'],] => 'Right',
[[loc_A, 'Dirty'],] => 'Suck',
[[loc_B, 'Clean'],] => 'Left',
[[loc_B, 'Dirty'],] => 'Suck',
[[loc_A, 'Clean'], [loc_A, 'Clean']] => 'Right',
[[loc_A, 'Clean'], [loc_A, 'Dirty']] => 'Suck',
# ...
[[loc_A, 'Clean'], [loc_A, 'Clean'], [loc_A, 'Clean']] => 'Right',
[[loc_A, 'Clean'], [loc_A, 'Clean'], [loc_A, 'Dirty']] => 'Suck',
# ...
}
return TableDrivenAgent.new table
end
class ModelBasedVacuumAgent < Agent
# An agent that keeps track of what locations are clean or dirty.
def initialize
super
@loc_A, @loc_B = [0, 0], [1, 0] # The two locations for the Vacuum world
@model = {@loc_A => nil, @loc_B => nil}
def program(hash={})
# Same as ReflexVacuumAgent, except if everything is clean, do NoOp
@model[hash[:location]] = hash[:status] ## Update the model here
if @model[@loc_A] == @model[@loc_B] && @model[@loc_A] == 'Clean'
'NoOp'
elsif hash[:status] == 'Dirty'
'Suck'
elsif hash[:location] == @loc_A
'Right'
elsif hash[:location] == @loc_B
'Left'
end
end
end
end
#########################################################################
class Enviroment
# Abstract class representing an Environment. 'Real' Environment classes
# inherit from this. Your Environment will typically need to implement:
# percept: Define the percept that an agent sees.
# execute_action: Define the effects of executing an action.
# Also update the agent.performance slot.
# The environment keeps a list of .objects and .agents (which is a subset
# of .objects). Each agent has a .performance slot, initialized to 0.
# Each object has a .location slot, even though some environments may not
# need this.
@@object_classes = [] ## List of classes that can go into environment
def initialize
@objects = []
@agents = []
end
def percept(agent)
# Return the percept that the agent sees at this point. Override this.
end
def execute_action(agent, action)
# Change the world to reflect this action. Override this.
end
def default_location(object)
# Default location to place a new object with unspecified location.
end
def exogenous_change
# If there is spontaneous change in the world, override this.
end
def is_done
# By default, we're done when we can't find a live agent.
@agents.each do |agent|
if agent.is_alive
return false
end
end
return true
end
def step
# Run the environment for one time step. If the
# actions and exogenous changes are independent, this method will
# do. If there are interactions between them, you'll need to
# override this method.
unless is_done
actions = @agents.map{|agent| agent.program(percept(agent))}
@agents.zip(actions).each do |agent, action|
execute_action agent, action
end
exogenous_change
end
end
def run(steps=1000)
# Run the Enviroment for given number of time steps.
(0..steps).times do |stp|
if is_done
return
end
self.step
end
end
def add_object(object, location=nil)
# Add an object to the environment, setting its location. Also keep
# track of objects that are agents. Shouldn't need to override this.
object.location = location or default_location(object)
@objects << object
if object.instance_of? Agent
object.performance = 0
@agents << object
end
return self
end
end