def react(self, tx): """ If the agent now has more social bonds than they can manage we drop new non carer friends then those not used recently and finally reduce the carers from most recent. :param tx: neo4j write transaction :return: None """ super(Patient, self).react(tx) self.contacts = intf.agentcontacts(tx, self.id, "Agent") carers = self.contacts + intf.agentcontacts(tx, self.id, "Agent", "Carer") if len(self.contacts) > self.social: while len(carers) > self.social: carer_drop = None latest_time = 0 for carer in carers: if carer["created"] > latest_time: carer_drop = carer latest_time = carer["created"] intf.deletecontact(tx, self.id, carer_drop.end_node["id"], "Agent", "Carer") if len(carers) == self.social: for contact in contacts: intf.deletecontact(tx, self.id, contact.end_node["id"], "Agent", "Agent") if len(carers) + len(contacts) > self.social: for contact in contacts: if len(carers) + len(contacts) > self.social: if intf.gettime() - contact['created'] < 5: intf.deletecontact(tx, self.id, contact.end_node["id"], "Agent", "Agent") oldest_usage = 0 contact_drop = None while len(carers) + len(contacts) > self.social: for contact in contacts: if intf.gettime() - contact["usage"] > oldest_usage: contact_drop = contact oldest_usage = intf.gettime() - contact["usage"] intf.deletecontact(tx, self.id, contact_drop.end_node["id"], "Agent", "Agent")
def update(self, tx): """ Check agent contacts compare list with co-located agents and update co-located contacts with new last usage :param tx: neo4j database write transaction :return: None """ super(Patient, self).update(tx) # Update existing com links with latest co-location self.contacts = intf.agentcontacts(tx, self.id, "Patient") if self.contacts and self.colocated: update = [ contact for contact in self.contacts if contact in self.colocated ] for contact in update: intf.updatecontactedge(tx, contact.end_node["id"], self.id, "last_usage", intf.gettime())
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 payment(self, tx): """ Modifies chosen edge and agent. These include mobility, confidence and energy modifications. :param tx: neo4j database write transaction :return: None """ super(Patient, self).payment(tx) # Deduct energy used on edge if "energy" in self.choice.keys(): if "energy" in self.choice.end_node.keys(): if self.choice["energy"] + self.choice.end_node[ "energy"] > self.current_energy: # Check for carers carers = intf.agentcontacts(tx, self.id, "Agent", "Carer") # Check for sufficient energy for carer in carers: print(carer) if carer.end_node["energy"] >= self.choice["energy"]: intf.updatenode(tx, carer.end_node["id"], "energy", carer.end_node["energy"] - self.choice["energy"], label='Carer') self.current_energy = self.current_energy + self.choice[ "energy"] intf.updateagent(tx, self.id, "energy", self.current_energy) intf.updatecontactedge(tx, self.id, carer.end_node["id"], "usage", intf.gettime(tx), "Agent", "Carer") break else: return False self.current_energy = npr.normal(self.choice["energy"], 0.05) + self.current_energy intf.updateagent(tx, self.id, "energy", self.current_energy) # mod variables based on edges if "modm" in self.choice: self.mobility = self.positive( npr.normal(self.choice["modm"], 0.05) + self.mobility) intf.updateagent(tx, self.id, "mob", self.mobility) if self.mobility == 0: if self.wellbeing != "Fallen": self.wellbeing = "Fallen" intf.updateagent(tx, self.id, "wellbeing", self.wellbeing) clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Fallen, " + str(clock) + ")" elif self.mobility > 1: if self.wellbeing != "Healthy": self.wellbeing = "Healthy" intf.updateagent(tx, self.id, "wellbeing", self.wellbeing) clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Healthy, " + str(clock) + ")" elif self.mobility <= 1: if self.wellbeing == "Healthy": self.wellbeing = "At risk" intf.updateagent(tx, self.id, "wellbeing", self.wellbeing) clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (At risk, " + str(clock) + ")" if "modmood" in self.choice: self.mood = self.positive( npr.normal(self.choice["modmood"], 0.05) + self.mood) intf.updateagent(tx, self.id, "mood", self.mood) return True