class Agent: class Environment: def __init__(self,state:list=['D','D']): self.loc1_ = 'A' self.loc2_ = 'B' self.state_ = state #state: ['D','D'] etc. print(self.__repr__()) def __str__(self): return 'Environment created with {} and {} in a state of {}'.format(self.loc1_,self.loc2_,self.state_) def __repr__(self): return f'Environment(state={self.state_},location1 = {self.loc1_},location2 = {self.loc2_})' def getloc1(self): return self.loc1_ def getloc2(self): return self.loc2_ def getstate(self): return self.state_ def getloc1_state(self): return self.state_[0] def getloc2_state(self): return self.state_[1] def setloc1_state(self,state): #state='D' self.state_[0] = state #print(f'Just changed state of enviro: {self}') def setloc2_state(self,state): #state='D' self.state_[1] = state print(state) def changestate(self,loc,state): try: if loc == 'A': self.setloc1_state(state) print(self) if loc == 'B': self.getloc2_state(state) print(self) except ValueError: print('Invalid location provided. Can only be A or B') def __init__(self,percept:list,location:'str',action:'str'='N',goal:list=['C','C']): self.percept_ = percept self.cost_ = 0 self.location_ = location self.action_ = action self.goal_ = goal self.enviro_ = self.Environment(percept) print(self.__repr__()) def __str__(self): return 'Agent with percept: {}, location: {}, goal: {} and cost: {}'.format(self.percept_,self.location_,self.goal_,self.cost_) def __repr__(self): return f'Agent(percept={self.percept_},location={self.location_},action={self.action_},goal={self.goal_})' def setpercept(self,percept): self.percept_ = percept def getpercept(self): return self.percept def setlocation(self,location): self.location_ = location def getlocation(self): return self.location_ def setcost(self,cost): self.cost_ += cost def getcost(self): return self.cost_ #def goaltest(self,state): # if isinstance(state,list): # return self.goal_ in state # else: # return self.goal_ == state # return false def goaltest(self,state): return self.goal_ == state def suck(self): place = self.getlocation() print(f'Cleaning rrom {place}') if place == 'A': print(f'current state of enviro: {self.enviro_.getstate()}') print(f'setting loc1 status to C') self.enviro_.setloc1_state(state='C') if place == 'B': print(f'setting loc2 status to C') self.enviro_.setloc2_state(state='C') def move(self,direction): if direction == 'L': self.setlocation('A') if direction == 'R': self.setlocation('B') def lookup_action(self,state:list): #print('STATE passed to lookp_action(): ',state) place = self.getlocation() #current_state = state.insert(0,place) current_state = state table={ tuple(['A','D','D']):'S', tuple(['A','D','C']):'S', tuple(['A','C','D']):'R', tuple(['A','C','C']):'N', tuple(['B','D','D']):'S', tuple(['B','D','C']):'L', tuple(['B','C','D']):'S', tuple(['B','C','C']):'N' } print(f'{table[tuple(current_state)]} returned as action') return table[tuple(current_state)] def execute_action(self,percept): #print(f'#####################percept passed to Agent.execute_action)(): {percept}') place = self.getlocation() #print(f'############################################agent location/place: {place}') state = percept.copy() #Python passes object reference. So need a copy to work with, else percept lebgth grows #print(f'#############################################state to be procced: {state}') state.insert(0,place) #print(f'##################################state after place was inserted: {state}') action = self.lookup_action(state) if action == 'S': self.suck() self.setcost(1) if action == 'N': print('No action needed') else: self.move(action) def main(): init_state = ['D','D'] count = 0 vacuum = Agent(percept=init_state,location='A') while vacuum.goaltest(vacuum.enviro_.getstate()) == False: if count > 10: break count +=1 print('Goal test in main: ', vacuum.enviro_.state_ == ['C','C']) vacuum.execute_action(vacuum.enviro_.getstate()) print(f'END STATE: {vacuum.enviro_.getstate()} in {count} moves') main()