The problem of the river crossing description: We have a farmer, a wolf, a goat and a cabbage and they need to cross the river with the following restrictions:
The wolf can’t be on the shame side with the goat
The goat can’t stay at the same side with the cabbage
-Initial state: (‘L’,‘L’,‘L’,‘L’)
-Finale state: (‘R’,‘R’,‘R’,‘R’)
The state list describes where everyone is, (farmer, wolf, goat, cabbage) so state(‘L’,‘R’,‘L’,‘R’) means that the wolf and the cabbage are on the right side and the farmer and the goat are on the left side of the river. I don’t want to make it more complex by implementing list of lists. The problem with my code is that it starts with the initial state and after the first step (farmer and goat on the right) it stops. I can’t find any way to make the recursion to work in order to have an acceptable result. So I could use some help in order to make the recursion functional and work as intended. Thank you in advance.""" ---------------------------------------------------------------------------- ******** Search Code for DFS and other search methods ******** (expanding front and extending queue) ******** author: """ import copy #left, right of the river spaces = { 'R': ['L'], 'L': ['R'] } def enter(state): if state[0]==state[1] and state[2]==state[3]: new_state=state=['R', state[1]] + ['R', state[3]] return new_state #function that swaps the side of the river that the farmer is def swap(state_l, i, j): state_l[i] = state_l[j] return state_l ''' operators for the movement of the farmer ''' def neighbour1(state): elem=['L'] i=state.index(elem) if elem in state else -1 #checking if the elem is in the state if i >=0: swap(state, i, spaces[i][0]) return state def neighbour2(state): elem=['L'] i=state.index(elem) if elem in state else -1 if i >=0: swap(state, i, spaces[i][1]) return state """ ---------------------------------------------------------------------------- **** FRONT managment **** """ def find_children(state): children=[] enter_state=copy.deepcopy(state) enter_child=enter(enter_state) tr1_state=copy.deepcopy(state) tr1_child=neighbour1(tr1_state) tr2_state=copy.deepcopy(state) tr2_child=neighbour2(tr2_state) if tr1_child is not None: children.append(tr1_child) if tr2_child is not None: children.append(tr2_child) if enter_child is not None: children.append(enter_child) return children """ ---------------------------------------------------------------------------- ** initialization of front ** """ def make_front(state): return [state] """ ---------------------------------------------------------------------------- **** expanding front **** """ def expand_front(front, method): if method=='DFS': if front: print("Front:") print(front) node=front.pop(0) for child in find_children(node): front.insert(0,child) elif method=='BFS': if front: print("Front:") print(front) node=front.pop(0) for child in find_children(node): front.append(child) return front ''' elif method=='BestFS': #''' #else: "other methods to be added" """ ---------------------------------------------------------------------------- **** QUEUE **** """ """ ---------------------------------------------------------------------------- ** initialization of queue ** """ def make_queue(state): return [[state]] """ ---------------------------------------------------------------------------- **** expanding queue **** επέκταση ουράς """ def extend_queue(queue, method): if method=='DFS': print("Queue:") print(queue) node=queue.pop(0) queue_copy=copy.deepcopy(queue) children=find_children(node[-1]) for child in children: path=copy.deepcopy(node) path.append(child) queue_copy.insert(0,path) elif method=='BFS': if queue: print("Queue:") print(queue) node=queue.pop(0) queue_copy=copy.deepcopy(queue) children=find_children(node[-1]) for child in children: path=copy.deepcopy(node) path.append(child) queue_copy.append(path) ''' elif method=='BestFS': #''' #else: "other methods to be added" return queue_copy """ ---------------------------------------------------------------------------- **** Basic recursive function to create search tree (recursive tree expansion) **** """ def find_solution(front, queue, closed, method): if not front: print('_NO_SOLUTION_FOUND_') elif front[0] in closed: new_front=copy.deepcopy(front) new_front.pop(0) new_queue=copy.deepcopy(queue) new_queue.pop(0) find_solution(new_front, new_queue, closed, method) elif is_goal_state(front[0]): print('_GOAL_FOUND_') print(queue[0]) else: closed.append(front[0]) front_copy=copy.deepcopy(front) front_children=expand_front(front_copy, method) queue_copy=copy.deepcopy(queue) queue_children=extend_queue(queue_copy, method) closed_copy=copy.deepcopy(closed) find_solution(front_children, queue_children, closed_copy, method) """" ---------------------------------------------------------------------------- ** Executing the code ** """ def is_goal_state(state): if state == ['R','R','R','R']: return True def main(): initial_state = ['L','L','L','L'] method='DFS' """ ---------------------------------------------------------------------------- **** starting search **** """ print('____BEGIN__SEARCHING____') find_solution(make_front(initial_state), make_queue(initial_state), [], method) if __name__ == "__main__": main()