Skip to content

Commit 89c116c

Browse files
Nicole NageliNicole Nageli
authored andcommitted
implemented hill climbing agent
1 parent 016a031 commit 89c116c

2 files changed

Lines changed: 285 additions & 41 deletions

File tree

‎aima/agents.py‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import copy
4040
import collections
4141
import numbers
42-
42+
import math
4343

4444
# ______________________________________________________________________________
4545

@@ -285,6 +285,37 @@ def program(percept):
285285
return Agent(program)
286286

287287

288+
def HillClimbingVacuumAgent():
289+
path = []
290+
291+
def program(percept):
292+
location, current_value, neighbors = percept
293+
# record current location to path
294+
path.append(location)
295+
296+
if not neighbors:
297+
program.done = True
298+
return 'NoOp'
299+
300+
# choose highest dirt level neighbor
301+
best_direction = max(neighbors, key = lambda d: neighbors[d][1])
302+
best_value = neighbors[best_direction][1]
303+
304+
# move if neighbor better
305+
if best_value > current_value:
306+
return best_direction
307+
308+
# clean current location after comparing?
309+
# if current_value > 0: return 'Suck'
310+
311+
# done when local maximum
312+
program.done = True
313+
return 'NoOp'
314+
315+
program.path = path
316+
program.done = False
317+
return Agent(program)
318+
288319
# ______________________________________________________________________________
289320

290321

@@ -392,6 +423,52 @@ def delete_thing(self, thing):
392423
if thing in self.agents:
393424
self.agents.remove(thing)
394425

426+
# 4x4 grid environment matrix (top left(0,0) to bottom right(3,3))
427+
class DirtGridEnv(Environment):
428+
moves = {'Up': (0, -1), 'Down': (0, 1), 'Left': (-1, 0), 'Right': (1, 0)}
429+
430+
def __init__(self, grid, start=(3,0)):
431+
super().__init__()
432+
self.grid = grid
433+
self.width = len(grid[0])
434+
self.height = len(grid)
435+
self.start = start
436+
437+
def default_location(self, thing):
438+
return self.start
439+
440+
def in_bounds(self, location):
441+
x, y = location
442+
return 0 <= x < self.width and 0 <= y < self.height
443+
444+
def percept(self, agent):
445+
x, y = agent.location
446+
neighbors = {}
447+
448+
for direction, (dx, dy) in self.moves.items():
449+
neighbor = (x + dx, y + dy)
450+
451+
if self.in_bounds(neighbor):
452+
nx, ny = neighbor
453+
neighbors[direction] = (neighbor, self.grid[ny][nx])
454+
455+
return (agent.location, self.grid[y][x], neighbors)
456+
457+
def execute_action(self, agent, action):
458+
if action in self.moves:
459+
dx, dy = self.moves[action]
460+
x, y = agent.location
461+
new_location = (x + dx, y + dy)
462+
463+
if self.in_bounds(new_location):
464+
agent.location = new_location
465+
"""
466+
elif action == 'Suck':
467+
x, y = agent.location
468+
self.grid[y][x] = 0
469+
"""
470+
def is_done(self):
471+
return any(agent.program.done for agent in self.agents)
395472

396473
class Direction:
397474
"""A direction class for agents that want to move in a 2D plane.

0 commit comments

Comments
 (0)