Part III: A Searcher class for random search In the next part of…

Question Answered step-by-step Part III: A Searcher class for random search In the next part of… Part III: A Searcher class for random search In the next part of the project, you will begin to implement the actual state-space search process. As discussed in class, we will use searcher objects to perform the search for us. Different types of searcher objects will implement different state-space search algorithms, and we’ll take advantage of inheritance when defining the searcher classes.Find the file in your project folder and open it in Spyder. It contains some starter code, including the beginnings of a class called Searcher, which will perform a random state-space search. Read over the comments accompanying the starter code. Write a constructor __init__(self, depth_limit) that constructs a new Searcher object by initializing the following attributes:an attribute states for the Searcher’s list of untested states; it should be initialized to an empty attribute num_tested that will keep track of how many states the Searcher tests; it should be initialized to 0an attribute depth_limit that specifies how deep in the state-space search tree the Searcher will go; it should be initialized to the value specified by the parameter depth_limit.(A depth_limit of -1 indicates that the Searcher does not use a depth limit.) Write a method should_add(self, state) that takes a State object called state and returns True if the called Searcher should add state to its list of untested states, and False otherwise.The method should return False if either of these conditions holds:the Searcher has a depth limit (i.e., its depth limit is not -1) and state is beyond the depth limit (i.e., the number of moves used to get to state is greater than the depth limit)state creates a cycle in the search, because the same board already appears in the sequence of moves that led to state. We’ve given you a method in the State class called creates_cycle() that checks for this. Read the comments accompanying that method to understand how it works, and apply it appropriately here. Write a method add_state(self, new_state) that adds takes a single State object called new_state and adds it to the Searcher’s list of untested states. This method should only require one line of code! It should not return a value.For the sake of efficiency, we recommend that you do not do something like the following:self.states = self.states + …     # don’t do this!Rather, we recommend that you either use the += operator or the append method in the list object. We will discuss the reasons for this in class.Examples:>>> b = Board(‘142358607’)>>> s = State(b, None, ‘init’)>>> searcher = Searcher(-1)>>> searcher.add_state(s)>>> searcher.states[142358607-init-0]>>> succ = s.generate_successors()>>> succ[142308657-up-1, 142358067-left-1, 142358670-right-1]>>> searcher.add_state(succ[0])  # add just the first successor>>> searcher.states[142358607-init-0, 142308657-up-1] Write a method add_states(self, new_states) that takes a list State objects called new_states, and that processes the elements of new_states one at a time as follows:If a given state s should be added to the Searcher’s list of untested states (because s would not cause a cycle and is not beyond the Searcher’s depth limit), the method should use the Searcher’s add_state() method to add s to the list of states.If a given state s should not be added to the Searcher object’s list of states, the method should ignore the state.Take advantage of the Searcher’s method for determining if a state should be added.Make sure that you use add_state() when adding the individual states to the list, rather than adding them yourself. This will will allow you to make fewer changes when you use inheritance to define other types of searchers.Copy the following method into your Searcher class:def next_state(self):    “”” chooses the next state to be tested from the list of        untested states, removing it from the list and returning it    “””    s = random.choice(self.states)    self.states.remove(s)    return sMake sure to maintain the appropriate indentation when you do so.This method will be used to obtain the next state to be tested, and you should review it carefully. Here are two points worth noting:Because Searcher objects perform a random search through the search space, we are using the random.choice method to randomly choose one of the elements of the states list.We’re using a list method called remove to remove the selected state s from the states list. Finally, write a method find_solution(self, init_state) that performs a full random state-space search, stopping when the goal state is found or when the Searcher runs out of untested states.To begin, the method should add the parameter init_state to the list of untested states;If the searcher finds a goal state, it should return it.If the searcher runs out of untested states before finding a goal state, it should return the special keyword None. (Note that there should not be any quotes around None, because it is not a string.)The method should increment the Searcher object’s num_tested attribute every time that it tests a state to see if it is the goal.In order to see the full solution (i.e., the sequence of moves from the initial state to the goal state), we need to add a method to the State class that will follow predecessor references back up the state-space search tree in order to find and print the sequence of moves.Open up your file, and add a method print_moves_to(self) that prints the sequence of moves that lead from the initial state to the called State object (i.e., to self).To accomplish this task, you should first review the attributes that each State object has inside it. Consult the guidelines for the State class __init__ method as needed.Next, it’s worth noting that this method will be starting at a given State object and following predecessor references back to the initial state. However, we want to print the sequence of moves in the reverse order – from the initial state to the called State object. One way to do this is using recursion, as shown in the following pseudocode:def print_moves_to(self):    if self is the initial state:    # base case        print(‘initial state:’)        print the board associated with self    else:        make a recursive call to print the moves to the predecessor state        print the move that led to self (see format below)        print the board associated with selfBecause the recursive call on the predecessor state comes before the processing of self, the method will print the sequence of moves in the correct order.Example (results may vary because of randomness):>>> b = Board(‘142305678’)    # only 2 moves from a goal>>> b1 4 2 3 _ 5 6 7 8>>> s = State(b, None, ‘init’)   >>> searcher = Searcher(-1)>>> goal = searcher.find_solution(s)>>> goal012345678-left-2>>> goal.print_moves_to()initial state:1 4 2 3 _ 5 6 7 8move the blank up:1 _ 2 3 4 5 6 7 8move the blank left:_ 1 2 3 4 5 6 7 8>>>Although the sequence of moves may vary because of randomness, the format of the output should be the same as what you see above.Once you have completed all of the methods specified above, you can use the driver function that we have provided to facilitate the process of solving a given puzzle. Find the file in your project folder and open it in Spyder. The driver function is called eight_puzzle, and it has two mandatory inputs:a string describing the board configuration for the initial statea string specifying the search algorithm that you want to use; for now, the only option is random.param – a parameter that is used to specify either a depth limit or the name of a heuristic function; we will give it default value of -1.  Computer Science Engineering & Technology Python Programming CS 111 Share QuestionEmailCopy link Comments (0)

Needs help with similar assignment?

We are available 24x7 to deliver the best services and assignment ready within 6-12 hours? Order a custom-written, plagiarism-free paper

Order Over WhatsApp Place an Order Online

Do you have an upcoming essay or assignment due?

All of our assignments are originally produced, unique, and free of plagiarism.

If yes Order Similar Paper