File size: 6,361 Bytes
413c24f
cd7e94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12f6869
cd7e94f
 
 
 
 
 
 
 
 
 
 
 
 
 
1eb4de2
cd7e94f
 
 
 
 
 
 
12f6869
cd7e94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e385476
cd7e94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b223c89
cd7e94f
 
 
 
b223c89
cd7e94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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']}")