def adjustcapasity(txl, history, dynamic=True): """ Rule function that applies the capacity change algorithm in the case of two intervention node systems. This is uses a history variable that is cleared when the capacity is adjusted so that another five timesteps must pass before the capacity can be changed again. This only modifies OpenIntervention if dynamic is true else only intervention is modified. :param txl: neo4j database write transaction :param history: List of previous average times since discharge :param dynamic: indicates if adjustment to OpenIntervention is needed :return: history """ currenttimes = timesincedischarge(txl) if not currenttimes and history: currentav = history[-1] elif not currenttimes: currentav = 14 else: currentav = mean(currenttimes) history = history + [currentav] # If it has been less than 20 time steps since last change do nothing if len(history) < 20: return history else: if history[-5] - history[-1] < -1 and history[-1] > 5: if dynamic: if intf.getnodevalue( txl, "InterventionOpen", "cap", uid="name") > 0 and dynamic: intf.updatenode( txl, "Intervention", "cap", intf.getnodevalue( txl, "Intervention", "cap", uid="name") + 1, "name") intf.updatenode( txl, "InterventionOpen", "cap", intf.getnodevalue( txl, "InterventionOpen", "cap", uid="name") - 1, "name") else: intf.updatenode( txl, "Intervention", "cap", intf.getnodevalue(txl, "Intervention", "cap", uid="name") + 1, "name") return [] elif history[-5] - history[-1] > 0 and history[-1] < 5: if intf.getnodevalue(txl, "Intervention", "cap", uid="name") > 0: intf.updatenode( txl, "Intervention", "cap", intf.getnodevalue(txl, "Intervention", "cap", uid="name") - 1, "name") if dynamic: intf.updatenode( txl, "InterventionOpen", "cap", intf.getnodevalue( txl, "InterventionOpen", "cap", uid="name") + 1, "name") return [] else: return history
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
def learn(self, tx, choice): """ Agent is changed by node and can change node and edge it arrived by. This can include changes to decision making parameters. :param tx: neo4j database write transaction :param choice: Chosen edge for move :return: None """ super(FallAgent, self).learn(tx, choice) # modify mob, conf, res and energy based on new node if self.fall and self.fall != "Mild": 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) + ")" if "modm" in choice.end_node: self.mobility = self.positive( npr.normal(choice.end_node["modm"], 0.05) + self.mobility) intf.updateagent(tx, self.id, "mob", self.mobility) # check for updates to wellbeing and log any changes 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 "modc" in choice.end_node: self.confidence = self.positive( npr.normal(choice.end_node["modc"], 0.05) + self.confidence) intf.updateagent(tx, self.id, "conf", self.confidence) if "modrc" in choice.end_node: self.confidence_resources = self.positive( npr.normal(choice.end_node["modrc"], 0.05) + self.confidence_resources) intf.updateagent(tx, self.id, "conf_res", self.confidence_resources) if "modrm" in choice.end_node: self.mobility_resources = self.positive( npr.normal(choice.end_node["modrm"], 0.05) + self.mobility) intf.updateagent(tx, self.id, "mob_res", self.mobility_resources) if "energy" in choice.end_node: self.current_energy = npr.normal(choice.end_node["energy"], 0.05) + self.current_energy intf.updateagent(tx, self.id, "energy", self.current_energy) # log going into care if choice.end_node["name"] == "Care": clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Care, " + str(clock) + ")" if "cap" in choice.end_node.keys(): intf.updatenode(tx, choice.end_node["name"], "load", choice.end_node["load"] + 1, "name") if self.view[0]["name"] == "Hos" and choice.end_node["name"] != "Hos": clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Discharged, " + str(clock) + ")" intf.updateagent(tx, self.id, "log", str(self.log))
def learn(self, tx, choice): """ Agent is changed by node and can change node and edge it arrived by. This can include changes to decision making parameters. :param tx: neo4j database write transaction :param choice: Chosen edge for move :return: None """ super(Patient, self).learn(tx, choice) # modify mob, conf, res and energy based on new node if self.fall and self.fall != "Mild": 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) + ")" if "modm" in choice.end_node: self.mobility = self.positive( npr.normal(choice.end_node["modm"], 0.05) + self.mobility) intf.updateagent(tx, self.id, "mob", self.mobility) # check for updates to wellbeing and log any changes 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 choice.end_node: self.mood = self.positive( npr.normal(choice.end_node["modmood"], 0.05) + self.mood) intf.updateagent(tx, self.id, "mood", self.mood) if "energy" in choice.end_node: self.current_energy = npr.normal(choice.end_node["energy"], 0.05) + self.current_energy energy_change = self.current_energy - self.energy intf.updateagent(tx, self.id, "energy", self.current_energy) edge_types = ["social", "fall", "medical", "inactive"] for i in range(len(edge_types)): if choice["type"] == edge_types[i]: if energy_change < 0: self.inclination[i] = self.inclination[i] + 1 elif energy_change > 0: self.inclination[i] = self.positive( self.inclination[i] - 1) # change to inclination based on mobility, energy and mood if self.current_energy > 0.8: self.inclination[0] = self.inclination[0] + 1 elif self.current_energy < 0.2: self.inclination[3] = self.inclination[3] + 1 if self.mood > 0.8: self.inclination[0] = self.inclination[0] - 1 self.inclination[3] = self.inclination[3] + 1 elif self.mood < 0.2: self.inclination[0] = self.inclination[0] + 1 self.inclination[3] = self.inclination[3] - 1 if self.mobility < 0.4: self.inclination[2] = self.inclination[2] + 1 self.inclination[3] = self.inclination[3] + 1 elif self.mobility > 0.8: self.inclination[3] = self.inclination[3] - 1 intf.updateagent(tx, self.id, "inclination", self.inclination) # log going into care if choice.end_node["name"] == "Care": clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Care, " + str(clock) + ")" if "cap" in choice.end_node.keys(): intf.updatenode(tx, choice.end_node["name"], "load", choice.end_node["load"] + 1, "name") if self.view[0]["name"] == "Hos" and choice.end_node["name"] != "Hos": clock = tx.run("MATCH (a:Clock) " "RETURN a.time").values()[0][0] self.log = self.log + ", (Discharged, " + str(clock) + ")" intf.updateagent(tx, self.id, "log", str(self.log))