File size: 11,475 Bytes
c395f6a
 
5636c9d
 
 
 
 
 
 
 
 
 
 
 
c395f6a
 
5636c9d
c395f6a
5636c9d
 
c395f6a
 
5636c9d
c395f6a
 
5636c9d
 
c395f6a
 
5636c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f6a
5636c9d
 
c395f6a
5636c9d
 
 
 
 
 
c395f6a
5636c9d
 
c395f6a
5636c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f6a
5636c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f6a
5636c9d
 
 
c395f6a
5636c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f6a
5636c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f6a
 
5636c9d
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
GridMind-RL Full Unified Demo — All 4 Themes Running Together
-------------------------------------------------------------
This single script demonstrates all hackathon themes in one flow.
Each step proves a theme and prints WHY it matters for judges.

Themes:
  Theme 1 — Multi-Agent: 3 buildings + coordinator + /feeder
  Theme 2 — Long-Horizon Planning: Task 4 instruction cards over 96 steps
  Theme 3 — World Modeling: /simulate before committing actions
  Theme 4 — Self-Improvement: Curriculum auto-advances task difficulty

Run: python scripts/full_demo.py
"""

from __future__ import annotations
import json
import sys
import time
import requests

ENV_URL = "http://localhost:7860"


def bold(text: str) -> str:
    return f"**{text}**"


def section(n: int, title: str, theme: str) -> None:
    print(f"\n{'='*62}")
    print(f"  STEP {n}{bold(title)}  [{theme}]")
    print(f"{'='*62}")


def check_pass(text: str) -> None:
    print(f"  [OK] {text}")


def check_warn(text: str) -> None:
    print(f"  [!] {text}")


def show_info(label: str, value: str) -> None:
    print(f"  {label}: {value}")


def r(path: str, method="GET", json_data=None) -> dict:
    url = f"{ENV_URL}{path}"
    try:
        if method == "GET":
            resp = requests.get(url, timeout=10)
        else:
            resp = requests.post(url, json=json_data, timeout=10)
        resp.raise_for_status()
        return resp.json()
    except Exception as e:
        check_warn(f"Request failed: {e}")
        return {}


def step(n: int, title: str, theme: str, fn):
    section(n, title, theme)
    result = fn()
    if result is not False:
        check_pass("Passed")
    return result


# ── STEP 1 — Health Check ──────────────────────────────────────────────
def do_step1():
    data = r("/health")
    show_info("Status", data.get("status", "?") + " OK" if data.get("status") == "ok" else data.get("status", "?"))
    show_info("Version", data.get("version", "?"))
    return data.get("status") == "ok"


# ── STEP 2 — OpenEnv /info Metadata ──────────────────────────────────
def do_step2():
    data = r("/info")
    show_info("Name", data.get("name", "?"))
    show_info("Version", data.get("version", "?"))
    themes = data.get("themes", [])
    show_info("Themes", ", ".join(themes))
    endpoints = data.get("endpoints", [])
    show_info("Endpoints", f"{len(endpoints)} registered")
    print(f"  Sample endpoints: {endpoints[:5]}")
    return len(themes) >= 4 and "multi-agent" in themes


# ── STEP 3 — All 4 Tasks Available ────────────────────────────────────
def do_step3():
    data = r("/tasks")
    tasks = data if isinstance(data, list) else data.get("tasks", [])
    print(f"  {len(tasks)} tasks available:")
    for t in tasks:
        print(f"    Task {t['id']}: {t['name']} ({t['difficulty']})")
    ids = [t["id"] for t in tasks]
    return 1 in ids and 2 in ids and 3 in ids and 4 in ids


# ── STEP 4 — Theme 1: Multi-Agent Reset ───────────────────────────────
def do_step4():
    data = r("/reset", "POST", {"task_id": 3, "num_buildings": 3, "seed": 42})
    obs_list = data.get("observations", [])
    print(f"  Buildings in federation: {len(obs_list)}")
    for b in obs_list:
        tid = b.get("building_id", 0)
        temp = round(b.get("indoor_temperature", 0), 1)
        storage = round(b.get("thermal_storage_level", 0) * 100)
        cost = round(b.get("cumulative_cost", 0), 2)
        print(f"    Building {tid}: {temp}C | storage {storage}% | cost ${cost}")
    return len(obs_list) == 3


# ── STEP 5 — Theme 1: Fleet-wide Feeder View ─────────────────────────
def do_step5():
    data = r("/feeder")
    total = round(data.get("total_demand_kw", 0), 1)
    limit = data.get("feeder_limit_kw", 0)
    overload = data.get("feeder_overload", False)
    util = round(data.get("utilization_pct", 0), 1)
    buildings = data.get("buildings", [])
    feeder_status = "OK" if not overload else "OVERLOAD"
    print(f"  Feeder: {total} / {limit} kW [{feeder_status}]")
    print(f"  Utilization: {util}%")
    for b in buildings:
        bid = b.get("building_id", "?")
        dem = round(b.get("current_demand_kw", 0), 1)
        pm = b.get("price_multiplier", 1.0)
        print(f"    Building {bid}: {dem} kW | price mult {pm}x")
    return total > 0 and len(buildings) == 3


# ── STEP 6 — Theme 1: Coordinator Price Signals ───────────────────────
def do_step6():
    multipliers = [1.5, 0.8, 1.0]
    r("/coordinate", "POST", {"price_multipliers": multipliers})
    print(f"  Set price signals: {multipliers}")
    feeder = r("/feeder")
    blds = feeder.get("buildings", [])
    for i, b in enumerate(blds):
        pm = b.get("price_multiplier", "?")
        signal = ">> expensive (will conserve)" if pm > 1.2 else "<< cheap (can charge)"
        print(f"    Building {i}: {pm}x | {signal}")
    return True


# ── STEP 7 — Theme 3: World Modeling /simulate ───────────────────────
def do_step7():
    action = {
        "hvac_power_level": 0.8,
        "thermal_charge_rate": 0.5,
        "batch_job_slot": 0,
        "load_shed_fraction": 0.0,
        "building_id": 0,
    }
    # Check step counter before simulate
    state_before = r("/state")
    step_before = state_before.get("step", "?")
    print(f"  Step before simulate: {step_before}")

    sim_data = r("/simulate", "POST", [action])
    results = sim_data.get("results", [])
    print(f"  Simulated 1 action ahead")
    if results:
        r0 = results[0]
        obs = r0.get("observation", {})
        reward = round(r0.get("reward", 0), 3)
        print(f"    Predicted reward: {reward}")
        print(f"    Predicted temp: {round(obs.get('indoor_temperature', 0), 1)}C")
        print(f"    Predicted storage: {round(obs.get('thermal_storage_level', 0) * 100)}%")
    print(f"    Would episode end? {sim_data.get('done', False)}")

    # Verify step did NOT advance
    state_after = r("/state")
    step_after = state_after.get("step", "?")
    if step_before == step_after:
        check_pass(f"World model confirmed: step unchanged ({step_before})")
        return True
    else:
        check_warn(f"Step changed from {step_before} to {step_after}")
        return False


# ── STEP 8 — Take a Step + Check Fault System ─────────────────────────
def do_step8():
    action = {
        "hvac_power_level": 0.7,
        "thermal_charge_rate": 0.0,
        "batch_job_slot": 0,
        "load_shed_fraction": 0.0,
        "building_id": 0,
    }
    data = r("/step", "POST", [action])
    if isinstance(data, list):
        results = data
    else:
        results = data.get("results", []) if isinstance(data, dict) else []
    if not results:
        print("  Warning: /step returned unexpected format")
        return False
    obs = results[0].get("observation", {})
    step_num = obs.get("step", "?")
    faults = obs.get("active_faults", [])
    reward = round(results[0].get("reward", 0), 3)
    show_info("Step", str(step_num))
    show_info("Reward", str(reward))
    show_info("Active faults", f"{len(faults)}")
    if faults:
        for f in faults:
            print(f"    [!] FAULT: {f}")
    else:
        print("    No faults triggered yet (expected on hard difficulty)")
    return True


# ── STEP 9 — Theme 2: Task 4 Instruction Card ───────────────────────────
def do_step9():
    data = r("/reset", "POST", {"task_id": 4, "seed": 99})
    obs_list = data.get("observations", [])
    if not obs_list:
        check_warn("No observations returned")
        return False
    obs = obs_list[0]
    card = obs.get("instruction_card") or {}
    card_text = card.get("text", "")
    targets = card.get("targets", {})
    print(f"  Task 4: Instruction Following")
    print(f"  Objective: {card_text[:120]}...")
    if targets:
        print(f"  Targets:")
        for k, v in targets.items():
            print(f"    {k}: {v}")
    return bool(card_text)


# ── STEP 10 — Theme 4: /grade + Reward Decomposition ──────────────────
def do_step10():
    # First run a full episode (simplified: 10 steps)
    r("/reset", "POST", {"task_id": 3, "num_buildings": 3, "seed": 42})
    for _ in range(10):
        action = {
            "hvac_power_level": 0.6,
            "thermal_charge_rate": 0.0,
            "batch_job_slot": 0,
            "load_shed_fraction": 0.0,
            "building_id": 0,
        }
        r("/step", "POST", [action])

    grade_data = r("/grade")
    score = grade_data.get("score", "?")
    exploit = grade_data.get("exploit_detected", False)
    penalty = grade_data.get("penalty_applied", 0)
    sub = grade_data.get("sub_scores", {})
    show_info("Episode score", f"{score} (0.0-1.0 clamped)" if score != "?" else "?")
    show_info("Exploit detected", str(exploit))
    show_info("Penalty applied", str(penalty))
    if sub:
        print("  Reward components:")
        for k, v in sub.items():
            print(f"    {k}: {round(v, 3)}")
    show_info("Task ID", str(grade_data.get("task_id", "?")))
    return score not in ("?", None) and 0.0 < float(score) < 1.0


# ── Main ────────────────────────────────────────────────────────────────
def main():
    print(bold("\nGridMind-RL — Full Unified Demo"))
    print(f"  Environment: {ENV_URL}")
    print(f"  Themes: Multi-Agent | Long-Horizon | World Modeling | Self-Improvement")

    results = []

    results.append(step(1, "Health Check", "Foundation", do_step1))
    results.append(step(2, "OpenEnv /info Metadata", "Foundation", do_step2))
    results.append(step(3, "All 4 Tasks Available", "Foundation", do_step3))
    results.append(step(4, "Multi-Agent Reset (3 Buildings)", "Theme 1: Multi-Agent", do_step4))
    results.append(step(5, "Fleet-wide Feeder View", "Theme 1: Multi-Agent", do_step5))
    results.append(step(6, "Coordinator Price Signals", "Theme 1: Multi-Agent", do_step6))
    results.append(step(7, "World Modeling /simulate", "Theme 3: World Modeling", do_step7))
    results.append(step(8, "Step + Fault Events", "Wild Card: Fault Resilience", do_step8))
    results.append(step(9, "Task 4 Instruction Card", "Theme 2: Long-Horizon", do_step9))
    results.append(step(10, "Episode /grade + Reward Decomposition", "Theme 4: Self-Improvement", do_step10))

    sep = "=" * 62
    print(f"\n{bold(sep)}")
    print(f"  SUMMARY — All Themes Check")
    print(sep)
    passed = sum(1 for r in results if r)
    total = len(results)
    print(f"  Passed: {passed}/{total}")
    if passed == total:
        print(f"  [OK] ALL THEMES OPERATIONAL")
    else:
        failed = [i + 1 for i, r in enumerate(results) if not r]
        print(f"  [FAIL] Failed steps: {failed}")
    print()


if __name__ == "__main__":
    main()