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

Skip to content

Commit ddac0dc

Browse files
AdityaDaflapurkarnorvig
authored andcommitted
Update PeakFindingProblem code to allow diagonal motion (#684)
* Update PeakFindingProblem code to allow diagonal motion * Fix unit test issues * update PeakFindingProblem to take actions as input param * Refactor code in search.py
1 parent 47e6089 commit ddac0dc

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

search.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from utils import (
88
is_in, argmin, argmax, argmax_random_tie, probability, weighted_sampler,
99
memoize, print_table, open_data, Stack, FIFOQueue, PriorityQueue, name,
10-
distance
10+
distance, vector_add
1111
)
1212

1313
from collections import defaultdict
@@ -526,39 +526,37 @@ def and_search(states, problem, path):
526526
# body of and or search
527527
return or_search(problem.initial, problem, [])
528528

529+
# Pre-defined actions for PeakFindingProblem
530+
directions4 = { 'W':(-1, 0), 'N':(0, 1), 'E':(1, 0), 'S':(0, -1) }
531+
directions8 = dict(directions4)
532+
directions8.update({'NW':(-1, 1), 'NE':(1, 1), 'SE':(1, -1), 'SW':(-1, -1) })
529533

530534
class PeakFindingProblem(Problem):
531535
"""Problem of finding the highest peak in a limited grid"""
532536

533-
def __init__(self, initial, grid):
537+
def __init__(self, initial, grid, defined_actions=directions4):
534538
"""The grid is a 2 dimensional array/list whose state is specified by tuple of indices"""
535539
Problem.__init__(self, initial)
536540
self.grid = grid
541+
self.defined_actions = defined_actions
537542
self.n = len(grid)
538543
assert self.n > 0
539544
self.m = len(grid[0])
540545
assert self.m > 0
541546

542547
def actions(self, state):
543-
"""Allows movement in only 4 directions"""
544-
# TODO: Add flag to allow diagonal motion
548+
"""Returns the list of actions which are allowed to be taken from the given state"""
545549
allowed_actions = []
546-
if state[0] > 0:
547-
allowed_actions.append('N')
548-
if state[0] < self.n - 1:
549-
allowed_actions.append('S')
550-
if state[1] > 0:
551-
allowed_actions.append('W')
552-
if state[1] < self.m - 1:
553-
allowed_actions.append('E')
550+
for action in self.defined_actions:
551+
next_state = vector_add(state, self.defined_actions[action])
552+
if next_state[0] >= 0 and next_state[1] >= 0 and next_state[0] <= self.n - 1 and next_state[1] <= self.m - 1:
553+
allowed_actions.append(action)
554+
554555
return allowed_actions
555556

556557
def result(self, state, action):
557558
"""Moves in the direction specified by action"""
558-
x, y = state
559-
x = x + (1 if action == 'S' else (-1 if action == 'N' else 0))
560-
y = y + (1 if action == 'E' else (-1 if action == 'W' else 0))
561-
return (x, y)
559+
return vector_add(state, self.defined_actions[action])
562560

563561
def value(self, state):
564562
"""Value of a state is the value it is the index to"""
@@ -1347,3 +1345,4 @@ def compare_graph_searchers():
13471345
GraphProblem('Q', 'WA', australia_map)],
13481346
header=['Searcher', 'romania_map(Arad, Bucharest)',
13491347
'romania_map(Oradea, Neamt)', 'australia_map'])
1348+

tests/test_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ def test_hill_climbing():
8888
def test_simulated_annealing():
8989
random.seed("aima-python")
9090
prob = PeakFindingProblem((0, 0), [[0, 5, 10, 20],
91-
[-3, 7, 11, 5]])
91+
[-3, 7, 11, 5]], directions4)
9292
sols = {prob.value(simulated_annealing(prob)) for i in range(100)}
9393
assert max(sols) == 20
9494
prob = PeakFindingProblem((0, 0), [[0, 5, 10, 8],
9595
[-3, 7, 9, 999],
96-
[1, 2, 5, 11]])
96+
[1, 2, 5, 11]], directions8)
9797
sols = {prob.value(simulated_annealing(prob)) for i in range(100)}
9898
assert max(sols) == 999
9999

0 commit comments

Comments
 (0)