-
Notifications
You must be signed in to change notification settings - Fork 0
/
farmer_wolf_goat_cabbage_problem.py
179 lines (130 loc) · 4.83 KB
/
farmer_wolf_goat_cabbage_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
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
import copy
# Farmer, Goat, Wolf and Cabbage
#######################
## Data Structures ####
#######################
# The following is how the state is represented during a search.
# A dictionary format is chosen for the convenience and quick access
LEFT = 0
RIGHT = 1
BOAT_POSITION = 2
Wolf = 0
Goat = 1
Cabbage = 2
# [[num_Goat_left, num_Wolf_left, num_Cabbage_left], [num_Goat_right, num_Wolf_right. num_Cabbage_right], boat_position (0=left, 1=right)]
initial_state = [[1, 1, 1], [0, 0, 0], LEFT]
goal_state = [[0, 0, 0], [1, 1, 1], RIGHT]
###################################################
# Functions related to the game
###################################################
# Verifies if the state is safe.
def is_safe(state):
return ((state[BOAT_POSITION] == LEFT and state[LEFT][Goat] == 1) or
(state[BOAT_POSITION] == RIGHT and state[RIGHT][Goat] == 1) or
(state[BOAT_POSITION] == LEFT and (state[RIGHT][Wolf] != state[RIGHT][Goat] and state[RIGHT][Goat] != state[RIGHT][Cabbage])) or
(state[BOAT_POSITION] == RIGHT and (state[LEFT][Wolf] !=
state[LEFT][Goat] and state[LEFT][Goat] != state[LEFT][Cabbage]))
)
def opposite_side(side):
if side == LEFT:
return RIGHT
else:
return LEFT
##############################################
## Functions for recording the path #########
##############################################
#
# A path is the states from the root to a certain goal node
# It is represented as a List of states from the root
# The create_path function create a new path by add one more node to an old path
#
def create_path(old_path, state):
new_path = old_path.copy()
new_path.append(state)
return new_path
##########################
# Functions for Searching
##########################
def move(state, Wolves, Goats, Cabbages):
side = state[BOAT_POSITION]
opposite = opposite_side(side)
if state[side][Wolf] >= Wolves and state[side][Goat] >= Goats and state[side][Cabbage] >= Cabbages:
new_state = copy.deepcopy(state) # Copy an object inside an object
new_state[side][Wolf] -= Wolves
new_state[opposite][Wolf] += Wolves
new_state[side][Goat] -= Goats
new_state[opposite][Goat] += Goats
new_state[side][Cabbage] -= Cabbages
new_state[opposite][Cabbage] += Cabbages
new_state[BOAT_POSITION] = opposite
return new_state
# Find out the possible moves, and return them as a list
def find_children(old_state):
children = []
side = old_state[BOAT_POSITION]
# Try to move one Wolf
if old_state[side][Wolf] == 1:
new_state = move(old_state, Wolves=1, Goats=0, Cabbages=0)
if is_safe(new_state):
children.append(new_state)
# Try to move one Goat
if old_state[side][Goat] == 1:
new_state = move(old_state, Wolves=0, Goats=1, Cabbages=0)
if is_safe(new_state):
children.append(new_state)
# Try to move one Cabbage
if old_state[side][Cabbage] == 1:
new_state = move(old_state, Wolves=0, Goats=0, Cabbages=1)
if is_safe(new_state):
children.append(new_state)
# Travel with nothing
if old_state[side]:
new_state = move(old_state, Wolves=0, Goats=0, Cabbages=0)
if is_safe(new_state):
children.append(new_state)
return children
# ---------------------------
# Search routine ####
# ---------------------------
def bfs_search(start_state):
visited = []
to_visit = []
path = create_path([], start_state)
next_node = [start_state, path]
end = False
while not end:
next_state, path = next_node
if not next_state in visited:
visited.append(next_state)
if next_state == goal_state:
return path
else:
for child_state in find_children(next_state):
child_path = create_path(path, child_state)
to_visit.append([child_state, child_path])
if to_visit:
next_node = to_visit.pop(0)
else:
print("Failed to find a goal state")
end = True
return ()
################
## Main ######
################
# Search for a solution
path = bfs_search(initial_state)
if path:
print("Path from start to goal:")
for p in path: # path = bfs_search(initial_state)
left, right, side = p
w_left, g_left, c_left = left
w_right, g_right, c_right = right
if side == LEFT:
boat_l = "#BOAT#"
boat_r = " "
else:
boat_l = " "
boat_r = "#BOAT#"
print(" Wolf {} Goat {} Cabbage {} {} |~~~~~| {} Wolf {} Goat {} Cabbage {}" .format(
w_left, g_left, c_left, boat_l, boat_r, w_right, g_right, c_right))
input()