Question
''' 1) Put start state into structure and seen list 2) Get state from structure if structure is empty return failure 3) Check if state
''' 1) Put start state into structure and seen list 2) Get state from structure if structure is empty return failure 3) Check if state is the goal state if so return that state as the answer 4) Get children of state 5) For each child: if not yet seen, add to seen list and structure 6) Go back to 2 '''
class State: def get_actions(self): return [] def apply_action(self,action): return self def is_goal(self): return False def __hash__(self): return 0 def __str__(self): return "the state"
class VacuumWorld(State): def __init__(self,pos,floor): self.pos = pos self.floor = floor def get_actions(self): actions = ['suck'] if self.pos != 0: actions.append('left') if self.pos != len(self.floor)-1: actions.append('right') return actions def apply_action(self,action): next = VacuumWorld(self.pos,self.floor) if action == 'left': next.pos = next.pos - 1 elif action == 'right': next.pos = next.pos + 1 elif action == 'suck': next.floor = next.floor[0:next.pos]+' '+next.floor[next.pos+1:] return next def __str__(self): output = ' '*self.pos output += '^ ' output += self.floor return output def is_goal(self): return '*' not in self.floor def __hash__(self): return hash((self.pos,self.floor)) def __eq__(self,other): return self.pos == other.pos and self.floor == other.floor
def search(structure,start_state): seen = set() structure.add(start_state) seen.add(start_state) while not structure.is_empty(): current = structure.remove() if current.is_goal(): return current actions = current.get_actions() for action in actions: child = current.apply_action(action) if child not in seen: seen.add(child) structure.add(child) return False
class Structure: def add(self,thing): return 0 def remove(self): return 0
class Stack(Structure): def __init__(self): self.stack = [] def add(self,thing): self.stack.append(thing) def remove(self): return self.stack.pop() def is_empty(self): return len(self.stack)==0
Step 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