Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +64 -139
inference.py
CHANGED
|
@@ -1,153 +1,78 @@
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
-
|
| 4 |
from env import TrafficEnv
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
MEDIUM_CONFIG = {
|
| 24 |
-
"max_steps": 20,
|
| 25 |
-
"max_queue": 20,
|
| 26 |
-
"arrival_rate": (1, 3),
|
| 27 |
-
"discharge_rate": (3, 5),
|
| 28 |
-
"emergency_prob": 0.03,
|
| 29 |
-
"switch_penalty": 0.2,
|
| 30 |
-
"starvation_threshold": 10,
|
| 31 |
-
"burst_prob": 0.2,
|
| 32 |
-
"burst_multiplier": 1.5,
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
HARD_CONFIG = {
|
| 36 |
-
"max_steps": 20,
|
| 37 |
-
"max_queue": 20,
|
| 38 |
-
"arrival_rate": (2, 4),
|
| 39 |
-
"discharge_rate": (3, 5),
|
| 40 |
-
"emergency_prob": 0.05,
|
| 41 |
-
"switch_penalty": 0.2,
|
| 42 |
-
"starvation_threshold": 8,
|
| 43 |
-
"burst_prob": 0.35,
|
| 44 |
-
"burst_multiplier": 2.0,
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def strict_score(x: float) -> float:
|
| 49 |
-
"""
|
| 50 |
-
Convert any raw value into a score strictly inside (0, 1).
|
| 51 |
-
This avoids validator failures for exact 0.0 or 1.0.
|
| 52 |
-
"""
|
| 53 |
-
# If x is RL reward in [-1, 1], map to [0, 1]
|
| 54 |
x = (float(x) + 1.0) / 2.0
|
| 55 |
-
# Clamp strictly inside (0, 1)
|
| 56 |
return max(0.001, min(0.999, x))
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
Reply
|
| 82 |
-
"""
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
temperature=0,
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
content = response.choices[0].message.content.strip()
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
action = int(content)
|
| 103 |
-
if action not in (0, 1):
|
| 104 |
-
action = 0
|
| 105 |
-
except Exception:
|
| 106 |
action = 0
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def run_task(task_name: str, config: dict) -> float:
|
| 112 |
-
env = TrafficEnv(config)
|
| 113 |
-
agent = LLMAgent()
|
| 114 |
-
|
| 115 |
-
state = env.reset()
|
| 116 |
-
agent.reset()
|
| 117 |
-
|
| 118 |
-
print("[START]", flush=True)
|
| 119 |
-
|
| 120 |
-
done = False
|
| 121 |
-
step_idx = 0
|
| 122 |
-
total_reward = 0.0
|
| 123 |
-
|
| 124 |
-
while not done:
|
| 125 |
-
action = agent.select_action(state)
|
| 126 |
-
state, reward, done, info = env.step(action)
|
| 127 |
-
|
| 128 |
-
step_score = strict_score(reward)
|
| 129 |
-
print(
|
| 130 |
-
f"[STEP] task={task_name}, step={step_idx}, action={action}, score={step_score:.3f}, done={done}",
|
| 131 |
-
flush=True,
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
total_reward += reward
|
| 135 |
-
step_idx += 1
|
| 136 |
-
|
| 137 |
-
avg_reward = total_reward / max(1, step_idx)
|
| 138 |
-
final_score = strict_score(avg_reward)
|
| 139 |
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
("medium", MEDIUM_CONFIG),
|
| 149 |
-
("hard", HARD_CONFIG),
|
| 150 |
-
]
|
| 151 |
|
| 152 |
-
|
| 153 |
-
run_task(task_name, config)
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
|
|
|
| 3 |
from env import TrafficEnv
|
| 4 |
|
| 5 |
+
# Minimal config (no tasks.py dependency)
|
| 6 |
+
CONFIG = {
|
| 7 |
+
"max_steps": 20,
|
| 8 |
+
"max_queue": 20,
|
| 9 |
+
"arrival_rate": (0, 2),
|
| 10 |
+
"discharge_rate": (3, 5),
|
| 11 |
+
"emergency_prob": 0.02,
|
| 12 |
+
"switch_penalty": 0.2,
|
| 13 |
+
"starvation_threshold": 10,
|
| 14 |
+
"burst_prob": 0.1,
|
| 15 |
+
"burst_multiplier": 1.2,
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def strict_score(x):
|
| 19 |
+
# Convert [-1,1] → (0,1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
x = (float(x) + 1.0) / 2.0
|
|
|
|
| 21 |
return max(0.001, min(0.999, x))
|
| 22 |
|
| 23 |
+
# LLM client (IMPORTANT)
|
| 24 |
+
client = OpenAI(
|
| 25 |
+
base_url=os.environ["API_BASE_URL"],
|
| 26 |
+
api_key=os.environ["API_KEY"]
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
MODEL_NAME = os.environ["MODEL_NAME"]
|
| 30 |
+
|
| 31 |
+
env = TrafficEnv(CONFIG)
|
| 32 |
+
|
| 33 |
+
print("[START]", flush=True)
|
| 34 |
+
|
| 35 |
+
state = env.reset()
|
| 36 |
+
done = False
|
| 37 |
+
step_count = 0
|
| 38 |
+
total_reward = 0.0
|
| 39 |
+
|
| 40 |
+
while not done:
|
| 41 |
+
prompt = f"""
|
| 42 |
+
State: {state}
|
| 43 |
+
Choose action:
|
| 44 |
+
0 = keep
|
| 45 |
+
1 = switch
|
| 46 |
+
Reply only 0 or 1
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
response = client.chat.completions.create(
|
| 50 |
+
model=MODEL_NAME,
|
| 51 |
+
messages=[
|
| 52 |
+
{"role": "system", "content": "Reply only 0 or 1."},
|
| 53 |
+
{"role": "user", "content": prompt}
|
| 54 |
+
],
|
| 55 |
+
temperature=0
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
action = int(response.choices[0].message.content.strip())
|
| 60 |
+
if action not in [0, 1]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
action = 0
|
| 62 |
+
except:
|
| 63 |
+
action = 0
|
| 64 |
|
| 65 |
+
state, reward, done, info = env.step(action)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
score = strict_score(reward)
|
| 68 |
|
| 69 |
+
print(f"[STEP] step={step_count}, score={score:.3f}, done={done}", flush=True)
|
| 70 |
|
| 71 |
+
total_reward += reward
|
| 72 |
+
step_count += 1
|
| 73 |
|
| 74 |
+
# Final score (IMPORTANT)
|
| 75 |
+
final_score = total_reward / max(1, step_count)
|
| 76 |
+
final_score = strict_score(final_score)
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
print(f"[END] score={final_score:.3f}", flush=True)
|
|
|