def talk(self, tx): """ Determine if agent forms a new social link with random co-located agent based on their social values and shortest social path :param tx: neo4j write transaction :return: None """ super(Patient, self).talk(tx) if self.colocated: newcontacts = [ nc for nc in self.colocated if nc not in self.contacts ] if newcontacts: if len(newcontacts) > 1: newfriend = npr.sample(newcontacts) else: newfriend = newcontacts # Based on relative social values and length of shortest path set probability for forming link with a # randomly sampled co-located unknown agent. social from 2-8 per agent, combined from 4-16. So combined -4 # over 24 gives value between 0 and 0.5 plus the if minimum path greater than 6 nothing, else from m=2-6 # then 1/(2m-2) gives 0.1-0.5 (m=1 or 0 means itself or already connected). prob1 = (newfriend.end_node["social"] + self.social - 4) / 24 sp = intf.shortestpath(tx, self.id, newfriend.end_node["id"], 'Agent', 'Social') if sp < 2: prob2 = 0 elif sp > 6: prob2 = 0 else: prob2 = 1 / (2 * sp - 2) if npr.random(1) <= (prob1 + prob2): # form friend link intf.createedge( self.id, newfriend.end_node["id"], 'Agent', 'Agent', 'SOCIAL:FRIEND', 'created: ' + intf.gettime() + ', usage: ' + intf.gettime() + ', carer: False') self.contacts = newfriend else: self.contacts = None else: self.contacts = None else: self.contacts = None
def listen(self, tx): """ If the agent has a new social link check if the new friend has a link to a carer if they do for a random carer they form a link with a .5 chance. :param tx: neo4j write transaction :return: None """ super(Patient, self).listen(tx) if self.contacts: carers = intf.agentcontacts(tx, self.contacts.end_node["id"], "Agent", "Carer") carers = [carer for carer in carers if carer["carer"]] carer = npr.sample(carers) if npr.random(1) < 0.5: intf.createedge( self.id, carer.end_node["id"], 'Patient', 'Carer', 'SOCIAL:FRIEND', 'created: ' + intf.gettime() + ', usage: ' + intf.gettime() + ', carer: True')
def generate_population(tx, ps): """ Generates the required number of agents and starts them at the Home node. Builds social connections between agents. :param tx: neo4j database write transaction :param ps: population size :return: None """ fa = Patient(None) for j in range(ps // 4): tx.run("CREATE (a:Carer {id:{j_id}, energy:20})", j_id=j) for i in range(ps): fa.generator(tx, [0.8, 0.9, 1, [2, 0, 1, 2], 2, 8]) if npr.random(1) < 0.5: if npr.random(1) < 0.5: samplesize = 2 else: samplesize = 1 newfriends = npr.choice(range(ps // 4), size=samplesize, replace=False) for nf in newfriends: intf.createedge( tx, i, nf, 'Agent', 'Carer', 'SOCIAL', 'created: ' + str(intf.gettime(tx)) + ', usage: ' + str(intf.gettime(tx)) + ', carer: True') intf.createedge(tx, i, nf, 'Agent', 'Carer', 'FRIEND') for i in range(ps): newfriends = npr.choice(range(ps), size=npr.choice(range(3)), replace=False) for nf in newfriends: if not nf == i: intf.createedge( tx, i, nf, 'Agent', 'Agent', 'SOCIAL', 'created: ' + str(intf.gettime(tx)) + ', usage: ' + str(intf.gettime(tx)) + ', carer: False') intf.createedge(tx, i, nf, 'Agent', 'Agent', 'FRIEND')