Package teamwork :: Package agent :: Module MemoryAgent
[hide private]
[frames] | no frames]

Source Code for Module teamwork.agent.MemoryAgent

 1  from teamwork.agent.RecursiveAgent import RecursiveAgent 
 2  from copy import deepcopy 
 3   
4 -class MemoryAgent(RecursiveAgent):
5 ''' 6 Memory is stored in a list such that the most recent memory is the 0th element in the list 7 and the oldest memory is the n-1th memory in the list where n is the memory length 8 ''' 9
10 - def __init__(self,name=''):
11 RecursiveAgent.__init__(self,name) 12 self.memoryLength = 3 13 self.memory = []
14
15 - def updateMemory(self, actions, previousBeliefs):
16 memory = {} 17 18 #update the memory of the action taken 19 memory['actions'] = actions.copy() 20 21 #update beliefs 22 memory['previousBeliefs'] = deepcopy(previousBeliefs) 23 24 #insert the memory 25 self.memory.insert(0, memory) 26 27 #make sure we don't go over the memory length 28 if len(self.memory) > self.memoryLength: 29 self.memory.pop()
30 31 if __name__ == '__main__': 32 from teamwork.multiagent.GenericSociety import GenericSociety 33 from teamwork.multiagent.sequential import SequentialAgents 34 import teamwork.examples.school.SchoolClasses as classModule 35 from teamwork.multiagent.Historical import HistoricalAgents 36 37 #We need to get a scenario 38 society = GenericSociety() 39 society.importDict(classModule.classHierarchy) 40 41 #Instantiate the agents 42 victim = society.instantiate("Victim", "Victim") 43 onlooker = society.instantiate("Onlooker", "Onlooker") 44 bully = society.instantiate("Bully", "Bully") 45 teacher = society.instantiate("Teacher", "Teacher") 46 47 #Set up the relationships 48 victim.relationships['victim'] = ['Victim'] 49 onlooker.relationships['victim'] = ['Victim'] 50 bully.relationships['victim'] = ['Victim'] 51 52 #Instantiate the scenario 53 entities = [victim, onlooker, bully, teacher] 54 agents = SequentialAgents(entities) 55 agents.applyDefaults() 56 agents.compileDynamics() 57 58 result = agents.microstep() 59 agents.microstep() 60 61 print "\n\nmemory!\n\n" 62 # print bully.memory[0]['previousBeliefs']['state'].items()[0][0].keys()[1]['entity'] 63 # print bully.memory[0]['previousBeliefs']['state'].items()[0][0].keys() 64 print victim.entities.getStateKeys().keys() 65 66 # for agent in agents.activeMembers(): 67 # print agent.name 68 # for mem in agent.memory: 69 # print "Previous Beliefs:" 70 # for belkey in mem['previousBeliefs'].keys(): 71 # if belkey == 'state': 72 # print mem['previousBeliefs'][belkey].keys()[0] 73 # print "\n" 74 # 75 # print "\n\n" 76