-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel2_feedback.py
More file actions
163 lines (141 loc) · 6.32 KB
/
Copy pathlevel2_feedback.py
File metadata and controls
163 lines (141 loc) · 6.32 KB
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
159
160
161
162
163
import streamlit as st
import random
def run_level2_feedback():
st.set_page_config(page_title="BioFSM Level 2", layout="centered")
st.markdown("""
<style>
body { background-color: #0f0f0f; color: #f0f0f0; }
.stButton>button { background-color: #222; color: white; border: 1px solid #555; }
.stAlert { border-radius: 0.5em; }
</style>
""", unsafe_allow_html=True)
st.title("🧬 Level 2: Feedback Frenzy")
# ----- Sidebar Hints & How to Win -----
with st.sidebar:
st.markdown("## 📘 How to Win")
st.markdown("""
- Survive **10+ turns**.
- Output must be `ON` in **≥ 4 turns**.
- Inputs:
- 🧴 IPTG disables LacI
- 💬 AHL activates TetR (if LacI is OFF)
- 🔁 aTc resets system
- Mutations & feedback appear randomly.
""")
st.markdown("## 🧠 Hints")
if st.checkbox("Show Strategic Hints", value=False):
circuit = st.session_state.get("Level2_Circuit", {})
inputs = st.session_state.get("Level2_Inputs", {})
if circuit.get("LacI") == "ON" and not inputs.get("IPTG"):
st.info("🧴 Use IPTG to disable LacI.")
elif circuit.get("LacI") == "OFF" and not inputs.get("AHL"):
st.info("💬 LacI is off — use AHL to trigger TetR.")
elif circuit.get("TetR") == "ON" and circuit.get("Output") == "ON":
st.info("💡 Output is ON — now protect it!")
elif inputs.get("aTc"):
st.info("🔁 aTc resets all — useful in danger.")
# ----- State Setup -----
defaults = {
"Level2_Circuit": {"LacI": "ON", "TetR": "OFF", "Output": "OFF", "Feedback": False, "CI": "OFF"},
"Level2_Turn": 1,
"Level2_Inputs": {"IPTG": False, "AHL": False, "aTc": False},
"Level2_Mutations": [],
"Level2_OutputStreak": 0,
"Level2_OutputONCount": 0,
"Level2_Won": False
}
for key, value in defaults.items():
if key not in st.session_state:
st.session_state[key] = value
turn = st.session_state.Level2_Turn if isinstance(st.session_state.Level2_Turn, int) else 0
circuit = st.session_state.Level2_Circuit
if "CI" not in circuit:
circuit["CI"] = "OFF"
inputs = st.session_state.Level2_Inputs
st.subheader(f"🌒 Turn `{turn if not st.session_state.Level2_Won else '🏁'}`")
# Display current circuit state
st.markdown(f"""
**⚙️ Circuit State:**
- 🧬 LacI: `{circuit['LacI']}`
- 🧪 CI (Inhibitor): `{circuit['CI']}`
- 🦠 TetR: `{circuit['TetR']}`
- 💡 Output: `{circuit['Output']}`
- ☢️ Feedback: `{"ON ⚠️" if circuit['Feedback'] else "OFF ✅"}`
""")
# ----- Input Panel -----
st.markdown("### 🧪 Input Panel")
col1, col2, col3 = st.columns(3)
if col1.button("🧴 Apply IPTG"):
inputs["IPTG"] = True
if col2.button("💬 Add AHL"):
inputs["AHL"] = True
if col3.button("🔁 Apply aTc (Reset)"):
inputs["aTc"] = True
# ----- Random Mutation (20% chance) -----
if not st.session_state.Level2_Won and random.random() < 0.15:
mutated = random.choice(["LacI", "TetR", "Feedback", "CI"])
st.session_state.Level2_Mutations.append((turn, mutated))
if mutated == "LacI":
circuit["LacI"] = "OFF"
elif mutated == "TetR":
circuit["TetR"] = "ON"
elif mutated == "CI":
circuit["CI"] = "ON"
elif mutated == "Feedback":
circuit["Feedback"] = not circuit["Feedback"]
st.error(f"☣️ Mutation triggered: `{mutated}` flipped!")
# ----- Apply Input Logic -----
if inputs["IPTG"]:
circuit["LacI"] = "OFF"
if circuit["TetR"] == "ON" and random.random() < 0.1:
circuit["CI"] = "ON"
st.warning("⚠️ CI inhibitor triggered unexpectedly!")
if inputs["AHL"] and circuit["LacI"] == "OFF" and circuit["CI"] == "OFF":
circuit["TetR"] = "ON"
if circuit["TetR"] == "ON" and circuit["CI"] == "OFF":
circuit["Output"] = "ON"
if inputs["aTc"]:
circuit["LacI"] = "ON"
circuit["TetR"] = "OFF"
circuit["Output"] = "OFF"
circuit["CI"] = "OFF"
circuit["Feedback"] = False
st.warning("🔁 System reset — all core components cleared.")
# ----- Feedback Check -----
if circuit["Feedback"] and circuit["Output"] == "ON":
st.error("💥 Feedback trap triggered! Output was ON during feedback.")
st.markdown("🧨 **GAME OVER** — synthetic meltdown.")
st.stop()
# ----- Output Streak Tracking -----
if circuit["Output"] == "ON":
st.session_state.Level2_OutputStreak += 1
st.session_state.Level2_OutputONCount += 1
else:
st.session_state.Level2_OutputStreak = 0
st.success(f"✅ Output: `{circuit['Output']}`")
st.markdown(f"🔥 Streak: `{st.session_state.Level2_OutputStreak}`")
st.markdown(f"🧮 Total Output ON: `{st.session_state.Level2_OutputONCount}`")
# ----- Win Check -----
if not st.session_state.Level2_Won and turn >= 10 and st.session_state.Level2_OutputONCount >= 4:
st.session_state.Level2_Won = True
st.balloons()
st.success("🎉 Victory! You survived feedback hell.")
st.markdown("🥇 Level 2 complete. Onward to Level 3!")
# ----- Clear Inputs -----
st.session_state.Level2_Inputs = {"IPTG": False, "AHL": False, "aTc": False}
# ----- Turn Advance -----
if not st.session_state.Level2_Won:
st.session_state.Level2_Turn = int(st.session_state.Level2_Turn) + 1 if str(st.session_state.Level2_Turn).isdigit() else 1
# ----- Mutation History (Optional)
with st.expander("📉 Mutation Log"):
if st.session_state.Level2_Mutations:
for t, m in st.session_state.Level2_Mutations:
st.markdown(f"Turn `{t}` → `{m}` flipped")
else:
st.markdown("No mutations yet.")
# ----- Reset Button -----
st.divider()
if st.button("🔄 Restart Level 2"):
for key, value in defaults.items():
st.session_state[key] = value
st.rerun()