Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import random | |
# Initialize constants | |
DOCTOR_ACTIONS = ["Prescribe Medication", "Recommend Tests", "Consult Clinician", "Schedule Surgery"] | |
NURSE_ACTIONS = ["Monitor Vitals", "Administer Medication", "Report to Doctor", "Assist Surgery"] | |
PATIENT_CONDITIONS = ["Healthy", "Mild Illness", "Chronic Illness", "Emergency"] | |
DOCTOR_EMOTIONS = ["Calm", "Stressed", "Overwhelmed"] | |
NURSE_EMOTIONS = ["Focused", "Fatigued", "Panicked"] | |
# Rewards and Penalties | |
REWARDS = { | |
"Prescribe Medication": 12, | |
"Recommend Tests": 7, | |
"Consult Clinician": 9, | |
"Schedule Surgery": 17, | |
"Monitor Vitals": 4, | |
"Administer Medication": 12, | |
} | |
PENALTIES = { | |
"Wrong Medication": -5, | |
"Missed Diagnosis": -10, | |
"Stress-Induced Mistake": -7, | |
} | |
# Initialize session state for metrics and satisfaction | |
if "performance_metrics" not in st.session_state: | |
st.session_state.performance_metrics = { | |
"Doctor": {"successful_treatments": 0, "failed_treatments": 0}, | |
"Nurse": {"successful_assists": 0, "failed_assists": 0} | |
} | |
if "patient_satisfaction" not in st.session_state: | |
st.session_state.patient_satisfaction = 100 | |
# Define the Agent class for Q-learning | |
class Agent: | |
def __init__(self, agent_type): | |
self.agent_type = agent_type | |
self.actions = DOCTOR_ACTIONS if agent_type == "Doctor" else NURSE_ACTIONS | |
self.q_table = np.zeros((len(PATIENT_CONDITIONS), len(self.actions))) | |
def choose_action(self, state, exploration_rate=0.05): | |
if random.uniform(0, 1) < exploration_rate: # Explore | |
return random.randint(0, len(self.actions)-1) | |
else: # Exploit (choose best action) | |
return np.argmax(self.q_table[state]) | |
def update_q_value(self, state, action, reward, learning_rate=0.1, discount_factor=0.9): | |
old_q_value = self.q_table[state, action] | |
best_future_q_value = np.max(self.q_table) | |
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value) | |
self.q_table[state, action] = new_q_value | |
# Instantiate agents | |
doctor_agent = Agent("Doctor") | |
nurse_agent = Agent("Nurse") | |
# Function to simulate a special event | |
def simulate_special_event(): | |
event = random.choice([None, "Disease Outbreak", "Resource Shortage"]) | |
if event == "Disease Outbreak": | |
st.subheader("Special Event: Disease Outbreak") | |
st.write("A sudden disease outbreak has flooded the hospital with new patients. Resources are limited!") | |
elif event == "Resource Shortage": | |
st.subheader("Special Event: Resource Shortage") | |
st.write("A medical supply shortage is impacting the hospital. Staff must prioritize high-risk patients.") | |
return event | |
# Function to handle complications during treatment | |
def handle_complications(): | |
complication = random.choices( | |
[None, "Allergic Reaction", "Unexpected Complication"], | |
weights=[0.6, 0.2, 0.2] | |
)[0] | |
penalty = 0 | |
if complication: | |
st.subheader(f"Complication: {complication}") | |
if complication == "Allergic Reaction": | |
st.write("The patient has developed an allergic reaction to the prescribed medication!") | |
penalty = PENALTIES["Wrong Medication"] | |
st.session_state.patient_satisfaction -= 10 | |
elif complication == "Unexpected Complication": | |
st.write("An unexpected complication occurred during surgery!") | |
penalty = PENALTIES["Stress-Induced Mistake"] | |
st.session_state.patient_satisfaction -= 15 | |
return penalty | |
# Main simulation button logic | |
if st.button("Run Simulation"): | |
# Simulate a special event | |
special_event = simulate_special_event() | |
# Patient condition simulation | |
patient_state = random.choice(PATIENT_CONDITIONS) | |
st.write(f"Simulated Patient Condition: {patient_state}") | |
patient_index = PATIENT_CONDITIONS.index(patient_state) | |
# Doctor and nurse emotions | |
doctor_emotion = random.choice(DOCTOR_EMOTIONS) | |
nurse_emotion = random.choice(NURSE_EMOTIONS) | |
st.write(f"Doctor's Emotional State: {doctor_emotion}") | |
st.write(f"Nurse's Emotional State: {nurse_emotion}") | |
# Doctor Action | |
doctor_action_index = doctor_agent.choose_action(patient_index) | |
doctor_action = DOCTOR_ACTIONS[doctor_action_index] | |
st.write(f"Doctor's Chosen Action: {doctor_action}") | |
# Nurse Action | |
nurse_action_index = nurse_agent.choose_action(patient_index) | |
nurse_action = NURSE_ACTIONS[nurse_action_index] | |
st.write(f"Nurse's Chosen Action: {nurse_action}") | |
# Handle potential complications | |
penalty = handle_complications() | |
# Reward or penalty | |
reward = REWARDS.get(doctor_action, 0) if penalty == 0 else penalty | |
if doctor_emotion in ["Stressed", "Overwhelmed"]: | |
penalty += PENALTIES["Stress-Induced Mistake"] | |
# Update Q-values | |
doctor_agent.update_q_value(patient_index, doctor_action_index, reward) | |
st.write(f"Doctor's Reward/Penalty: {reward}") | |
st.write(f"Patient Satisfaction: {st.session_state.patient_satisfaction}") | |
# Outcome and Performance Metrics Update | |
outcome = random.choices( | |
["Recovery", "Further Treatment Needed", "Complication"], | |
weights=[0.6, 0.3, 0.1] | |
)[0] | |
st.write(f"Patient Status after Treatment: {outcome}") | |
if outcome == "Recovery": | |
st.session_state.performance_metrics["Doctor"]["successful_treatments"] += 1 | |
st.session_state.performance_metrics["Nurse"]["successful_assists"] += 1 | |
else: | |
st.session_state.performance_metrics["Doctor"]["failed_treatments"] += 1 | |
st.session_state.performance_metrics["Nurse"]["failed_assists"] += 1 | |
st.write("Simulation completed! Run again for different outcomes.") | |
# Display performance metrics | |
st.subheader("Performance Metrics:") | |
st.write(f"Doctor's Successful Treatments: {st.session_state.performance_metrics['Doctor']['successful_treatments']}") | |
st.write(f"Doctor's Failed Treatments: {st.session_state.performance_metrics['Doctor']['failed_treatments']}") | |
st.write(f"Nurse's Successful Assists: {st.session_state.performance_metrics['Nurse']['successful_assists']}") | |
st.write(f"Nurse's Failed Assists: {st.session_state.performance_metrics['Nurse']['failed_assists']}") | |