Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,21 +25,21 @@ nurse_actions = ["Monitor Vitals", "Administer Medication", "Report to Doctor",
|
|
| 25 |
|
| 26 |
# Reward and penalty system
|
| 27 |
rewards = {
|
| 28 |
-
"Prescribe Medication":
|
| 29 |
-
"Recommend Tests":
|
| 30 |
-
"Consult Clinician":
|
| 31 |
-
"Schedule Surgery":
|
| 32 |
-
"Monitor Vitals":
|
| 33 |
-
"Administer Medication":
|
| 34 |
-
"Review Diagnostic Test":
|
| 35 |
-
"Recommend Additional Tests":
|
| 36 |
}
|
| 37 |
|
| 38 |
penalties = {
|
| 39 |
-
"Wrong Medication": -
|
| 40 |
-
"Missed Diagnosis": -
|
| 41 |
-
"Incorrect Test Recommendation": -
|
| 42 |
-
"Stress-Induced Mistake": -
|
| 43 |
}
|
| 44 |
|
| 45 |
# Initialize the session state for counting treatments
|
|
@@ -58,13 +58,13 @@ class Agent:
|
|
| 58 |
self.q_table = np.zeros((len(patient_conditions), len(doctor_actions if agent_type == "Doctor" else nurse_actions)))
|
| 59 |
self.state = random.choice(patient_conditions)
|
| 60 |
|
| 61 |
-
def choose_action(self, exploration_rate=0.
|
| 62 |
if random.uniform(0, 1) < exploration_rate: # Explore
|
| 63 |
return random.randint(0, len(doctor_actions)-1 if self.agent_type == "Doctor" else len(nurse_actions)-1)
|
| 64 |
else: # Exploit (choose the best action based on Q-values)
|
| 65 |
return np.argmax(self.q_table)
|
| 66 |
|
| 67 |
-
def update_q_value(self, state, action, reward, learning_rate=0.
|
| 68 |
old_q_value = self.q_table[state, action]
|
| 69 |
best_future_q_value = np.max(self.q_table)
|
| 70 |
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
|
@@ -104,7 +104,11 @@ if st.button("Run Simulation"):
|
|
| 104 |
st.write(f"Nurse's Chosen Action: {nurse_actions[nurse_action]}")
|
| 105 |
|
| 106 |
# Complications that can arise during treatment
|
| 107 |
-
complication = random.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if complication:
|
| 109 |
st.subheader(f"Complication: {complication}")
|
| 110 |
if complication == "Allergic Reaction":
|
|
@@ -137,7 +141,10 @@ if st.button("Run Simulation"):
|
|
| 137 |
st.write(f"Patient Satisfaction: {st.session_state.patient_satisfaction}")
|
| 138 |
|
| 139 |
# Simulated patient feedback and next steps
|
| 140 |
-
next_step = random.
|
|
|
|
|
|
|
|
|
|
| 141 |
st.write(f"Patient Status after treatment: {next_step}")
|
| 142 |
|
| 143 |
# Increment performance based on outcome
|
|
|
|
| 25 |
|
| 26 |
# Reward and penalty system
|
| 27 |
rewards = {
|
| 28 |
+
"Prescribe Medication": 12, # Slightly increased reward
|
| 29 |
+
"Recommend Tests": 7, # Slightly increased reward
|
| 30 |
+
"Consult Clinician": 9,
|
| 31 |
+
"Schedule Surgery": 17,
|
| 32 |
+
"Monitor Vitals": 4,
|
| 33 |
+
"Administer Medication": 12,
|
| 34 |
+
"Review Diagnostic Test": 7,
|
| 35 |
+
"Recommend Additional Tests": 6
|
| 36 |
}
|
| 37 |
|
| 38 |
penalties = {
|
| 39 |
+
"Wrong Medication": -5, # Reduced penalty
|
| 40 |
+
"Missed Diagnosis": -10,
|
| 41 |
+
"Incorrect Test Recommendation": -3,
|
| 42 |
+
"Stress-Induced Mistake": -7
|
| 43 |
}
|
| 44 |
|
| 45 |
# Initialize the session state for counting treatments
|
|
|
|
| 58 |
self.q_table = np.zeros((len(patient_conditions), len(doctor_actions if agent_type == "Doctor" else nurse_actions)))
|
| 59 |
self.state = random.choice(patient_conditions)
|
| 60 |
|
| 61 |
+
def choose_action(self, exploration_rate=0.03): # Further reduce exploration rate
|
| 62 |
if random.uniform(0, 1) < exploration_rate: # Explore
|
| 63 |
return random.randint(0, len(doctor_actions)-1 if self.agent_type == "Doctor" else len(nurse_actions)-1)
|
| 64 |
else: # Exploit (choose the best action based on Q-values)
|
| 65 |
return np.argmax(self.q_table)
|
| 66 |
|
| 67 |
+
def update_q_value(self, state, action, reward, learning_rate=0.25, discount_factor=0.95): # Increase learning rate
|
| 68 |
old_q_value = self.q_table[state, action]
|
| 69 |
best_future_q_value = np.max(self.q_table)
|
| 70 |
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
|
|
|
| 104 |
st.write(f"Nurse's Chosen Action: {nurse_actions[nurse_action]}")
|
| 105 |
|
| 106 |
# Complications that can arise during treatment
|
| 107 |
+
complication = random.choices(
|
| 108 |
+
[None, "Allergic Reaction", "Unexpected Complication"],
|
| 109 |
+
weights=[0.6, 0.2, 0.2] # 60% chance no complications, 20% for each complication
|
| 110 |
+
)[0]
|
| 111 |
+
|
| 112 |
if complication:
|
| 113 |
st.subheader(f"Complication: {complication}")
|
| 114 |
if complication == "Allergic Reaction":
|
|
|
|
| 141 |
st.write(f"Patient Satisfaction: {st.session_state.patient_satisfaction}")
|
| 142 |
|
| 143 |
# Simulated patient feedback and next steps
|
| 144 |
+
next_step = random.choices(
|
| 145 |
+
["Recovery", "Further Treatment Needed", "Complication"],
|
| 146 |
+
weights=[0.6, 0.3, 0.1] # Biased toward successful recovery
|
| 147 |
+
)[0]
|
| 148 |
st.write(f"Patient Status after treatment: {next_step}")
|
| 149 |
|
| 150 |
# Increment performance based on outcome
|