0% found this document useful (0 votes)
49 views4 pages

Assignment 2 Dhruv Sahgal

The document describes the Farmer-Wolf-Goat-Cabbage (FWGC) problem and provides a solution. It represents the problem as a state-space with states defined by the locations of the farmer, wolf, goat and cabbage. The initial state has all on the east bank and the goal is to get all to the west bank. It then shows one solution path consisting of 7 steps of valid moves. It also provides code to check for alternative paths using depth-first search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Assignment 2 Dhruv Sahgal

The document describes the Farmer-Wolf-Goat-Cabbage (FWGC) problem and provides a solution. It represents the problem as a state-space with states defined by the locations of the farmer, wolf, goat and cabbage. The initial state has all on the east bank and the goal is to get all to the west bank. It then shows one solution path consisting of 7 steps of valid moves. It also provides code to check for alternative paths using depth-first search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ARTIFICIAL ASSIGNMENT

Q.) Solve the Farmer-Wolf-Goat-Cabbage(FWGC) problem.

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:

Let's represent the states as a tuple (F, W, G, C) where:

 F: Farmer's location (0 for east bank, 1 for west bank)

 W: Wolf's location (0 for east bank, 1 for west bank)

 G: Goat's location (0 for east bank, 1 for west bank)

 C: Cabbage's location (0 for east bank, 1 for west bank)

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:

1. Farmer crosses alone.

2. Farmer crosses with the wolf.

3. Farmer crosses with the goat.

4. Farmer crosses with the cabbage.

However, you cannot perform moves that would result in the wolf eating the goat or the goat
eating the cabbage.
ii) Solution path:

Here's the solution path:

1. Farmer and goat cross (0, 0, 0, 0) -> (1, 0, 1, 0)

2. Farmer returns alone (1, 0, 1, 0) -> (0, 0, 1, 0)

3. Farmer and cabbage cross (0, 0, 1, 0) -> (1, 0, 1, 1)

4. Farmer and goat cross (1, 0, 1, 1) -> (0, 0, 0, 1)

5. Farmer and wolf cross (0, 0, 0, 1) -> (1, 1, 0, 1)

6. Farmer returns alone (1, 1, 0, 1) -> (0, 1, 0, 1)

7. Farmer and goat cross (0, 1, 0, 1) -> (1, 1, 1, 1)

##ii.)
def is_valid_state(state):

if (state[1] == state[2] and state[0] != state[1]) or (state[2] ==


state[3] and state[0] != state[2]):
return False
return True
def dfs(current_state, visited_states, path):
visited_states.append(current_state)
if current_state == (1, 1, 1, 1):
return True
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 visited_states:
path.append(new_state)
if dfs(new_state, visited_states, path):
return True
path.pop()

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

You might also like