Assignment 2 Dhruv Sahgal
Assignment 2 Dhruv Sahgal
Ans.)A farmer with his wolf, goat and a bag of cabbages arrives at the east side of a river
they wish to cross. There is a boat at the rivers edge. The farmer can row the boat. The boat
can only hold two things (including the rower) at any one time.
If the wolf is ever left alone with the goat, the wolf will eat it. Similarly if the goat is ever left
alone with the cabbages, the goat will eat it.
How can the farmer get across the river so that all four arrive safely on the other side?
i) Represent the problem as a state-space problem (define the states and the moves/operators).
ii) Show a solution path (the sequence of operators to reach the goal from a start state).
iii) Check if there is an alternative solution path. If so, display the path.
Solution
i) State-space representation:
The initial state is (0, 0, 0, 0) (all on the east bank), and the goal state is (1, 1, 1, 1) (all on the
west bank).
The valid moves/operators are:
However, you cannot perform moves that would result in the wolf eating the goat or the goat
eating the cabbage.
ii) Solution path:
##ii.)
def is_valid_state(state):
return False
initial_state = (0, 0, 0, 0)
moves = [(0, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (0, 1, 0, 1)]
visited_states = []
path = [initial_state]
if dfs(initial_state, visited_states, path):
for state in path:
print(state)
else:
print("No solution found.")
##iii.)
def
find_alternative_path(current_path)
: current_state = current_path[-1]
for move in moves:
new_state = tuple(current_state[i] + move[i] for i in range(4))
if all(0 <= new_state[i] <= 1 for i in range(4)) and
is_valid_state(new_state) and new_state not in current_path:
new_path = current_path + [new_state]
if dfs(new_state, [], new_path):
return new_path
return None
alternative_path = find_alternative_path(path)
if alternative_path:
print("\nAlternative Solution Path:")
for state in alternative_path:
print(state)
else:
BY –
SOMIK BANSAL
210C2030118