Question
search.py: Please use Python. here is my code that needs to be fixed to get the correct output: from search import * class WolfGoatCabbage(Problem): def
search.py:
Please use Python. here is my code that needs to be fixed to get the correct output:
from search import *
class WolfGoatCabbage(Problem): def __init__(self): super().__init__(frozenset({'F', 'G', 'W', 'C'}), frozenset())
def actions(self, state): boat = {'F', 'G', 'W', 'C'} - state res = set() if 'F' in state: for x in boat: if x != 'F': res.add(frozenset({x})) for x in state: if x != 'F': res.add(frozenset({x, 'F'})) return res
def result(self, state, action): return state.symmetric_difference(action)
def goal_test(self, state): return len(state) == 4 and 'W' not in state.symmetric_difference({'F', 'G', 'W', 'C'})
def path_cost(self, c, state1, action, state2): return c + 1
if __name__ == '__main__': wgc = WolfGoatCabbage() solution = breadth_first_graph_search(wgc).path() for node in solution: state = node.state print([{x for x in state if x != y and x != 'F'} | {y, 'F'} for y in state if y != 'F']) solution = depth_first_graph_search(wgc).path() for node in solution: state = node.state print([{x for x in state if x != y and x != 'F'} | {y, 'F'} for y in state if y != 'F'])
Please fix my code to get the correct solution
Write a Python class, WolfGoatCabbage, that describes the Wolf, goat and cabbage problem (same problem from HW \#2) and can then be used to solve it by calling a search algorithm. - The class must extend class Problem in the search.py code. - Represent the state by a set of characters, representing the objects on the left bank. Use the characters: ' F ', ' G ', 'W', 'C'. Note that it is sufficient to represent the objects on one bank since the remaining will be on the other bank. E.g., {F,G} represents Farmer and Goat on the left bank and Wolf and Cabbage on the right. - An action in this puzzle is 1-2 objects crossing in the boat. Represent an action as a set of characters representing the objects crossing. E.g., {F ', 'G'\} represents the farmer and goat crossing. Note that it is not necessary to represent the direction of the boat as this will be clear from the state (e.g., if the farmer is on the left, then the boat will have to cross to the right). The class should be usable in the main function below to print the sequence of actions to reach the goal state. from search import * \# YOUR CODE GOES HERE if ___ name_ == '_main_': wgc = WolfGoatCabbage( ) solution = depth_first_graph_search(wgc).solution() print(solution) solution = breadth_first_graph_search(wgc).solution() print(solution) Your code should print something like: [\{'G', 'F'\}, {F},{F,F},{G,F},{W,F},{F},{G,F}] [{G,F},{F},{W,F},{G,F},{C,F},{F},{G,F}] - The class will be similar to class EightPuzzle in search.py. However, this is a different type of puzzle, so the code logic will be very different, just the structure remains the same. Specifically, your class should have a 1. a constructor that sets the initial and goal states 2. a method goal_test (state) that returns True if the given state is a goal state 3. a method result(state, action) that returns the new state reached from the given state and the given action 1 . Assume that the action is valid. 4. a method actions (state) that returns a list of valid actions in the given state - It is recommended to implement the methods in the order above and test each method individually - Do not change anything in search.pyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started