-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbridge problem.py
118 lines (104 loc) · 3.88 KB
/
bridge problem.py
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
# -----------------
# User Instructions
#
# Modify the bridge_problem(here) function so that it
# tests for goal later: after pulling a state off the
# frontier, not when we are about to put it on the
# frontier.
def successors(state):
"""Return a dict of {state:action} pairs. A state is a (here, there, t) tuple,
where here and there are frozensets of people (indicated by their times) and/or
the light, and t is a number indicating the elapsed time."""
here, there = state
if 'light' in here:
return dict(((here - frozenset([a, b, 'light']),
there | frozenset([a, b, 'light'])),
(a, b, '->'))
for a in here if a is not 'light'
for b in here if b is not 'light')
else:
return dict(((here | frozenset([a, b, 'light']),
there - frozenset([a, b, 'light'])),
(a, b, '<-'))
for a in there if a is not 'light'
for b in there if b is not 'light')
def path_cost(path):
"""The total cost of a path (which is stored in a tuple
with the final action."""
# path = [state, (action, total_cost), state, ... ]
if len(path) < 3:
return 0
else:
action, total_cost = path[-2]
return total_cost
def action_cost(action):
"""Returns the cost (a number) of an action in the
bridge problem."""
# An action is an (a, b, arrow) tuple; a and b are
# times; arrow is a string.
a, b, arrow = action
return max(a, b)
def elapsed_time(path):
return path[-1][2]
def bridge_problem3(here):
'''
bridge_problem3 docstring
'''
start = (frozenset(here) | frozenset(['light']), frozenset())
return lowest_cost_search(start, successors, all_over, action_cost)
def all_over(state):
here, there = state
return not here or here == set('light')
def lowest_cost_search(start, successors, is_goal, action_cost):
explored = set()
frontier = [[start]]
while frontier:
path = frontier.pop(0)
state1 = final_state(path)
if is_goal(state1):
return path
explored.add(state1)
pcost = path_cost(path)
for (state, action) in successors(state1).items():
if state not in explored:
total_cost = pcost + action_cost(action)
path2 = path + [(action, total_cost), state]
add_to_frontier(frontier, path2)
return Fail
def bridge_problem2(here):
"""Modify this to test for goal later: after pulling a state off frontier,
not when we are about to put it on the frontier."""
## modify code below
here = frozenset(here) | frozenset(['light'])
explored = set() # set of states we have visited
# State will be a (people-here, people-there, time-elapsed)
frontier = [[(here, frozenset())]] # ordered list of paths we have blazed
while frontier:
path = frontier.pop(0)
here1, there1 = state1 = final_state(path)
if not here1 or (len(here1) == 1 and 'light' in here1):
return path
explored.add(state1)
pcost = path_cost(path)
for (state, action) in bsuccessors2(state1).items():
if state not in explored:
total_cost = pcost + bcost(action)
path2 = path + [(action, total_cost), state]
add_to_frontier(frontier, path2)
return Fail
def final_state(path): return path[-1]
def add_to_frontier(frontier, path):
old = None
for i,p in enumerate(frontier):
if final_state(p) == final_state(path):
old = i
break
if old is not None and path_cost(frontier[old]) < path_cost(path):
return
elif old is not None:
del frontier[old]
frontier.append(path)
frontier.sort(key=path_cost)
answer = bridge_problem3(frozenset((1, 2, 7, 11),))
for step in answer:
print(step)