openenv compliance
Browse files- environment/env.py +126 -200
environment/env.py
CHANGED
|
@@ -1,26 +1,22 @@
|
|
| 1 |
"""
|
| 2 |
FabYieldEnv β OpenEnv-compliant semiconductor yield optimization environment.
|
| 3 |
-
|
| 4 |
-
Mirrors the HFOrchestratorEnv pattern exactly:
|
| 5 |
-
- Pydantic models for Observation and Action
|
| 6 |
-
- .reset() returns Observation
|
| 7 |
-
- .step(action) returns (Observation, reward_dict, done, info)
|
| 8 |
-
- server.py imports this and mounts it on FastAPI
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
-
import
|
| 14 |
import numpy as np
|
| 15 |
-
from dataclasses import dataclass, field
|
| 16 |
-
from typing import Dict, List, Optional, Tuple, Any
|
| 17 |
from pydantic import BaseModel, Field
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
from
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# βββ Pydantic Models
|
| 24 |
|
| 25 |
class ExperimentRecord(BaseModel):
|
| 26 |
step: int
|
|
@@ -30,32 +26,30 @@ class ExperimentRecord(BaseModel):
|
|
| 30 |
primary_bottleneck_guess: str = ""
|
| 31 |
reasoning: str = ""
|
| 32 |
|
| 33 |
-
|
| 34 |
class FabObservation(BaseModel):
|
| 35 |
-
"""What the agent sees each step. Serializable for FastAPI response."""
|
| 36 |
step: int
|
| 37 |
phase: str
|
|
|
|
| 38 |
budget_remaining: int
|
| 39 |
target_yield: float = 92.0
|
| 40 |
current_best_yield: float = 0.0
|
| 41 |
experiment_history: List[ExperimentRecord] = Field(default_factory=list)
|
| 42 |
phase_hint: str = ""
|
| 43 |
-
reviewer_feedback: str = ""
|
|
|
|
| 44 |
active_params: List[str] = Field(default_factory=list)
|
| 45 |
param_ranges: Dict[str, List[float]] = Field(default_factory=dict)
|
| 46 |
done: bool = False
|
| 47 |
-
episode_result: Optional[str] = None
|
| 48 |
-
|
| 49 |
|
| 50 |
class FabAction(BaseModel):
|
| 51 |
-
|
| 52 |
params: Dict[str, float]
|
| 53 |
primary_bottleneck: str = ""
|
| 54 |
reasoning: str = ""
|
| 55 |
submit: bool = False
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# βββ Phase Config βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
|
| 60 |
EPISODE_PHASES = {
|
| 61 |
"exploration": (1, 4),
|
|
@@ -64,60 +58,54 @@ EPISODE_PHASES = {
|
|
| 64 |
"submission": (12, 12),
|
| 65 |
}
|
| 66 |
|
| 67 |
-
PHASE_HINTS = {
|
| 68 |
-
"exploration": "Vary parameters broadly to map the response surface. Cover diverse combinations.",
|
| 69 |
-
"hypothesis": "You have data now. Form a hypothesis about the primary bottleneck and test it directly.",
|
| 70 |
-
"exploitation": "Converge on the optimum. Fine-tune 1-2 params near your current best point.",
|
| 71 |
-
"submission": "Final step. Submit your recipe and state your causal diagnosis of the yield bottleneck.",
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
MAX_STEPS = 12
|
| 75 |
-
TARGET_YIELD = 92.0
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def _get_phase(step: int) -> str:
|
| 79 |
for phase, (lo, hi) in EPISODE_PHASES.items():
|
| 80 |
-
if lo <= step <= hi:
|
| 81 |
-
return phase
|
| 82 |
return "submission"
|
| 83 |
|
| 84 |
-
|
| 85 |
# βββ Main Environment βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
|
| 87 |
-
class FabYieldEnv:
|
| 88 |
-
|
| 89 |
-
Semiconductor Yield Optimization RL Environment.
|
| 90 |
-
|
| 91 |
-
Episode flow:
|
| 92 |
-
obs = env.reset(difficulty=1) # start new wafer lot
|
| 93 |
-
obs, rewards, done, info = env.step(action) # run one experiment
|
| 94 |
-
...
|
| 95 |
-
obs, rewards, done, info = env.step(FabAction(params={...}, submit=True))
|
| 96 |
-
|
| 97 |
-
difficulty: 1 = 5 params (easy), 2 = 10 params (medium), 3 = 15 params (hard)
|
| 98 |
-
"""
|
| 99 |
-
|
| 100 |
def __init__(self, difficulty: int = 1):
|
|
|
|
| 101 |
self.difficulty = difficulty
|
| 102 |
-
self.rsm
|
| 103 |
-
self.reviewer
|
| 104 |
-
self._obs
|
| 105 |
-
self._done
|
| 106 |
-
self.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
if difficulty is not None:
|
| 111 |
self.difficulty = difficulty
|
| 112 |
|
| 113 |
self.rsm = RSMSimulator(seed=seed, difficulty=self.difficulty)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
)
|
| 118 |
self.reviewer = SeniorEngineerReviewer(constraints)
|
|
|
|
|
|
|
|
|
|
| 119 |
self._done = False
|
| 120 |
-
self._last_submitted_params = None
|
| 121 |
|
| 122 |
active = PHASE_PARAMS[self.difficulty]
|
| 123 |
param_ranges = {p: list(PARAM_RANGES[p]) for p in active}
|
|
@@ -125,182 +113,120 @@ class FabYieldEnv:
|
|
| 125 |
self._obs = FabObservation(
|
| 126 |
step=1,
|
| 127 |
phase="exploration",
|
|
|
|
| 128 |
budget_remaining=MAX_STEPS,
|
| 129 |
-
target_yield=
|
| 130 |
current_best_yield=0.0,
|
| 131 |
experiment_history=[],
|
| 132 |
-
phase_hint=PHASE_HINTS["exploration"],
|
| 133 |
-
reviewer_feedback="",
|
| 134 |
active_params=active,
|
| 135 |
param_ranges=param_ranges,
|
| 136 |
done=False,
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
Returns:
|
| 145 |
-
obs: updated FabObservation
|
| 146 |
-
reward_dict: breakdown of all reward components
|
| 147 |
-
done: episode over?
|
| 148 |
-
info: causal ground truth (for training, not shown to agent)
|
| 149 |
-
"""
|
| 150 |
if self._done:
|
| 151 |
raise RuntimeError("Episode is done. Call reset() first.")
|
| 152 |
|
| 153 |
-
|
| 154 |
-
params = self._clamp_params(
|
| 155 |
-
|
| 156 |
-
#
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
# Record the experiment
|
| 160 |
record = ExperimentRecord(
|
| 161 |
step=self._obs.step,
|
| 162 |
params=params,
|
| 163 |
yield_pct=yield_pct,
|
| 164 |
defect=defect,
|
| 165 |
-
primary_bottleneck_guess=
|
| 166 |
-
reasoning=
|
| 167 |
)
|
| 168 |
self._obs.experiment_history.append(record)
|
|
|
|
| 169 |
|
| 170 |
-
# Update best yield
|
| 171 |
-
best = max(r.yield_pct for r in self._obs.experiment_history)
|
| 172 |
-
self._obs.current_best_yield = best
|
| 173 |
-
|
| 174 |
-
# Advance state
|
| 175 |
self._obs.step += 1
|
| 176 |
self._obs.budget_remaining -= 1
|
| 177 |
self._obs.phase = _get_phase(self._obs.step)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
# Terminal condition
|
| 188 |
-
done = (
|
| 189 |
-
action.submit
|
| 190 |
-
or self._obs.budget_remaining <= 0
|
| 191 |
-
or self._obs.step > MAX_STEPS
|
| 192 |
-
)
|
| 193 |
self._done = done
|
| 194 |
self._obs.done = done
|
| 195 |
-
if done:
|
| 196 |
-
self._obs.episode_result = (
|
| 197 |
-
"success" if best >= TARGET_YIELD else "budget_exhausted"
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
# Compute rewards
|
| 201 |
-
reward_dict = self._compute_reward(
|
| 202 |
-
action=action,
|
| 203 |
-
yield_pct=yield_pct,
|
| 204 |
-
done=done,
|
| 205 |
-
submitted_params=params if action.submit else None,
|
| 206 |
-
reviewer_approved=reviewer_result["approved"],
|
| 207 |
-
)
|
| 208 |
|
| 209 |
-
#
|
|
|
|
|
|
|
| 210 |
info = {
|
| 211 |
"causal_structure": self.rsm.causal_structure(),
|
| 212 |
-
"reviewer_approved": reviewer_result["approved"],
|
| 213 |
-
"true_optimum": self.rsm.optimum_params,
|
| 214 |
"defect": defect,
|
|
|
|
| 215 |
}
|
| 216 |
|
| 217 |
-
return self._obs, reward_dict, done, info
|
| 218 |
-
|
| 219 |
-
def _compute_reward(
|
| 220 |
-
self,
|
| 221 |
-
action: FabAction,
|
| 222 |
-
yield_pct: float,
|
| 223 |
-
done: bool,
|
| 224 |
-
submitted_params: Optional[Dict],
|
| 225 |
-
reviewer_approved: bool,
|
| 226 |
-
) -> Dict[str, float]:
|
| 227 |
-
rewards = {}
|
| 228 |
-
|
| 229 |
-
# 1. YIELD REWARD β Make it a steep hill, not a freebie
|
| 230 |
-
# 0 points for anything under 50% yield. 1.0 points for hitting target.
|
| 231 |
-
yield_floor = 50.0
|
| 232 |
-
if yield_pct <= yield_floor:
|
| 233 |
-
rewards["yield"] = 0.0
|
| 234 |
-
else:
|
| 235 |
-
rewards["yield"] = min((yield_pct - yield_floor) / (TARGET_YIELD - yield_floor), 1.0)
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
# 3. EFFICIENCY REWARD β Gated behind competence
|
| 248 |
-
# You only get the speed bonus if you ACTUALLY fixed the yield AND found the root cause
|
| 249 |
-
if yield_pct >= TARGET_YIELD and rewards["causal"] > 0:
|
| 250 |
-
rewards["efficiency"] = max(0.0, (MAX_STEPS - self._obs.step + 1) / MAX_STEPS)
|
| 251 |
-
else:
|
| 252 |
-
rewards["efficiency"] = 0.0
|
| 253 |
-
|
| 254 |
-
# 4. STABILITY REWARD β Penalize 1-step blind submissions
|
| 255 |
-
if done and submitted_params:
|
| 256 |
-
if self._obs.step < 3: # Punish guessing without experimenting
|
| 257 |
-
rewards["stability"] = 0.0
|
| 258 |
-
else:
|
| 259 |
-
lot_std = self.rsm.lot_variance(submitted_params, n_lots=20)
|
| 260 |
-
rewards["stability"] = max(0.0, 1.0 - lot_std / 5.0)
|
| 261 |
-
if not reviewer_approved:
|
| 262 |
-
rewards["stability"] *= 0.3
|
| 263 |
-
else:
|
| 264 |
-
rewards["stability"] = 0.0
|
| 265 |
-
|
| 266 |
-
# Composite (Weights remain the same)
|
| 267 |
-
rewards["total"] = (
|
| 268 |
-
0.50 * rewards["yield"] +
|
| 269 |
-
0.20 * rewards["efficiency"] +
|
| 270 |
-
0.15 * rewards["causal"] +
|
| 271 |
-
0.15 * rewards["stability"]
|
| 272 |
-
)
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
return {k: round(float(v), 4) for k, v in rewards.items()}
|
| 275 |
|
| 276 |
-
def _clamp_params(self, params:
|
| 277 |
-
|
| 278 |
-
result = {}
|
| 279 |
for p in PHASE_PARAMS[self.difficulty]:
|
| 280 |
lo, hi = PARAM_RANGES[p]
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
return result
|
| 284 |
-
|
| 285 |
-
@property
|
| 286 |
-
def observation_space(self) -> Dict:
|
| 287 |
-
return {
|
| 288 |
-
"type": "dict",
|
| 289 |
-
"fields": [
|
| 290 |
-
"step", "phase", "budget_remaining", "target_yield",
|
| 291 |
-
"current_best_yield", "experiment_history",
|
| 292 |
-
"phase_hint", "reviewer_feedback", "active_params", "param_ranges",
|
| 293 |
-
]
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
@property
|
| 297 |
-
def action_space(self) -> Dict:
|
| 298 |
-
return {
|
| 299 |
-
"type": "dict",
|
| 300 |
-
"fields": {
|
| 301 |
-
"params": "Dict[str, float] β experiment parameter values",
|
| 302 |
-
"primary_bottleneck": "str β agent's causal hypothesis",
|
| 303 |
-
"reasoning": "str β one sentence explanation",
|
| 304 |
-
"submit": "bool β True to end episode and submit recipe",
|
| 305 |
-
}
|
| 306 |
-
}
|
|
|
|
| 1 |
"""
|
| 2 |
FabYieldEnv β OpenEnv-compliant semiconductor yield optimization environment.
|
| 3 |
+
Features: Multi-Agent Financial Controller, TPU/GPU Task Binning, and Test-Time Compute.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
+
import random
|
| 9 |
import numpy as np
|
|
|
|
|
|
|
| 10 |
from pydantic import BaseModel, Field
|
| 11 |
+
from typing import Dict, List, Optional, Tuple
|
| 12 |
|
| 13 |
+
# π 1. STRICT OPENENV COMPLIANCE
|
| 14 |
+
from openenv import Environment
|
| 15 |
|
| 16 |
+
from environment.rsm_simulator import RSMSimulator, PARAM_RANGES, PHASE_PARAMS
|
| 17 |
+
from agents.senior_reviewer import SeniorEngineerReviewer, sample_episode_constraints
|
| 18 |
|
| 19 |
+
# βββ Pydantic Models βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
|
| 21 |
class ExperimentRecord(BaseModel):
|
| 22 |
step: int
|
|
|
|
| 26 |
primary_bottleneck_guess: str = ""
|
| 27 |
reasoning: str = ""
|
| 28 |
|
|
|
|
| 29 |
class FabObservation(BaseModel):
|
|
|
|
| 30 |
step: int
|
| 31 |
phase: str
|
| 32 |
+
task_target: str # NEW: i9 GPU vs Edge TPU Binning
|
| 33 |
budget_remaining: int
|
| 34 |
target_yield: float = 92.0
|
| 35 |
current_best_yield: float = 0.0
|
| 36 |
experiment_history: List[ExperimentRecord] = Field(default_factory=list)
|
| 37 |
phase_hint: str = ""
|
| 38 |
+
reviewer_feedback: str = ""
|
| 39 |
+
financial_feedback: str = "" # NEW: Multi-agent cost feedback
|
| 40 |
active_params: List[str] = Field(default_factory=list)
|
| 41 |
param_ranges: Dict[str, List[float]] = Field(default_factory=dict)
|
| 42 |
done: bool = False
|
| 43 |
+
episode_result: Optional[str] = None
|
|
|
|
| 44 |
|
| 45 |
class FabAction(BaseModel):
|
| 46 |
+
think: str = "" # NEW: Claude-style Test-Time Compute block
|
| 47 |
params: Dict[str, float]
|
| 48 |
primary_bottleneck: str = ""
|
| 49 |
reasoning: str = ""
|
| 50 |
submit: bool = False
|
| 51 |
|
| 52 |
+
# βββ Config & Task Definitions ββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 53 |
|
| 54 |
EPISODE_PHASES = {
|
| 55 |
"exploration": (1, 4),
|
|
|
|
| 58 |
"submission": (12, 12),
|
| 59 |
}
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
MAX_STEPS = 12
|
|
|
|
| 62 |
|
| 63 |
+
# NEW: The GPU/TPU Binning Tasks
|
| 64 |
+
TASKS = [
|
| 65 |
+
{
|
| 66 |
+
"name": "Data Center GPU (H200X Flagship)",
|
| 67 |
+
"target": 95.0,
|
| 68 |
+
"hint": "Maximize performance. Budget is high, but the yield surface is highly volatile."
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"name": "Edge TPU (Mobile Neural Engine)",
|
| 72 |
+
"target": 90.0,
|
| 73 |
+
"hint": "Thermal efficiency is critical. The Financial Controller will reject high temp/power recipes."
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
|
| 77 |
def _get_phase(step: int) -> str:
|
| 78 |
for phase, (lo, hi) in EPISODE_PHASES.items():
|
| 79 |
+
if lo <= step <= hi: return phase
|
|
|
|
| 80 |
return "submission"
|
| 81 |
|
|
|
|
| 82 |
# βββ Main Environment βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 83 |
|
| 84 |
+
class FabYieldEnv(Environment): # π Inherits from OpenEnv
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def __init__(self, difficulty: int = 1):
|
| 87 |
+
super().__init__()
|
| 88 |
self.difficulty = difficulty
|
| 89 |
+
self.rsm = None
|
| 90 |
+
self.reviewer = None
|
| 91 |
+
self._obs = None
|
| 92 |
+
self._done = False
|
| 93 |
+
self._current_task = None
|
| 94 |
+
|
| 95 |
+
# βββ METHOD 1: RESET βββββββββββββββββββββββββββββββββββββββ
|
| 96 |
+
def reset(self, seed: int = None, difficulty: int = None) -> dict:
|
| 97 |
if difficulty is not None:
|
| 98 |
self.difficulty = difficulty
|
| 99 |
|
| 100 |
self.rsm = RSMSimulator(seed=seed, difficulty=self.difficulty)
|
| 101 |
+
rng = np.random.default_rng(seed)
|
| 102 |
+
|
| 103 |
+
constraints = sample_episode_constraints(PHASE_PARAMS[self.difficulty], rng)
|
|
|
|
| 104 |
self.reviewer = SeniorEngineerReviewer(constraints)
|
| 105 |
+
|
| 106 |
+
# Randomly assign GPU or TPU task
|
| 107 |
+
self._current_task = random.choice(TASKS)
|
| 108 |
self._done = False
|
|
|
|
| 109 |
|
| 110 |
active = PHASE_PARAMS[self.difficulty]
|
| 111 |
param_ranges = {p: list(PARAM_RANGES[p]) for p in active}
|
|
|
|
| 113 |
self._obs = FabObservation(
|
| 114 |
step=1,
|
| 115 |
phase="exploration",
|
| 116 |
+
task_target=f"{self._current_task['name']} β {self._current_task['hint']}",
|
| 117 |
budget_remaining=MAX_STEPS,
|
| 118 |
+
target_yield=self._current_task['target'],
|
| 119 |
current_best_yield=0.0,
|
| 120 |
experiment_history=[],
|
|
|
|
|
|
|
| 121 |
active_params=active,
|
| 122 |
param_ranges=param_ranges,
|
| 123 |
done=False,
|
| 124 |
)
|
| 125 |
+
|
| 126 |
+
# π Return dict for OpenEnv serialization
|
| 127 |
+
return self._obs.model_dump()
|
| 128 |
+
|
| 129 |
+
# βββ METHOD 2: STEP ββββββββββββββββββββββββββββββββββββββββ
|
| 130 |
+
def step(self, action: dict) -> Tuple[dict, Dict[str, float], bool, dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
if self._done:
|
| 132 |
raise RuntimeError("Episode is done. Call reset() first.")
|
| 133 |
|
| 134 |
+
act = FabAction(**action)
|
| 135 |
+
params = self._clamp_params(act.params)
|
| 136 |
+
|
| 137 |
+
# π FINANCIAL CONTROLLER LOGIC (Multi-Agent)
|
| 138 |
+
fin_approved = True
|
| 139 |
+
fin_feedback = ""
|
| 140 |
+
if "Edge TPU" in self._current_task["name"]:
|
| 141 |
+
if params.get("temp", 0) > 180 or params.get("rf_power", 0) > 300:
|
| 142 |
+
fin_approved = False
|
| 143 |
+
fin_feedback = "β FINANCIAL CONTROLLER REJECTED: Edge TPU requires thermal efficiency. Temp must be <=180 and RF Power <=300."
|
| 144 |
+
|
| 145 |
+
# If financially rejected, simulate a failed lot without checking physics
|
| 146 |
+
if not fin_approved:
|
| 147 |
+
yield_pct = 0.0
|
| 148 |
+
defect = "budget_overrun"
|
| 149 |
+
self._obs.financial_feedback = fin_feedback
|
| 150 |
+
else:
|
| 151 |
+
yield_pct, defect = self.rsm.predict(params)
|
| 152 |
+
self._obs.financial_feedback = "β
Financial Controller Approved."
|
| 153 |
|
|
|
|
| 154 |
record = ExperimentRecord(
|
| 155 |
step=self._obs.step,
|
| 156 |
params=params,
|
| 157 |
yield_pct=yield_pct,
|
| 158 |
defect=defect,
|
| 159 |
+
primary_bottleneck_guess=act.primary_bottleneck,
|
| 160 |
+
reasoning=act.reasoning,
|
| 161 |
)
|
| 162 |
self._obs.experiment_history.append(record)
|
| 163 |
+
self._obs.current_best_yield = max(r.yield_pct for r in self._obs.experiment_history)
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
self._obs.step += 1
|
| 166 |
self._obs.budget_remaining -= 1
|
| 167 |
self._obs.phase = _get_phase(self._obs.step)
|
| 168 |
+
|
| 169 |
+
reviewer_approved = True
|
| 170 |
+
if act.submit:
|
| 171 |
+
rev_res = self.reviewer.review(params)
|
| 172 |
+
reviewer_approved = rev_res["approved"]
|
| 173 |
+
self._obs.reviewer_feedback = rev_res["feedback"]
|
| 174 |
+
|
| 175 |
+
done = (act.submit or self._obs.budget_remaining <= 0 or self._obs.step > MAX_STEPS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
self._done = done
|
| 177 |
self._obs.done = done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
# π REWARD CALCULATION
|
| 180 |
+
reward_dict = self._compute_reward(act, yield_pct, done, params if act.submit else None, reviewer_approved, fin_approved)
|
| 181 |
+
|
| 182 |
info = {
|
| 183 |
"causal_structure": self.rsm.causal_structure(),
|
|
|
|
|
|
|
| 184 |
"defect": defect,
|
| 185 |
+
"financial_approved": fin_approved
|
| 186 |
}
|
| 187 |
|
| 188 |
+
return self._obs.model_dump(), reward_dict, done, info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
# βββ METHOD 3: STATE βββββββββββββββββββββββββββββββββββββββ
|
| 191 |
+
def state(self) -> dict:
|
| 192 |
+
"""OpenEnv read-only state method."""
|
| 193 |
+
if self._obs is None:
|
| 194 |
+
raise RuntimeError("Environment not initialized.")
|
| 195 |
+
return self._obs.model_dump()
|
| 196 |
+
|
| 197 |
+
# βββ REWARDS βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 198 |
+
def _compute_reward(self, action: FabAction, yield_pct: float, done: bool, submitted_params: dict, rev_ok: bool, fin_ok: bool) -> dict:
|
| 199 |
+
rewards = {"yield": 0.0, "causal": 0.0, "reasoning": 0.0, "financial": 0.0, "total": 0.0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
# 1. Yield Reward
|
| 202 |
+
target = self._current_task["target"]
|
| 203 |
+
if yield_pct > 50.0:
|
| 204 |
+
rewards["yield"] = min((yield_pct - 50.0) / (target - 50.0), 1.0)
|
| 205 |
+
|
| 206 |
+
# 2. Causal Attribution
|
| 207 |
+
causal = self.rsm.causal_structure()
|
| 208 |
+
if action.primary_bottleneck.strip().lower() == causal["primary_param"].lower():
|
| 209 |
+
rewards["causal"] = 1.0
|
| 210 |
+
|
| 211 |
+
# 3. TEST-TIME COMPUTE REWARD (Claude-style)
|
| 212 |
+
think_len = len(action.think.split())
|
| 213 |
+
if think_len > 30:
|
| 214 |
+
rewards["reasoning"] = 0.5 # Good thought process
|
| 215 |
+
elif think_len == 0:
|
| 216 |
+
rewards["reasoning"] = -0.5 # Penalty for skipping thought
|
| 217 |
+
|
| 218 |
+
# 4. Financial Controller Penalty
|
| 219 |
+
if not fin_ok:
|
| 220 |
+
rewards["financial"] = -1.0
|
| 221 |
+
rewards["yield"] = 0.0 # Nullify yield if it's too expensive
|
| 222 |
+
|
| 223 |
+
# Total Composite
|
| 224 |
+
rewards["total"] = (0.40 * rewards["yield"]) + (0.20 * rewards["causal"]) + (0.20 * rewards["reasoning"]) + (0.20 * rewards["financial"])
|
| 225 |
return {k: round(float(v), 4) for k, v in rewards.items()}
|
| 226 |
|
| 227 |
+
def _clamp_params(self, params: dict) -> dict:
|
| 228 |
+
res = {}
|
|
|
|
| 229 |
for p in PHASE_PARAMS[self.difficulty]:
|
| 230 |
lo, hi = PARAM_RANGES[p]
|
| 231 |
+
res[p] = float(np.clip(params.get(p, (lo + hi) / 2.0), lo, hi))
|
| 232 |
+
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|