Spaces:
Running
Running
Add coordinator endpoint tests and project readiness verification script
Browse files- Created `test_coordinator.py` to test `/coordinator/reset` and `/coordinator/step` endpoints, including multi-step episode functionality.
- Added `verify_readiness.py` to check essential files, directories, and key features for project readiness before submission.
Files changed (6)
- baseline_scores.json+9-9
- inference.py+218-81
- main.go+112-36
- scripts/gridmind_grpo_colab.ipynb+469-365
- test_coordinator.pyAdded+86
- verify_readiness.pyAdded+150
1234567891011121314151617181920212223{"model": "<your-active-model>","api_base": "<your-active-endpoint>","episodes_per_task": 1,"seed_base": 1000,"fast_mode": true,"llm_every": 8,"max_steps": null,"task_averages": {"3": 0.7278},"overall_average": 0.7278,"all_results": [{"task_id": 3,"seed": 1300,"total_reward": 248.19888206740697,"total_steps": 96,"elapsed_sec": 1.187589406967163,"score": 0.7278,"sub_scores": {},"exploit_detected": false}1234567891011121314151617181920212223{"model": "Qwen/Qwen2.5-7B-Instruct","api_base": "https://api-inference.huggingface.co/v1","episodes_per_task": 1,"seed_base": 1000,"fast_mode": true,"llm_every": 8,"max_steps": null,"task_averages": {"1": 0.5482},"overall_average": 0.5482,"all_results": [{"task_id": 1,"seed": 1100,"total_reward": 249.22208122816207,"total_steps": 96,"elapsed_sec": 1.4036986827850342,"score": 0.5482,"sub_scores": {},"exploit_detected": false}
162 unmodified lines1631641651661671681691701711721731 unmodified line17517617717817918018142 unmodified lines224225226227228229149 unmodified lines37938038138238338491 unmodified lines476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568165 unmodified lines734735736737738739740162 unmodified lines# ── LLM Agent ────────────────────────────────────────────────────────────────class LLMAgent:def __init__(self):self.client = get_llm_client()self.model = MODEL_NAMEself.fallback_mode = Falseself.instruction_card: Optional[dict] = None # set for task 4 episodesdef set_instruction_card(self, card: Optional[dict]) -> None:"""Store the instruction card received from reset for task 4 episodes."""1 unmodified linedef choose_action(self, obs: dict, task_id: int) -> dict:"""Prompt the LLM with current observation, return parsed action dict."""if self.fallback_mode:return self._heuristic_action(obs)task_desc = TASK_DESCRIPTIONS.get(task_id, TASK_DESCRIPTIONS[1])42 unmodified linesRespond with ONLY a JSON action:{ACTION_SCHEMA}"""for attempt in range(MAX_RETRIES):try:completion = self.client.chat.completions.create(149 unmodified linesprint(f"[ERROR] Failed to step environment: {e}", file=sys.stderr)return Nonedef simulate(self, actions: list[dict]) -> Optional[dict]:"""Predict the next state using the world modeling API without advancing the real environment."""try:91 unmodified linesif total_steps >= step_limit:breakif fast_mode:action = agent._heuristic_action(obs)else:if llm_reuse_remaining <= 0:cached_action = agent.choose_action(obs, task_id)llm_reuse_remaining = max(1, llm_every)action = cached_action# C5: World Modeling - Use /simulate when efficiency is low or faults activehvac_eff = obs.get("hvac_efficiency", 1.0)active_faults_list = obs.get("active_faults", [])use_simulation = not fast_mode and (use_planning or hvac_eff < 0.7 or len(active_faults_list) > 0)sim_result = Nonesim_reward = Noneif use_simulation:try:sim_result = env_client.simulate([action])if sim_result and "results" in sim_result and len(sim_result["results"]) > 0:sim_reward = float(sim_result["results"][0]["reward"])print(f"🔮 SIMULATE → predicted_reward={sim_reward:.4f} | committed", file=sys.stderr)except Exception as e:print(f"🔮 SIMULATE → failed ({e}), proceeding without", file=sys.stderr)# Check if simulation predicts poor reward vs running averageif sim_reward is not None and running_avg != 0.0 and sim_reward < running_avg - 0.3:# Ask LLM for alternative action with simulation warningprint(f"⚠️ SIMULATION RESULT: proposed action yields reward {sim_reward:.3f} "f"which is below your running average {running_avg:.3f}. "f"Consider reducing HVAC load or increasing load shed fraction.", file=sys.stderr)# Get a revised action from the LLMrevised_action = agent.choose_action(obs, task_id)action = revised_actionstep_resp = env_client.step(action)if step_resp is None or not isinstance(step_resp, dict) or "observation" not in step_resp:log_step(step=total_steps + 1,action="null",reward=0.0,done=True,error="invalid step response from environment",)breakif not fast_mode:llm_reuse_remaining -= 1obs = step_resp["observation"]raw_reward = float(step_resp["reward"])total_reward += raw_rewardraw_rewards.append(raw_reward)# Update running average for world model comparisonif total_steps > 0:running_avg = running_avg * 0.9 + raw_reward * 0.1if raw_reward < reward_min:reward_min = raw_rewardif raw_reward > reward_max:reward_max = raw_rewardtotal_steps += 1done = bool(step_resp.get("done", False))normalized_reward = normalize_reward(raw_reward, reward_min, reward_max)action_json = json.dumps(action, separators=(',', ':'))last_action_error = step_resp.get("last_action_error")log_step(step=total_steps,action=action_json,reward=normalized_reward,done=done,error=last_action_error,)if verbose and total_steps % 16 == 0:print(f" step={total_steps:02d} price=${obs['current_price']:.3f} "f"temp={obs['indoor_temperature']:.1f}°C "f"stress={obs['grid_stress_signal']:.2f} "f"cost=${obs['cumulative_cost']:.2f}",flush=True,file=sys.stderr,)success = bool(step_resp.get("done", False))except Exception as e:165 unmodified linesprint("Environment server not reachable.", file=sys.stderr)sys.exit(1)agent = LLMAgent()all_results: list[dict[str, Any]] = []# Determine task list: use --task if specified, otherwise all162 unmodified lines1631641651661671681691701711721731741751761771 unmodified line17918018118218318418542 unmodified lines228229230231232233234235236237149 unmodified lines38738838939039139239339439539639739839940040140291 unmodified lines494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705165 unmodified lines871872873874875876877162 unmodified lines# ── LLM Agent ────────────────────────────────────────────────────────────────class LLMAgent:def __init__(self, fast_mode: bool = False):self.client = Noneself.model = MODEL_NAMEself.fallback_mode = fast_mode # Start in fallback if fast modeself.instruction_card: Optional[dict] = None # set for task 4 episodes# Only initialize LLM client if not in fast modeif not fast_mode:self.client = get_llm_client()def set_instruction_card(self, card: Optional[dict]) -> None:"""Store the instruction card received from reset for task 4 episodes."""1 unmodified linedef choose_action(self, obs: dict, task_id: int) -> dict:"""Prompt the LLM with current observation, return parsed action dict."""if self.fallback_mode or self.client is None:return self._heuristic_action(obs)task_desc = TASK_DESCRIPTIONS.get(task_id, TASK_DESCRIPTIONS[1])42 unmodified linesRespond with ONLY a JSON action:{ACTION_SCHEMA}"""# If no client available, use heuristicif self.client is None:return self._heuristic_action(obs)for attempt in range(MAX_RETRIES):try:completion = self.client.chat.completions.create(149 unmodified linesprint(f"[ERROR] Failed to step environment: {e}", file=sys.stderr)return Nonedef coordinator_step(self, actions: list[dict]) -> Optional[dict]:"""Multi-agent step: send per-building actions to /coordinator/step."""try:r = requests.post(f"{self.base}/coordinator/step", json=actions, timeout=self.timeout)r.raise_for_status()return r.json()except Exception as e:print(f"[ERROR] Failed to coordinator step: {e}", file=sys.stderr)return Nonedef simulate(self, actions: list[dict]) -> Optional[dict]:"""Predict the next state using the world modeling API without advancing the real environment."""try:91 unmodified linesif total_steps >= step_limit:breakif coordinator:# ─────────────────────────────────────────────────────# Multi-Agent Coordinator Mode (Theme 1)# ──────────────────────────────────────���──────────────building_actions = []action_jsons = []# Get LLM action for each buildingfor bid, building_obs in enumerate(obs_list):if fast_mode:action = agent._heuristic_action(building_obs)else:if llm_reuse_remaining <= 0:action = agent.choose_action(building_obs, task_id)llm_reuse_remaining = max(1, llm_every)else:action = cached_actionaction["building_id"] = bidbuilding_actions.append(action)action_jsons.append(json.dumps(action, separators=(',', ':')))if not fast_mode:llm_reuse_remaining -= 1# Execute coordinator step with all building actionscoord_resp = env_client.coordinator_step(building_actions)if coord_resp is None or not isinstance(coord_resp, (dict, list)):log_step(step=total_steps + 1,action="null",reward=0.0,done=True,error="invalid coordinator step response",)break# Process responses from all buildings# coord_resp can be either an array directly or a dict with "responses" keyif isinstance(coord_resp, list):responses = coord_respdone = False # Will be set from responses or episode stateelse:responses = coord_resp.get("responses", [])done = bool(coord_resp.get("done", False))obs_list = []step_rewards = []for i, resp in enumerate(responses):if isinstance(resp, dict):if "observation" in resp:obs_list.append(resp["observation"])reward = float(resp.get("reward", 0.0))else:reward = 0.0step_rewards.append(reward)if not obs_list:log_step(step=total_steps + 1,action="null",reward=0.0,done=True,error="no observations in coordinator response",)breakobs = obs_list[0] # Use primary building for logging# Aggregate reward (mean of all buildings)raw_reward = sum(step_rewards) / len(step_rewards) if step_rewards else 0.0if isinstance(coord_resp, list) and len(responses) > 0:done = bool(responses[-1].get("done", False)) if isinstance(responses[-1], dict) else False# Log primary building action and aggregated rewardprimary_action_json = action_jsons[0] if action_jsons else "null"total_reward += raw_rewardraw_rewards.append(raw_reward)# Update running averageif total_steps > 0:running_avg = running_avg * 0.9 + raw_reward * 0.1if raw_reward < reward_min:reward_min = raw_rewardif raw_reward > reward_max:reward_max = raw_rewardtotal_steps += 1normalized_reward = normalize_reward(raw_reward, reward_min, reward_max)log_step(step=total_steps,action=primary_action_json,reward=normalized_reward,done=done,error=None,)if verbose and total_steps % 16 == 0:temps = [o.get('indoor_temperature', 21) for o in obs_list]costs = [o.get('cumulative_cost', 0) for o in obs_list]print(f" step={total_steps:02d} buildings={len(obs_list)} "f"temps={[f'{t:.1f}' for t in temps]} "f"costs=${sum(costs):.2f}",flush=True,file=sys.stderr,)step_resp = {"done": done}else:# ─────────────────────────────────────────────────────# Single-Building Mode (default)# ─────────────────────────────────────────────────────if fast_mode:action = agent._heuristic_action(obs)else:if llm_reuse_remaining <= 0:cached_action = agent.choose_action(obs, task_id)llm_reuse_remaining = max(1, llm_every)action = cached_action# C5: World Modeling - Use /simulate when efficiency is low or faults activehvac_eff = obs.get("hvac_efficiency", 1.0)active_faults_list = obs.get("active_faults", [])use_simulation = not fast_mode and (use_planning or hvac_eff < 0.7 or len(active_faults_list) > 0)sim_result = Nonesim_reward = Noneif use_simulation:try:sim_result = env_client.simulate([action])if sim_result and "results" in sim_result and len(sim_result["results"]) > 0:sim_reward = float(sim_result["results"][0]["reward"])print(f"🔮 SIMULATE → predicted_reward={sim_reward:.4f} | committed", file=sys.stderr)except Exception as e:print(f"🔮 SIMULATE → failed ({e}), proceeding without", file=sys.stderr)# Check if simulation predicts poor reward vs running averageif sim_reward is not None and running_avg != 0.0 and sim_reward < running_avg - 0.3:# Ask LLM for alternative action with simulation warningprint(f"⚠️ SIMULATION RESULT: proposed action yields reward {sim_reward:.3f} "f"which is below your running average {running_avg:.3f}. "f"Consider reducing HVAC load or increasing load shed fraction.", file=sys.stderr)# Get a revised action from the LLMrevised_action = agent.choose_action(obs, task_id)action = revised_actionstep_resp = env_client.step(action)if step_resp is None or not isinstance(step_resp, dict) or "observation" not in step_resp:log_step(step=total_steps + 1,action="null",reward=0.0,done=True,error="invalid step response from environment",)breakif not fast_mode:llm_reuse_remaining -= 1obs = step_resp["observation"]raw_reward = float(step_resp["reward"])total_reward += raw_rewardraw_rewards.append(raw_reward)# Update running average for world model comparisonif total_steps > 0:running_avg = running_avg * 0.9 + raw_reward * 0.1if raw_reward < reward_min:reward_min = raw_rewardif raw_reward > reward_max:reward_max = raw_rewardtotal_steps += 1done = bool(step_resp.get("done", False))normalized_reward = normalize_reward(raw_reward, reward_min, reward_max)action_json = json.dumps(action, separators=(',', ':'))last_action_error = step_resp.get("last_action_error")log_step(step=total_steps,action=action_json,reward=normalized_reward,done=done,error=last_action_error,)if verbose and total_steps % 16 == 0:print(f" step={total_steps:02d} price=${obs['current_price']:.3f} "f"temp={obs['indoor_temperature']:.1f}°C "f"stress={obs['grid_stress_signal']:.2f} "f"cost=${obs['cumulative_cost']:.2f}",flush=True,file=sys.stderr,)step_resp = {"done": done}success = bool(step_resp.get("done", False))except Exception as e:165 unmodified linesprint("Environment server not reachable.", file=sys.stderr)sys.exit(1)agent = LLMAgent(fast_mode=args.fast_mode)all_results: list[dict[str, Any]] = []# Determine task list: use --task if specified, otherwise all
148 unmodified lines149150151152153154157 unmodified lines312313314315316317193 unmodified lines511512513514515516517518519520521522523524525108 unmodified lines63463563663763863964064164264364464564652 unmodified lines69970070170270370470570670770870971071123 unmodified lines7357367377387397407417424 unmodified lines74774874975075175275375475575675775875949 unmodified lines809810811812813814815816817818819820821148 unmodified linesmux.HandleFunc("/ping", s.handlePing)mux.HandleFunc("/reset", s.handleReset)mux.HandleFunc("/step", s.handleStep)mux.HandleFunc("/state", s.handleState)mux.HandleFunc("/replay", s.handleReplay)mux.HandleFunc("/grade", s.handleGrade)157 unmodified lines}}// ── /state ───────────────────────────────────────────────────────────────────func (s *Server) handleState(w http.ResponseWriter, r *http.Request) {193 unmodified lines// ── /ws (WebSocket) ───────────────────────────────────────────────────────────type WSMessage struct {Type string `json:"type"`Data json.RawMessage `json:"data,omitempty"`Seed *int64 `json:"seed,omitempty"`TaskID int `json:"task_id,omitempty"`}type WSResetMessage struct {Seed *int64 `json:"seed,omitempty"`TaskID int `json:"task_id,omitempty"`NumBuildings int `json:"num_buildings,omitempty"`}108 unmodified lines"thermal_storage_level": obs.ThermalStorageLevel,"process_demand": obs.ProcessDemand,"current_price": obs.CurrentPrice,"grid_stress_signal": obs.GridStressSignal,"carbon_intensity": obs.CarbonIntensity,"hour_of_day": obs.HourOfDay,"batch_queue": obs.BatchQueue,"cumulative_cost": obs.CumulativeCost,"step": obs.Step,"building_id": obs.BuildingID,},"reward": nil,"done": false,52 unmodified lines"thermal_storage_level": obs.Observation.ThermalStorageLevel,"process_demand": obs.Observation.ProcessDemand,"current_price": obs.Observation.CurrentPrice,"grid_stress_signal": obs.Observation.GridStressSignal,"carbon_intensity": obs.Observation.CarbonIntensity,"hour_of_day": obs.Observation.HourOfDay,"batch_queue": obs.Observation.BatchQueue,"cumulative_cost": obs.Observation.CumulativeCost,"step": obs.Observation.Step,"building_id": obs.Observation.BuildingID,},"reward": obs.Reward,"done": done,23 unmodified lines}resp := s.envMgr.Reset(env.ResetRequest{Seed: seed,TaskID: taskID,NumBuildings: 1,})4 unmodified lines"thermal_storage_level": obs.ThermalStorageLevel,"process_demand": obs.ProcessDemand,"current_price": obs.CurrentPrice,"grid_stress_signal": obs.GridStressSignal,"carbon_intensity": obs.CarbonIntensity,"hour_of_day": obs.HourOfDay,"batch_queue": obs.BatchQueue,"cumulative_cost": obs.CumulativeCost,"step": obs.Step,"building_id": obs.BuildingID,},"reward": nil,"done": false,49 unmodified lines"thermal_storage_level": obs.Observation.ThermalStorageLevel,"process_demand": obs.Observation.ProcessDemand,"current_price": obs.Observation.CurrentPrice,"grid_stress_signal": obs.Observation.GridStressSignal,"carbon_intensity": obs.Observation.CarbonIntensity,"hour_of_day": obs.Observation.HourOfDay,"batch_queue": obs.Observation.BatchQueue,"cumulative_cost": obs.Observation.CumulativeCost,"step": obs.Observation.Step,"building_id": obs.Observation.BuildingID,},"reward": obs.Reward,"done": done,148 unmodified lines149150151152153154155156157 unmodified lines314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393193 unmodified lines587588589590591592593594595596597598599600601108 unmodified lines71071171271371471571671771871972072172252 unmodified lines77577677777877978078178278378478578678723 unmodified lines8118128138148158168178184 unmodified lines82382482582682782882983083183283383483549 unmodified lines885886887888889890891892893894895896897148 unmodified linesmux.HandleFunc("/ping", s.handlePing)mux.HandleFunc("/reset", s.handleReset)mux.HandleFunc("/step", s.handleStep)mux.HandleFunc("/coordinator/reset", s.handleCoordinatorReset)mux.HandleFunc("/coordinator/step", s.handleCoordinatorStep)mux.HandleFunc("/state", s.handleState)mux.HandleFunc("/replay", s.handleReplay)mux.HandleFunc("/grade", s.handleGrade)157 unmodified lines}}// ── /coordinator/reset ──────────────────────────────────────────────────────func (s *Server) handleCoordinatorReset(w http.ResponseWriter, r *http.Request) {if r.Method != http.MethodPost {http.Error(w, "method not allowed", http.StatusMethodNotAllowed)return}var req env.ResetRequestif err := json.NewDecoder(r.Body).Decode(&req); err != nil {// Allow empty body → defaultsreq = env.ResetRequest{TaskID: 1, NumBuildings: 3}}if req.TaskID == 0 {req.TaskID = 1}if req.NumBuildings == 0 {req.NumBuildings = 3}resp := s.envMgr.Reset(req)w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(resp)}// ── /coordinator/step ───────────────────────────────────────────────────────func (s *Server) handleCoordinatorStep(w http.ResponseWriter, r *http.Request) {if r.Method != http.MethodPost {http.Error(w, "method not allowed", http.StatusMethodNotAllowed)return}start := time.Now()// Accept array of actions (one per building)var actions []env.ActionModelbody := make([]byte, 0, 512)buf := make([]byte, 512)for {n, err := r.Body.Read(buf)body = append(body, buf[:n]...)if err != nil {break}}if err := json.Unmarshal(body, &actions); err != nil {atomic.AddInt64(&metrics.errorCount, 1)http.Error(w, "invalid action array: "+err.Error(), http.StatusBadRequest)return}// If empty array provided, use defaultsif len(actions) == 0 {actions = []env.ActionModel{{HVACPowerLevel: 0.5, BuildingID: 0}}}responses, _ := s.envMgr.Step(actions)latency := float64(time.Since(start).Microseconds()) / 1000.0for _, resp := range responses {metrics.recordStep(latency, resp.Reward)}if len(actions) > 0 {metrics.recordAction(actions[0].HVACPowerLevel)}w.Header().Set("Content-Type", "application/json")w.Header().Set("Access-Control-Allow-Origin", "*")// Always return array format for coordinatorjson.NewEncoder(w).Encode(responses)}// ── /state ───────────────────────────────────────────────────────────────────func (s *Server) handleState(w http.ResponseWriter, r *http.Request) {193 unmodified lines// ── /ws (WebSocket) ───────────────────────────────────────────────────────────type WSMessage struct {Type string `json:"type"`Data json.RawMessage `json:"data,omitempty"`Seed *int64 `json:"seed,omitempty"`TaskID int `json:"task_id,omitempty"`}type WSResetMessage struct {Seed *int64 `json:"seed,omitempty"`TaskID int `json:"task_id,omitempty"`NumBuildings int `json:"num_buildings,omitempty"`}108 unmodified lines"thermal_storage_level": obs.ThermalStorageLevel,"process_demand": obs.ProcessDemand,"current_price": obs.CurrentPrice,"grid_stress_signal": obs.GridStressSignal,"carbon_intensity": obs.CarbonIntensity,"hour_of_day": obs.HourOfDay,"batch_queue": obs.BatchQueue,"cumulative_cost": obs.CumulativeCost,"step": obs.Step,"building_id": obs.BuildingID,},"reward": nil,"done": false,52 unmodified lines"thermal_storage_level": obs.Observation.ThermalStorageLevel,"process_demand": obs.Observation.ProcessDemand,"current_price": obs.Observation.CurrentPrice,"grid_stress_signal": obs.Observation.GridStressSignal,"carbon_intensity": obs.Observation.CarbonIntensity,"hour_of_day": obs.Observation.HourOfDay,"batch_queue": obs.Observation.BatchQueue,"cumulative_cost": obs.Observation.CumulativeCost,"step": obs.Observation.Step,"building_id": obs.Observation.BuildingID,},"reward": obs.Reward,"done": done,23 unmodified lines}resp := s.envMgr.Reset(env.ResetRequest{Seed: seed,TaskID: taskID,NumBuildings: 1,})4 unmodified lines"thermal_storage_level": obs.ThermalStorageLevel,"process_demand": obs.ProcessDemand,"current_price": obs.CurrentPrice,"grid_stress_signal": obs.GridStressSignal,"carbon_intensity": obs.CarbonIntensity,"hour_of_day": obs.HourOfDay,"batch_queue": obs.BatchQueue,"cumulative_cost": obs.CumulativeCost,"step": obs.Step,"building_id": obs.BuildingID,},"reward": nil,"done": false,49 unmodified lines"thermal_storage_level": obs.Observation.ThermalStorageLevel,"process_demand": obs.Observation.ProcessDemand,"current_price": obs.Observation.CurrentPrice,"grid_stress_signal": obs.Observation.GridStressSignal,"carbon_intensity": obs.Observation.CarbonIntensity,"hour_of_day": obs.Observation.HourOfDay,"batch_queue": obs.Observation.BatchQueue,"cumulative_cost": obs.Observation.CumulativeCost,"step": obs.Observation.Step,"building_id": obs.Observation.BuildingID,},"reward": obs.Reward,"done": done,
1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215221 unmodified line"cells": [{"cell_type": "markdown","metadata": {},"source": ["# GridMind-RL: GRPO Training with Unsloth + TRL\n","\n","Fine-tunes **Qwen2.5-1.5B-Instruct** (4-bit LoRA) to control industrial building HVAC,\n","thermal storage, and batch scheduling via the live **GridMind-RL OpenEnv** environment.\n","\n","**Key fix:** This notebook uses episode-level rewards from the `/grade` endpoint —\n","not step-level rewards. This prevents mode collapse where the model\n","finds one action and repeats it forever.\n","\n","| | |\n","|---|---|\n","| **Environment** | https://lo-kyu-gridmind.hf.space |\n","| **Method** | GRPO (Group Relative Policy Optimization) |\n","| **Framework** | Unsloth 4-bit LoRA + HF TRL |\n","| **Model** | unsloth/Qwen2.5-1.5B-Instruct |\n","| **Training** | 300 steps, T4 GPU (~40 min) |\n","\n","### What the agent learns:\n","- Task 1: Charge storage off-peak, discharge at peak to minimize cost\n","- Task 2: Balance temperature comfort vs HVAC energy spend\n","- Task 3: Respond to grid stress (shed load), schedule batch jobs, minimize carbon"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["%%capture\n","!pip install unsloth requests\n","!pip install --no-deps bitsandbytes accelerate xformers peft trl triton\n","!pip install --no-deps cut_cross_entropy unsloth_zoo\n","!pip install \"datasets>=3.4.1,<4.0.0\" pandas matplotlib"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 1 — Verify the Live Environment"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import requests\n","\n","ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n","\n","print(\"Environment health:\", requests.get(f\"{ENV_URL}/health\", timeout=10).json())\n","print(\"\\nTasks available:\")\n","for t in requests.get(f\"{ENV_URL}/tasks\", timeout=10).json():\n"," print(f\" Task {t['id']}: {t['name']} ({t['difficulty']})\")\n","\n","# Quick smoke test: reset + step + grade\n","r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 1, \"seed\": 42}, timeout=30)\n","obs = r.json()[\"observations\"][0]\n","print(f\"\\nObservation keys: {list(obs.keys())}\")\n","step_r = requests.post(f\"{ENV_URL}/step\", json=[{\n"," \"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0,\n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0\n","}], timeout=30)\n","sr = step_r.json()\n","print(f\"Step reward: {sr[0]['reward']:.3f}, done: {sr[0]['done']}\")\n","grade_r = requests.get(f\"{ENV_URL}/grade\", timeout=30).json()\n","print(f\"Episode score: {grade_r['score']:.3f}\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 2 — Load Unsloth Model"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["from unsloth import FastLanguageModel\n","import torch\n","\n","max_seq_length = 512\n","lora_rank = 16\n","\n","print(\"Loading model...\")\n","model, tokenizer = FastLanguageModel.from_pretrained(\n"," model_name = \"unsloth/Qwen2.5-1.5B-Instruct\",\n"," max_seq_length = max_seq_length,\n"," load_in_4bit = True,\n",")\n","\n","model = FastLanguageModel.get_peft_model(\n"," model,\n"," r = lora_rank,\n"," target_modules = [\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n"," \"gate_proj\", \"up_proj\", \"down_proj\"],\n"," lora_alpha = lora_rank * 2,\n"," use_gradient_checkpointing = \"unsloth\",\n"," random_state = 42,\n",")\n","print(f\"Model loaded. Trainable params: {model.num_trainable_parameters():,}\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 3 — Build Diverse Training Prompts"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import json, re, random\n","\n","random.seed(42)\n","\n","SCENARIOS = [\n"," # Off-peak: cheap electricity, agent should charge storage\n"," (\"off_peak\", \"price=$0.03/kWh\", \"grid_stress=0.0\", \"Charge thermal storage now — price is cheapest today\"),\n"," (\"off_peak\", \"price=$0.04/kWh\", \"grid_stress=0.0\", \"Off-peak period. Use this time to charge storage cheaply.\"),\n"," (\"off_peak\", \"price=$0.05/kWh\", \"grid_stress=0.0\", \"Low price window. Charge storage aggressively.\"),\n"," # Mid-peak: moderate price, balance HVAC and storage\n"," (\"mid_peak\", \"price=$0.12/kWh\", \"grid_stress=0.2\", \"Mid-peak pricing. Moderate HVAC, monitor grid.\"),\n"," (\"mid_peak\", \"price=$0.10/kWh\", \"grid_stress=0.1\", \"Moderate prices. Keep HVAC at setpoint.\"),\n"," # Peak: expensive, should discharge storage if available\n"," (\"peak\", \"price=$0.28/kWh\", \"grid_stress=0.4\", \"Peak pricing! Discharge storage, reduce HVAC if comfortable.\"),\n"," (\"peak\", \"price=$0.32/kWh\", \"grid_stress=0.5\", \"CRITICAL PEAK. Minimize consumption, shed non-critical load.\"),\n"," # Grid stress: respond to demand-response signal\n"," (\"grid_stress\", \"price=$0.20/kWh\", \"grid_stress=0.8\", \"GRID EMERGENCY. Shed load immediately (load_shed_fraction > 0.3).\"),\n"," (\"grid_stress\", \"price=$0.25/kWh\", \"grid_stress=0.9\", \"CRITICAL GRID STRESS. Maximize load shedding now.\"),\n"," (\"grid_stress\", \"price=$0.18/kWh\", \"grid_stress=0.7\", \"Demand response event. Respond by shedding load.\"),\n"," # Temperature: comfort vs cost tradeoff\n"," (\"temp_hot\", \"price=$0.15/kWh\", \"grid_stress=0.0\", \"Indoor temp=25.2C (too hot). Cool down but watch cost.\"),\n"," (\"temp_cold\", \"price=$0.15/kWh\", \"grid_stress=0.0\", \"Indoor temp=18.4C (too cold). Heat but watch cost.\"),\n"," # Storage full: must discharge before charging\n"," (\"storage_full\", \"price=$0.25/kWh\", \"grid_stress=0.3\", \"Storage is 95%% full. Peak pricing — discharge storage now!\"),\n"," (\"storage_empty\", \"price=$0.03/kWh\", \"grid_stress=0.0\", \"Storage is 5%% full. Off-peak — charge storage aggressively.\"),\n"," # Batch job: schedule production work\n"," (\"batch_job\", \"price=$0.20/kWh\", \"grid_stress=0.2\", \"Batch job deadline approaching. Schedule batch_job_slot=0 (do it now).\"),\n"," (\"batch_job\", \"price=$0.03/kWh\", \"grid_stress=0.0\", \"Batch job queued. Off-peak — good time to run production.\"),\n"," # General strategy\n"," (\"general\", \"price=$0.08/kWh\", \"grid_stress=0.0\", \"Standard operation. Maintain comfort, minimize cost.\"),\n"," (\"general\", \"price=$0.15/kWh\", \"grid_stress=0.1\", \"Normal conditions. Optimize for cost within comfort bounds.\"),\n","]\n","\n","SYSTEM_PROMPT = (\"You are GridMind, an expert industrial building energy controller.\\n\"\n"," \"You control HVAC (0-1), thermal storage charge/discharge (-1 to 1), batch job scheduling (0-4),\\n\"\n"," \"and load shedding (0-0.5). Output ONLY a JSON object with these exact fields:\\n\"\n"," '{\"hvac_power_level\": float, \"thermal_charge_rate\": float, \"batch_job_slot\": int, \"load_shed_fraction\": float, \"building_id\": 0}\\n\\n\"\n"," \"Strategy rules:\\n\"\n"," \"- Charge storage (positive thermal_charge_rate) when price < $0.08/kWh\\n\"\n"," \"- Discharge storage (negative thermal_charge_rate) when price > $0.15/kWh\\n\"\n"," \"- Shed load (load_shed_fraction > 0) when grid_stress_signal > 0.7\\n\"\n"," \"- Reduce HVAC when indoor temperature is comfortable and price is high\\n\"\n"," \"- Schedule batch jobs during off-peak periods (price < $0.08)\\n\"\n"," \"- Keep indoor temperature between 19-23C\\n\"\n"," \"Never output any text — only JSON.\")\n","\n","N_PROMPTS = 300\n","dataset_rows = []\n","for i in range(N_PROMPTS):\n"," scenario_type, price_str, stress_str, instruction = random.choice(SCENARIOS)\n"," # Vary temperature\n"," if scenario_type in (\"temp_hot\",):\n"," temp_str = \"Indoor temperature=25.2C (ABOVE comfort range)\"\n"," elif scenario_type in (\"temp_cold\",):\n"," temp_str = \"Indoor temperature=18.4C (BELOW comfort range)\"\n"," else:\n"," temp_str = \"Indoor temperature=21.0C (within comfort range)\"\n"," \n"," # Vary storage\n"," if scenario_type in (\"storage_full\",):\n"," storage_str = \"Thermal storage level=95%% (FULL)\"\n"," elif scenario_type in (\"storage_empty\",):\n"," storage_str = \"Thermal storage level=5%% (NEARLY EMPTY)\"\n"," else:\n"," storage_str = \"Thermal storage level=50%%\"\n"," \n"," user_content = (\n"," f\"Building state:\\n\"\n"," f\" {temp_str}\\n\"\n",f\" {storage_str}\\n\"\n",f\" Price: {price_str} | Grid: {stress_str}\\n\"\n",f\" Instruction: {instruction}\\n\\n\"\n",f\" Output your action as JSON only.\"\n"," )\n"," \n"," dataset_rows.append({\n"," \"prompt\": [\n"," {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n"," {\"role\": \"user\", \"content\": user_content}\n"," ]\n"," \"scenario\": scenario_type,\n"," \"instruction\": instruction[:40],\n"," })\n","\n","print(f\"Generated {len(dataset_rows)} diverse training prompts\")\n","print(f\"Scenario types: {random.sample([r['scenario'] for r in dataset_rows], min(8, len(dataset_rows))]}\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 4 — Define Reward Functions\n","\n","**CRITICAL:** This notebook uses episode-level grading from `/grade`, NOT step-level rewards.\n","This prevents mode collapse (where the model finds one action and repeats it forever).\n","\n","Reward structure:\n","- `reward_json_valid`: 0.2 if output is valid JSON, else 0.0\n","- `reward_env_interaction`: 0.0-1.0 from `/grade` episode score (THE MAIN SIGNAL)\n","\n","The episode score (0.0-1.0) comes from a full 8-step rollout, grading cost,\n","temperature, grid response, carbon, and batch scheduling together.\n","This gives a rich, non-saturating signal for the model to learn from."]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["from trl import GRPOConfig, GRPOTrainer\n","from datasets import Dataset\n","\n","def reward_json_valid(completions, **kwargs):\n"," \"\"\"0.2 if output contains a valid JSON object with required fields.\"\"\"\n"," rewards = []\n"," for c in completions:\n"," text = c[0][\"content\"] if isinstance(c, list) else c\n"," try:\n"," match = re.search(r'\\{.*?\\}', text, re.DOTALL)\n"," if match:\n"," action = json.loads(match.group())\n"," required = {\"hvac_power_level\", \"thermal_charge_rate\", \"batch_job_slot\", \"load_shed_fraction\"}\n"," if required.issubset(action.keys()):\n"," rewards.append(0.2)\n"," else:\n"," rewards.append(0.0)\n"," else:\n"," rewards.append(0.0)\n"," except Exception:\n"," rewards.append(0.0)\n"," return rewards\n","\n","def reward_env_interaction(completions, **kwargs):\n"," \"\"\"Episode-level reward from /grade endpoint.\n"," \n"," Does NOT use step-level rewards — those are too noisy and saturate quickly.\n"," Instead, runs 8 steps, then calls /grade to get the true episode score (0.0-1.0).\n"," This is the PRIMARY learning signal and is non-saturating.\n"," \"\"\"\n"," rewards = []\n"," for c in completions:\n"," text = c[0][\"content\"] if isinstance(c, list) else c\n"," try:\n"," match = re.search(r'\\{.*?\\}', text, re.DOTALL)\n"," action = json.loads(match.group()) if match else {}\n"," step_action = {\n"," \"hvac_power_level\": float(max(0, min(1, action.get(\"hvac_power_level\", 0.5)))),\n"," \"thermal_charge_rate\": float(max(-1, min(1, action.get(\"thermal_charge_rate\", 0.0)))),\n"," \"batch_job_slot\": int(max(0, min(4, action.get(\"batch_job_slot\", 0)))),\n"," \"load_shed_fraction\": float(max(0, min(0.5, action.get(\"load_shed_fraction\", 0.0)))),\n"," \"building_id\": 0\n"," }\n"," \n"," # Run 8-step episode\n"," r_reset = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 2, \"seed\": 42}, timeout=30)\n"," if r_reset.status_code != 200:\n"," rewards.append(0.0)\n"," continue\n"," \n"," for _ in range(8):\n"," r_step = requests.post(f\"{ENV_URL}/step\", json=[step_action], timeout=30)\n"," if r_step.status_code != 200:\n"," break\n"," \n"," # Get episode-level score from /grade — this is the real signal\n"," r_grade = requests.get(f\"{ENV_URL}/grade\", timeout=30)\n"," if r_grade.status_code == 200:\n"," episode_score = float(r_grade.json().get(\"score\", 0.5))\n"," rewards.append(episode_score) # 0.0 to 1.0\n"," else:\n"," rewards.append(0.0)\n"," \n"," except Exception as e:\n"," rewards.append(0.0)\n"," return rewards\n","\n","print(\"Reward functions defined:\")\n","print(\" reward_json_valid: 0.0-0.2 (JSON format check)\")\n","print(\" reward_env_interaction: 0.0-1.0 (EPISODE SCORE from /grade — PRIMARY SIGNAL)\")\n","print(\" Total range: 0.0-1.2 (non-saturating)\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 5 — GRPO Training (300 steps)"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import os\n","os.makedirs(\"results\", exist_ok=True)\n","\n","dataset = Dataset.from_dict({\n"," \"prompt\": [{\"role\": r[\"prompt\"][0][\"role\"], \"content\": r[\"prompt\"][0][\"content\"]} \n"," for r in dataset_rows]\n","})\n","# Add user turns properly\n","dataset = dataset.add_column(\"prompt\", [r[\"prompt\"] for r in dataset_rows])\n","\n","training_args = GRPOConfig(\n"," output_dir = \"gridmind-grpo-results\",\n"," num_train_epochs = 1,\n"," per_device_train_batch_size = 1,\n"," gradient_accumulation_steps = 4,\n"," num_generations = 4,\n"," max_prompt_length = 256,\n"," max_completion_length = 128,\n"," learning_rate = 5e-6,\n"," lr_scheduler_type = \"cosine\",\n"," warmup_ratio = 0.1,\n"," logging_steps = 5,\n"," save_steps = 100,\n"," fp16 = True,\n"," report_to = \"none\",\n"," seed = 42,\n",")\n","\n","trainer = GRPOTrainer(\n"," model = model,\n"," tokenizer = tokenizer,\n"," args = training_args,\n"," train_dataset = dataset,\n"," reward_funcs = [reward_json_valid, reward_env_interaction],\n",")\n","\n","print(f\"Starting GRPO training ({N_PROMPTS} prompts, 1 epoch)...\")\n","print(f\"Expected time on T4: ~35-45 minutes\\n\")\n","trainer.train()\n","trainer.save_model(\"gridmind-grpo-results/final\")\n","print(\"Training complete!\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 6 — Plot Training Curves"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import pandas as pd\n","import matplotlib.pyplot as plt\n","\n","# Load training log\n","try:\n"," df = pd.read_csv(\"gridmind-grpo-results/training_log.csv\")\n","except:\n"," print(\"No CSV found — checking trainer state...\")\n"," import glob\n"," csvs = glob.glob(\"**/training_log.csv\")\n"," if csvs:\n"," df = pd.read_csv(csvs[0])\n"," else:\n"," print(\"No training log CSV. Training may still be in progress.\")\n"," df = None\n","\n","if df is not None and len(df) > 0:\n"," plt.style.use('dark_background')\n"," fig, axes = plt.subplots(1, 2, figsize=(14, 5))\n"," \n"," # Plot episode score\n"," if 'rewards/reward_env_interaction/mean' in df.columns:\n"," col = 'rewards/reward_env_interaction/mean'\n"," smooth = df[col].rolling(window=5, min_periods=1).mean()\n"," axes[0].plot(df['step'], df[col], alpha=0.3, color='#4ECDC4', label='Raw')\n"," axes[0].plot(df['step'], smooth, color='#4ECDC4', linewidth=2, label='Smoothed (5)')\n"," axes[0].axhline(y=0.5, color='#FFE66D', linestyle='--', alpha=0.7, label='Heuristic baseline (0.5)')\n"," axes[0].set_xlabel('Training Step')\n"," axes[0].set_ylabel('Episode Score (0.0-1.0)')\n"," axes[0].set_title('Episode Score (from /grade endpoint)')\n"," axes[0].legend()\n"," axes[0].grid(True, alpha=0.3)\n"," axes[0].set_ylim(0, 1.05)\n"," \n"," # Plot JSON validity\n"," if 'rewards/reward_json_valid/mean' in df.columns:\n"," col = 'rewards/reward_json_valid/mean'\n"," smooth = df[col].rolling(window=5, min_periods=1).mean()\n"," axes[1].plot(df['step'], df[col], alpha=0.3, color='#FF6B6B', label='Raw')\n"," axes[1].plot(df['step'], smooth, color='#FF6B6B', linewidth=2, label='Smoothed (5)')\n"," axes[1].set_xlabel('Training Step')\n"," axes[1].set_ylabel('JSON Validity (0.0-0.2)')\n"," axes[1].set_title('JSON Format Compliance')\n"," axes[1].legend()\n"," axes[1].grid(True, alpha=0.3)\n"," axes[1].set_ylim(0, 0.25)\n"," \n"," plt.tight_layout()\n"," plt.savefig(\"results/training_curve.png\", dpi=200, bbox_inches='tight')\n"," plt.show()\n"," print(\"\\nTraining curve saved to results/training_curve.png\")\n","else:\n"," print(\"No training data to plot yet.\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 7 — Before vs After Comparison"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["# Test scenario: peak pricing + grid stress (hardest scenario)\n","test_scenarios = [\n"," (\"CRITICAL GRID STRESS\",\n"," \"Indoor temp=24.5C | Storage=70%% full | Price=$0.28/kWh | Grid stress=0.85 | Hour=18 (peak)\"),\n"," (\"OFF-PEAK CHARGE\",\n"," \"Indoor temp=21.0C | Storage=20%% full | Price=$0.03/kWh | Grid stress=0.0 | Hour=3 (off-peak)\"),\n"," (\"TEMPERATURE HOT\",\n"," \"Indoor temp=25.3C | Storage=50%% | Price=$0.15/kWh | Grid stress=0.2 | Hour=14\"),\n","]\n","\n","FastLanguageModel.for_inference(model)\n","\n","for name, state in test_scenarios:\n"," messages = [\n"," {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n"," {\"role\": \"user\", \"content\": f\"Building state: {state}\\nOutput your action as JSON only.\"}\n"," ]\n"," inputs = tokenizer.apply_chat_template(\n"," messages, tokenize=True, add_generation_prompt=True, return_tensors=\"pt\"\n"," ).to(\"cuda\")\n"," \n"," with torch.no_grad():\n"," outputs = model.generate(\n"," inputs, max_new_tokens=100, temperature=0.1,\n"," do_sample=True, pad_token_id=tokenizer.eos_token_id\n"," )\n"," \n"," response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)\n"," print(f\"=== {name} ===\")\n"," print(f\" State: {state}\")\n"," try:\n"," match = re.search(r'\\{.*?\\}', response, re.DOTALL)\n"," if match:\n"," action = json.loads(match.group())\n"," print(f\" Action: hvac={action.get('hvac_power_level')}, \"\n"," f\"thermal={action.get('thermal_charge_rate')}, \"\n"," f\"batch={action.get('batch_job_slot')}, \"\n"," f\"shed={action.get('load_shed_fraction')}\")\n"," # Check if action makes sense\n"," if \"GRID STRESS\" in name:\n"," if action.get(\"load_shed_fraction\", 0) > 0.2:\n"," print(\" [CORRECT] Load shedding on grid stress\")\n"," else:\n"," print(\" [WARNING] Should shed more load during grid stress!\")\n"," if \"OFF-PEAK\" in name:\n"," if action.get(\"thermal_charge_rate\", 0) > 0.0:\n"," print(\" [CORRECT] Charging storage during off-peak\")\n"," else:\n"," print(\" [WARNING] Should charge storage during off-peak!\")\n"," else:\n"," print(f\" Raw response: {response[:100]}\")\n"," except:\n"," print(f\" Response: {response[:200]}\")\n"," print()"]}],"metadata": {"kernelspec": {"display_name": "Python 3","language": "python","name": "python3"},"language_info": {"name": "python","version": "3.11.4"}},"nbformat": 4,"nbformat_minor": 4}1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256261 unmodified line"cells": [{"cell_type": "markdown","id": "193da661","metadata": {},"source": ["# GridMind-RL: GRPO Training for Industrial Energy Management\n","\n","**Meta PyTorch OpenEnv Hackathon — GridMind-RL Team**\n","\n","This notebook trains a small LLM (Qwen2.5-1.5B) using TRL GRPO on the GridMind-RL environment.\n","The environment covers all 4 hackathon themes:\n","\n","1. **Theme 1: Multi-Agent** — 3 buildings share a grid feeder; each agent makes independent decisions\n","2. **Theme 2: Instruction Following** — Task 4 provides natural language objectives that must be satisfied\n","3. **Theme 3: World Modeling** — `/simulate` endpoint predicts outcomes before committing actions\n","4. **Theme 4: Self-Improvement** — Curriculum automatically advances difficulty as agent performance improves\n","\n","| | |\n","|---|---|\n","| **Environment** | https://lo-kyu-gridmind.hf.space |\n","| **Method** | GRPO (Group Relative Policy Optimization) |\n","| **Model** | Qwen2.5-1.5B-Instruct |\n","| **Training Time** | ~30-40 minutes on free Colab T4 GPU |\n","| **Expected Improvement** | 20-40% score gain over heuristic baseline |"]},{"cell_type": "code","execution_count": null,"id": "f28e2f2c","metadata": {},"outputs": [],"source": ["# Install dependencies\n","!pip install trl==0.8.6 transformers==4.40.0 torch accelerate datasets requests -q\n","\n","import torch\n","import sys\n","\n","print(f\"PyTorch: {torch.__version__}\")\n","print(f\"CUDA available: {torch.cuda.is_available()}\")\n","if torch.cuda.is_available():\n"," print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n"," print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")"]},{"cell_type": "markdown","id": "5021a299","metadata": {},"source": ["## Step 1: Connect to Environment and Verify Connectivity"]},{"cell_type": "code","execution_count": null,"id": "4cdf0f35","metadata": {},"outputs": [],"source": ["import requests\n","import json\n","import time\n","\n","ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n","\n","# Test connectivity\n","print(\"Testing environment connectivity...\")\n","try:\n"," health = requests.get(f\"{ENV_URL}/health\", timeout=10).json()\n"," print(f\"✓ Health check: {health}\")\n","except Exception as e:\n"," print(f\"✗ Health check failed: {e}\")\n"," sys.exit(1)\n","\n","# Test each task reset\n","print(\"\\nTesting all 4 tasks...\")\n","for task_id in [1, 2, 3, 4]:\n"," try:\n"," r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n"," obs = r.json()\n"," has_card = \"instruction_card\" in obs or \"observations\" in obs and obs[\"observations\"][0].get(\"instruction_card\")\n"," print(f\"✓ Task {task_id}: status={r.status_code}, has_instruction_card={has_card}\")\n"," except Exception as e:\n"," print(f\"✗ Task {task_id} failed: {e}\")\n","\n","# Test coordinator (multi-agent)\n","print(\"\\nTesting multi-agent coordinator...\")\n","try:\n"," r = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10)\n"," obs = r.json()\n"," n_buildings = len(obs.get(\"observations\", []))\n"," print(f\"✓ Coordinator reset: {n_buildings} buildings\")\n","except Exception as e:\n"," print(f\"✗ Coordinator failed: {e}\")\n","\n","# Test world modeling\n","print(\"\\nTesting world modeling (/simulate)...\")\n","try:\n"," r = requests.post(f\"{ENV_URL}/simulate\", \n"," json=[{\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n"," timeout=10)\n"," sim = r.json()\n"," has_results = \"results\" in sim\n"," print(f\"✓ Simulate: has_results={has_results}\")\n","except Exception as e:\n"," print(f\"✗ Simulate failed: {e}\")\n","\n","print(\"\\n✓ All connectivity checks passed!\")"]},{"cell_type": "markdown","id": "4a5b58c2","metadata": {},"source": ["## Step 2: Measure Baseline Performance (Before Training)"]},{"cell_type": "code","execution_count": null,"id": "42cecadb","metadata": {},"outputs": [],"source": ["import random\n","\n","def run_heuristic_episode(task_id=1, max_steps=96):\n"," \"\"\"Run an episode using a rule-based heuristic policy.\"\"\"\n"," try:\n"," r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n"," obs_data = r.json()\n"," obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n"," except:\n"," return 0.0\n"," \n"," for step in range(max_steps):\n"," # Simple heuristic: charge off-peak, discharge peak\n"," hour = step // 4\n"," hvac = 0.7 if 8 <= hour <= 18 else 0.3\n"," charge = 0.6 if hour < 6 else (-0.4 if 14 <= hour <= 18 else 0.0)\n"," shed = 0.3 if 14 <= hour <= 17 else 0.0\n"," \n"," action = {\n"," \"hvac_power_level\": hvac,\n"," \"thermal_charge_rate\": charge,\n"," \"batch_job_slot\": 1 if 22 <= hour or hour <= 5 else 0,\n"," \"load_shed_fraction\": shed,\n"," \"building_id\": 0\n"," }\n"," \n"," try:\n"," r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n"," step_data = r.json()\n"," if isinstance(step_data, list):\n"," step_data = step_data[0]\n"," obs = step_data.get(\"observation\", obs)\n"," if step_data.get(\"done\", False):\n"," break\n"," except:\n"," break\n"," \n"," # Get final grade\n"," try:\n"," grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n"," return float(grade.get(\"score\", 0))\n"," except:\n"," return 0.0\n","\n","print(\"Measuring heuristic baseline (2 episodes per task)...\")\n","baseline_scores = {}\n","for task_id in [1, 2, 3, 4]:\n"," scores = []\n"," for ep in range(2):\n"," score = run_heuristic_episode(task_id=task_id)\n"," scores.append(score)\n"," print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n"," baseline_scores[task_id] = sum(scores) / len(scores)\n","\n","print(f\"\\nHeuristic Baseline Averages:\")\n","for task_id, avg in baseline_scores.items():\n"," print(f\" Task {task_id}: {avg:.3f}\")\n","print(f\" Overall: {sum(baseline_scores.values()) / len(baseline_scores):.3f}\")"]},{"cell_type": "markdown","id": "7abdd330","metadata": {},"source": ["## Step 3: Build Multi-Theme Training Dataset"]},{"cell_type": "code","execution_count": null,"id": "1c496af9","metadata": {},"outputs": [],"source": ["# Build a dataset that covers all 4 themes\n","dataset = []\n","\n","# Theme 1: Multi-Agent (3 buildings cooperating)\n","print(\"Building multi-agent theme examples...\")\n","for i in range(20):\n"," try:\n"," resp = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10).json()\n"," if \"observations\" in resp:\n"," for b_idx, b_obs in enumerate(resp[\"observations\"]):\n"," prompt = f\"\"\"You control Building {b_idx} in a 3-building facility.\n","All buildings share one grid connection (feeder limit: 250 kW).\n","Your current state: temp={b_obs.get('indoor_temperature', 21):.1f}°C, \n","storage={b_obs.get('thermal_storage_level', 0.5):.2f}, \n","price=${b_obs.get('current_price', 0.1):.3f}/kWh\n","Grid stress signal: {b_obs.get('grid_stress_signal', 0):.2f}\n","\n","You must coordinate with other buildings to keep total feeder load under 250 kW.\n","Each building decides independently. Respond with your JSON action:\n","{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n","\"load_shed_fraction\": <0-0.5>, \"building_id\": {b_idx}}}\"\"\"\n"," dataset.append({\"prompt\": prompt, \"theme\": \"multi_agent\"})\n"," except:\n"," pass\n","\n","print(f\"Multi-agent examples: {len([d for d in dataset if d.get('theme')=='multi_agent'])}\")\n","\n","# Theme 2: Instruction Following (Task 4 with explicit objectives)\n","print(\"Building instruction-following theme examples...\")\n","for i in range(20):\n"," try:\n"," resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 4}, timeout=10).json()\n"," if \"observations\" in resp:\n"," obs = resp[\"observations\"][0]\n"," instruction = resp.get(\"instruction_card\", obs.get(\"instruction_card\", {}))\n"," instruction_text = instruction.get(\"text\", \"Minimize cost\") if isinstance(instruction, dict) else str(instruction)\n"," prompt = f\"\"\"INSTRUCTION CARD: {instruction_text}\n","\n","Current state: temp={obs.get('indoor_temperature', 21):.1f}°C, \n","storage={obs.get('thermal_storage_level', 0.5):.2f}, \n","cost_so_far=${obs.get('cumulative_cost', 0):.2f}, \n","step={obs.get('step', 0)}/96\n","\n","You MUST satisfy the instruction. Output JSON action:\n","{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n","\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n"," dataset.append({\"prompt\": prompt, \"theme\": \"instruction_following\"})\n"," except:\n"," pass\n","\n","print(f\"Instruction-following examples: {len([d for d in dataset if d.get('theme')=='instruction_following'])}\")\n","\n","# Theme 3: World Modeling (use /simulate)\n","print(\"Building world-modeling theme examples...\")\n","for task_id in [1, 2]:\n"," for i in range(10):\n"," try:\n"," resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10).json()\n"," if \"observations\" in resp:\n"," obs = resp[\"observations\"][0]\n"," # Simulate 2 candidate actions\n"," try:\n"," sim_a = requests.post(f\"{ENV_URL}/simulate\",\n"," json=[{\"hvac_power_level\": 0.8, \"thermal_charge_rate\": 0.3,\n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n"," timeout=10).json()\n"," sim_b = requests.post(f\"{ENV_URL}/simulate\",\n"," json=[{\"hvac_power_level\": 0.3, \"thermal_charge_rate\": -0.2,\n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.2, \"building_id\": 0}],\n"," timeout=10).json()\n"," sim_context = \"\\nPredicted outcomes:\\nOption A (high HVAC): efficient\\nOption B (low HVAC): economical\"\n"," except:\n"," sim_context = \"\"\n"," \n"," prompt = f\"\"\"Plan your actions using simulation of future outcomes.\n","State: temp={obs.get('indoor_temperature', 21):.1f}°C, storage={obs.get('thermal_storage_level', 0.5):.2f}{sim_context}\n","\n","Output your best JSON action:\n","{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n","\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n"," dataset.append({\"prompt\": prompt, \"theme\": \"world_modeling\"})\n"," except:\n"," pass\n","\n","print(f\"World-modeling examples: {len([d for d in dataset if d.get('theme')=='world_modeling'])}\")\n","\n","# Theme 4: Self-Improvement (curriculum across difficulties)\n","print(\"Building self-improvement theme examples...\")\n","for difficulty in [1, 1, 2, 2, 3, 3]:\n"," try:\n"," resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": difficulty}, timeout=10).json()\n"," if \"observations\" in resp:\n"," obs = resp[\"observations\"][0]\n"," prompt = f\"\"\"Difficulty Level {difficulty}/3 - Control building energy system.\n","State: temp={obs.get('indoor_temperature', 21):.1f}°C, storage={obs.get('thermal_storage_level', 0.5):.2f},\n","price=${obs.get('current_price', 0.1):.3f}/kWh\n","\n","Output JSON action:\n","{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n","\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n"," dataset.append({\"prompt\": prompt, \"theme\": \"curriculum\", \"difficulty\": difficulty})\n"," except:\n"," pass\n","\n","print(f\"Self-improvement examples: {len([d for d in dataset if d.get('theme')=='curriculum'])}\")\n","\n","print(f\"\\nTotal dataset: {len(dataset)} prompts\")\n","theme_counts = {}\n","for d in dataset:\n"," theme = d.get(\"theme\", \"unknown\")\n"," theme_counts[theme] = theme_counts.get(theme, 0) + 1\n","print(f\"Theme distribution: {theme_counts}\")"]},{"cell_type": "markdown","id": "2ed46c06","metadata": {},"source": ["## Step 4: Load Model and Tokenizer"]},{"cell_type": "code","execution_count": null,"id": "5e5826e4","metadata": {},"outputs": [],"source": ["from transformers import AutoTokenizer, AutoModelForCausalLM\n","\n","MODEL_NAME = \"Qwen/Qwen2.5-1.5B-Instruct\"\n","print(f\"Loading {MODEL_NAME}...\")\n","\n","tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n","if tokenizer.pad_token is None:\n"," tokenizer.pad_token = tokenizer.eos_token\n","\n","model = AutoModelForCausalLM.from_pretrained(\n"," MODEL_NAME,\n"," torch_dtype=torch.float16,\n"," device_map=\"cuda\" if torch.cuda.is_available() else \"cpu\"\n",")\n","\n","total_params = sum(p.numel() for p in model.parameters())\n","print(f\"Model loaded. Parameters: {total_params/1e6:.0f}M\")\n","print(f\"Device: {next(model.parameters()).device}\")"]},{"cell_type": "markdown","id": "ba6645a6","metadata": {},"source": ["## Step 5: Define Reward Function"]},{"cell_type": "code","execution_count": null,"id": "02686008","metadata": {},"outputs": [],"source": ["import json as _json\n","\n","training_rewards = []\n","\n","def gridmind_reward_fn(completions, **kwargs):\n"," \"\"\"Reward function that calls the real environment.\"\"\"\n"," rewards = []\n"," \n"," for completion in completions:\n"," try:\n"," # Extract JSON action from completion\n"," text = str(completion).strip()\n"," start = text.rfind('{')\n"," end = text.rfind('}') + 1\n"," if start < 0 or end <= start:\n"," rewards.append(-1.0)\n"," continue\n"," \n"," action_str = text[start:end]\n"," action = _json.loads(action_str)\n"," \n"," # Clamp action to valid ranges\n"," action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n"," action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n"," action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n"," action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n"," action[\"building_id\"] = int(action.get(\"building_id\", 0))\n"," \n"," # Call environment\n"," r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n"," if r.status_code != 200:\n"," rewards.append(-0.5)\n"," continue\n"," \n"," step_data = r.json()\n"," if isinstance(step_data, list):\n"," step_data = step_data[0]\n"," \n"," reward = float(step_data.get(\"reward\", 0))\n"," rewards.append(max(-1.0, min(1.0, reward))) # Clamp to [-1, 1]\n"," training_rewards.append(reward)\n"," \n"," except Exception as e:\n"," rewards.append(-1.0)\n"," \n"," return rewards\n","\n","print(\"Reward function defined.\")"]},{"cell_type": "markdown","id": "adae3837","metadata": {},"source": ["## Step 6: Configure and Run GRPO Training"]},{"cell_type": "code","execution_count": null,"id": "ceac8c9d","metadata": {},"outputs": [],"source": ["from trl import GRPOTrainer, GRPOConfig\n","from datasets import Dataset\n","\n","# Prepare dataset\n","train_data = [{\"prompt\": d[\"prompt\"]} for d in dataset]\n","train_ds = Dataset.from_list(train_data)\n","\n","print(f\"Training dataset: {len(train_ds)} prompts\")\n","print(f\"Sample prompt:\\n{train_data[0]['prompt'][:200]}...\\n\")\n","\n","# GRPO config for free T4 GPU\n","config = GRPOConfig(\n"," output_dir=\"./gridmind-grpo-output\",\n"," num_train_epochs=1,\n"," max_steps=60, # Complete in ~30-40 min on T4\n"," per_device_train_batch_size=2,\n"," gradient_accumulation_steps=2,\n"," max_new_tokens=100,\n"," max_prompt_length=512,\n"," learning_rate=5e-6,\n"," logging_steps=5,\n"," save_steps=60,\n"," fp16=True,\n"," dataloader_num_workers=0,\n"," report_to=\"none\",\n"," num_generations=2, # 2 generations per prompt for speed\n",")\n","\n","print(\"\\nStarting GRPO training...\")\n","print(f\"Estimated time: 30-40 minutes on Colab T4 GPU\")\n","print(f\"Steps: {config.max_steps}, Batch size: {config.per_device_train_batch_size * config.gradient_accumulation_steps}\\n\")\n","\n","# Initialize trainer\n","trainer = GRPOTrainer(\n"," model=model,\n"," tokenizer=tokenizer,\n"," config=config,\n"," train_dataset=train_ds,\n"," reward_funcs=gridmind_reward_fn,\n",")\n","\n","# Train\n","trainer.train()\n","print(\"\\n✓ Training complete!\")"]},{"cell_type": "markdown","id": "c145c8c6","metadata": {},"source": ["## Step 7: Evaluate Trained Model"]},{"cell_type": "code","execution_count": null,"id": "dac005cc","metadata": {},"outputs": [],"source": ["def run_llm_episode(task_id=1, max_steps=96):\n"," \"\"\"Run an episode using the trained LLM.\"\"\"\n"," try:\n"," r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n"," obs_data = r.json()\n"," obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n"," except:\n"," return 0.0\n"," \n"," model.eval()\n"," \n"," for step in range(max_steps):\n"," prompt = f\"\"\"Control industrial building energy system.\n","State: temp={obs.get('indoor_temperature', 21):.1f}°C, storage={obs.get('thermal_storage_level', 0.5):.2f}\n","Output JSON action (hvac_power_level 0-1, thermal_charge_rate -1 to 1, batch_job_slot 0-4,\n","load_shed_fraction 0-0.5, building_id 0):\"\"\"\n"," \n"," try:\n"," inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=400).to(model.device)\n"," with torch.no_grad():\n"," outputs = model.generate(**inputs, max_new_tokens=80, do_sample=False, pad_token_id=tokenizer.eos_token_id)\n"," generated = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)\n"," \n"," start = generated.rfind('{')\n"," end = generated.rfind('}') + 1\n"," if start >= 0 and end > start:\n"," action = _json.loads(generated[start:end])\n"," action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n"," action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n"," action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n"," action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n"," action[\"building_id\"] = 0\n"," else:\n"," action = {\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \"batch_job_slot\": 0,\n"," \"load_shed_fraction\": 0.0, \"building_id\": 0}\n"," \n"," r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n"," step_data = r.json()\n"," if isinstance(step_data, list):\n"," step_data = step_data[0]\n"," obs = step_data.get(\"observation\", obs)\n"," if step_data.get(\"done\", False):\n"," break\n"," except:\n"," break\n"," \n"," try:\n"," grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n"," return float(grade.get(\"score\", 0))\n"," except:\n"," return 0.0\n","\n","print(\"Evaluating trained model (2 episodes per task)...\")\n","trained_scores = {}\n","for task_id in [1, 2, 3, 4]:\n"," scores = []\n"," for ep in range(2):\n"," score = run_llm_episode(task_id=task_id)\n"," scores.append(score)\n"," print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n"," trained_scores[task_id] = sum(scores) / len(scores)\n","\n","print(f\"\\nTrained Model Scores:\")\n","for task_id, avg in trained_scores.items():\n"," baseline = baseline_scores[task_id]\n"," improvement = ((avg - baseline) / baseline * 100) if baseline > 0 else 0\n"," print(f\" Task {task_id}: {avg:.3f} (baseline: {baseline:.3f}, {improvement:+.1f}%)\")\n","\n","trained_avg = sum(trained_scores.values()) / len(trained_scores)\n","baseline_avg = sum(baseline_scores.values()) / len(baseline_scores)\n","overall_improvement = ((trained_avg - baseline_avg) / baseline_avg * 100) if baseline_avg > 0 else 0\n","\n","print(f\"\\nOverall Scores:\")\n","print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n","print(f\" Trained LLM: {trained_avg:.3f}\")\n","print(f\" Improvement: {overall_improvement:+.1f}%\")"]},{"cell_type": "markdown","id": "0f955e71","metadata": {},"source": ["## Step 8: Save Results"]},{"cell_type": "code","execution_count": null,"id": "00844cb1","metadata": {},"outputs": [],"source": ["results = {\n"," \"heuristic_baseline\": {\n"," \"scores_by_task\": {str(k): v for k, v in baseline_scores.items()},\n"," \"average\": baseline_avg\n"," },\n"," \"trained_llm\": {\n"," \"scores_by_task\": {str(k): v for k, v in trained_scores.items()},\n"," \"average\": trained_avg\n"," },\n"," \"improvement_percent\": overall_improvement,\n"," \"model\": MODEL_NAME,\n"," \"training_steps\": config.max_steps,\n"," \"themes_covered\": [\"multi_agent\", \"instruction_following\", \"world_modeling\", \"curriculum\"],\n"," \"training_rewards_log\": training_rewards[-20:] if training_rewards else [],\n","}\n","\n","print(\"Saving results...\")\n","with open(\"gridmind_training_results.json\", \"w\") as f:\n"," _json.dump(results, f, indent=2)\n","\n","print(\"✓ Results saved to gridmind_training_results.json\")\n","print(f\"\\nSummary:\")\n","print(f\" Model: {MODEL_NAME}\")\n","print(f\" Themes: {results['themes_covered']}\")\n","print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n","print(f\" Trained LLM: {trained_avg:.3f}\")\n","print(f\" Improvement: {overall_improvement:+.1f}%\")"]}],"metadata": {"language_info": {"name": "python"}},"nbformat": 4,"nbformat_minor": 5}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#!/usr/bin/env python3"""Quick test of coordinator endpoints."""
import requestsimport json
ENV_URL = "http://localhost:7860"
print("=" * 60)print("COORDINATOR ENDPOINT TEST")print("=" * 60)
# Test coordinator resetprint("\n1. Testing /coordinator/reset...")try: r = requests.post(f"{ENV_URL}/coordinator/reset", json={}, timeout=10) print(f" Status: {r.status_code}") resp = r.json() obs_list = resp.get("observations", []) print(f" Observations count: {len(obs_list)}") if obs_list: print(f" First observation keys: {list(obs_list[0].keys())[:5]}") print(f" First building temp: {obs_list[0].get('indoor_temperature', 'N/A')}°C")except Exception as e: print(f" ERROR: {e}")
# Test coordinator stepprint("\n2. Testing /coordinator/step...")actions = [ {"hvac_power_level": 0.5, "thermal_charge_rate": 0.0, "batch_job_slot": 0, "load_shed_fraction": 0.0, "building_id": 0}, {"hvac_power_level": 0.6, "thermal_charge_rate": 0.1, "batch_job_slot": 1, "load_shed_fraction": 0.1, "building_id": 1}, {"hvac_power_level": 0.4, "thermal_charge_rate": -0.2, "batch_job_slot": 2, "load_shed_fraction": 0.0, "building_id": 2},]try: r = requests.post(f"{ENV_URL}/coordinator/step", json=actions, timeout=10) print(f" Status: {r.status_code}") resp = r.json() responses = resp.get("responses", []) print(f" Responses count: {len(responses)}") done = resp.get("done", False) print(f" Episode done: {done}") if responses: for i, sr in enumerate(responses): reward = sr.get("reward", 0.0) obs = sr.get("observation", {}) temp = obs.get("indoor_temperature", "N/A") print(f" Building {i}: reward={reward:.4f}, temp={temp}°C")except Exception as e: print(f" ERROR: {e}")
# Test several steps to verify stateful behaviorprint("\n3. Testing multi-step coordinator episode...")try: # Reset r = requests.post(f"{ENV_URL}/coordinator/reset", json={}, timeout=10) resp = r.json() obs_list = resp.get("observations", []) print(f" Reset: {len(obs_list)} buildings") # Take 3 steps for step_num in range(3): actions = [ {"hvac_power_level": 0.5, "thermal_charge_rate": 0.0, "batch_job_slot": 0, "load_shed_fraction": 0.0, "building_id": i} for i in range(len(obs_list)) ] r = requests.post(f"{ENV_URL}/coordinator/step", json=actions, timeout=10) resp = r.json() responses = resp.get("responses", []) rewards = [sr.get("reward", 0.0) for sr in responses] avg_reward = sum(rewards) / len(rewards) if rewards else 0.0 done = resp.get("done", False) print(f" Step {step_num+1}: avg_reward={avg_reward:.4f}, done={done}") # Update obs for next iteration obs_list = [sr.get("observation", {}) for sr in responses] if done: print(f" Episode completed at step {step_num+1}") breakexcept Exception as e: print(f" ERROR: {e}")
print("\n" + "=" * 60)print("✓ Coordinator endpoint test complete!")print("=" * 60)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150#!/usr/bin/env python3"""Final project readiness verification."""
import jsonimport osimport subprocessimport sysfrom pathlib import Path
GRIDMIND_ROOT = Path(".")
def check_file_exists(path: str, description: str) -> bool: """Check if a file exists.""" exists = os.path.exists(path) status = "✓" if exists else "✗" print(f" {status} {description:<50} ({path})") return exists
def check_directory_exists(path: str, description: str) -> bool: """Check if a directory exists.""" exists = os.path.isdir(path) status = "✓" if exists else "✗" print(f" {status} {description:<50} ({path})") return exists
def check_file_size(path: str, min_bytes: int, description: str) -> bool: """Check if a file exists and is above minimum size.""" if not os.path.exists(path): print(f" ✗ {description:<50} (not found)") return False size = os.path.getsize(path) ok = size >= min_bytes status = "✓" if ok else "✗" print(f" {status} {description:<50} ({size} bytes, min {min_bytes})") return ok
print("=" * 70)print("GridMind-RL PROJECT READINESS CHECK")print("=" * 70)
all_ok = True
# 1. Essential Filesprint("\n1. ESSENTIAL FILES")all_ok &= check_file_exists("main.go", "Go server main file")all_ok &= check_file_exists("inference.py", "Python inference script")all_ok &= check_file_exists("go.mod", "Go module file")all_ok &= check_file_exists("go.sum", "Go dependencies")
# 2. Environment Moduleprint("\n2. ENVIRONMENT PACKAGE")all_ok &= check_directory_exists("env", "Environment package directory")all_ok &= check_file_exists("env/environment.go", "Main environment logic")all_ok &= check_file_exists("env/models.go", "Data models")all_ok &= check_file_exists("env/rewards.go", "Reward computation")all_ok &= check_file_exists("env/faults.go", "Fault system")all_ok &= check_file_exists("env/tasks.go", "Task definitions")
# 3. Python Moduleprint("\n3. PYTHON PACKAGE")all_ok &= check_directory_exists("python", "Python package directory")all_ok &= check_file_exists("python/__init__.py", "Python package init")all_ok &= check_file_exists("python/models.py", "Python models")all_ok &= check_file_size("python/requirements.txt", 100, "Python requirements")
# 4. Notebooksprint("\n4. NOTEBOOKS")all_ok &= check_file_size("scripts/gridmind_grpo_colab.ipynb", 20000, "Colab notebook (≥20KB)")
# 5. Dashboardprint("\n5. DASHBOARD")all_ok &= check_directory_exists("dashboard", "Dashboard directory")all_ok &= check_file_exists("dashboard/server.py", "Dashboard server")all_ok &= check_file_exists("dashboard/static/index.html", "Dashboard HTML")all_ok &= check_file_exists("dashboard/static/dashboard.js", "Dashboard JavaScript")
# 6. Test Filesprint("\n6. TEST/DEMO FILES")all_ok &= check_file_exists("scripts/demo_run.py", "Demo runner")all_ok &= check_file_exists("scripts/full_demo.py", "Full demo")all_ok &= check_file_exists("tests/environment_test.go", "Go tests")
# 7. README & Docsprint("\n7. DOCUMENTATION")all_ok &= check_file_exists("README.md", "README")all_ok &= check_file_exists("HF_BLOG_POST.md", "Blog post")
# 8. Key Features Checkprint("\n8. KEY FEATURES (Code Inspection)")try: with open("inference.py", encoding="utf-8-sig", errors="ignore") as f: content = f.read() has_coordinator = "--coordinator" in content and "coordinator_step" in content has_curriculum = "CurriculumManager" in content has_planning = "--use-planning" in content and "simulate" in content status = "✓" if has_coordinator else "✗" print(f" {status} Multi-Agent Coordinator mode (Theme 1)") status = "✓" if has_curriculum else "✗" print(f" {status} Curriculum Learning (Theme 4)") status = "✓" if has_planning else "✗" print(f" {status} World Modeling (/simulate) (Theme 3)") all_ok &= has_coordinator and has_curriculum and has_planningexcept Exception as e: print(f" ✗ Could not read inference.py: {e}") all_ok = False
try: with open("main.go", encoding="utf-8-sig", errors="ignore") as f: content = f.read() has_coord_reset = "handleCoordinatorReset" in content has_coord_step = "handleCoordinatorStep" in content has_simulate = "handleSimulate" in content has_reset = "handleReset" in content status = "✓" if has_coord_reset else "✗" print(f" {status} /coordinator/reset endpoint") status = "✓" if has_coord_step else "✗" print(f" {status} /coordinator/step endpoint") status = "✓" if has_simulate else "✗" print(f" {status} /simulate endpoint (world modeling)") status = "✓" if has_reset else "✗" print(f" {status} /reset endpoint (task 1-4 support)") all_ok &= has_coord_reset and has_coord_step and has_simulate and has_resetexcept Exception as e: print(f" ✗ Could not read main.go: {e}") all_ok = False
# 9. Test Quick Functionalityprint("\n9. QUICK FUNCTIONALITY TEST")try: import requests health = requests.get("http://localhost:7860/health", timeout=5) if health.status_code == 200: print(f" ✓ Server health check passed (port 7860)") else: print(f" ✗ Server health check failed ({health.status_code})") all_ok = Falseexcept Exception as e: print(f" ✗ Could not reach server: {e}") all_ok = False
# Final Summaryprint("\n" + "=" * 70)if all_ok: print("✓ PROJECT READY FOR SUBMISSION") print("=" * 70) sys.exit(0)else: print("✗ SOME CHECKS FAILED - REVIEW REQUIRED") print("=" * 70) sys.exit(1)baseline_scores.json+9-9
inference.py+218-81
main.go+112-36
scripts/gridmind_grpo_colab.ipynb+469-365
test_coordinator.pyAdded+86
verify_readiness.pyAdded+150