Update inference.py
Browse files- inference.py +124 -126
inference.py
CHANGED
|
@@ -1,130 +1,128 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from env import TrafficEnv
|
| 5 |
-
from tasks import get_config
|
| 6 |
-
from baseline_agent import RuleBasedAgent
|
| 7 |
import os
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
state,reward,done,info = env.step(action)
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
for task_name in
|
| 110 |
-
config
|
| 111 |
-
eval_env = TrafficEnv(config)
|
| 112 |
-
eval_agent = LLMAgent()
|
| 113 |
-
|
| 114 |
-
state = eval_env.reset()
|
| 115 |
-
eval_agent.reset()
|
| 116 |
-
|
| 117 |
-
print("[START]", flush=True)
|
| 118 |
-
|
| 119 |
-
done = False
|
| 120 |
-
step_idx = 0
|
| 121 |
-
total_reward = 0.0
|
| 122 |
-
|
| 123 |
-
while not done:
|
| 124 |
-
action = eval_agent.select_action(state)
|
| 125 |
-
state, reward, done, info = eval_env.step(action)
|
| 126 |
-
print(f"[STEP] step={step_idx}, reward={reward}, done={done}", flush=True)
|
| 127 |
-
step_idx += 1
|
| 128 |
-
total_reward += reward
|
| 129 |
-
|
| 130 |
-
print("[END]", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from env import TrafficEnv
|
| 4 |
+
|
| 5 |
+
EASY_CONFIG = {
|
| 6 |
+
"max_steps": 20,
|
| 7 |
+
"max_queue": 20,
|
| 8 |
+
"arrival_rate": (0, 2),
|
| 9 |
+
"discharge_rate": (3, 5),
|
| 10 |
+
"emergency_prob": 0.01,
|
| 11 |
+
"switch_penalty": 0.2,
|
| 12 |
+
"starvation_threshold": 10,
|
| 13 |
+
"burst_prob": 0.0,
|
| 14 |
+
"burst_multiplier": 1.0,
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
MEDIUM_CONFIG = {
|
| 18 |
+
"max_steps": 20,
|
| 19 |
+
"max_queue": 20,
|
| 20 |
+
"arrival_rate": (1, 3),
|
| 21 |
+
"discharge_rate": (3, 5),
|
| 22 |
+
"emergency_prob": 0.03,
|
| 23 |
+
"switch_penalty": 0.2,
|
| 24 |
+
"starvation_threshold": 10,
|
| 25 |
+
"burst_prob": 0.2,
|
| 26 |
+
"burst_multiplier": 1.5,
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
HARD_CONFIG = {
|
| 30 |
+
"max_steps": 20,
|
| 31 |
+
"max_queue": 20,
|
| 32 |
+
"arrival_rate": (2, 4),
|
| 33 |
+
"discharge_rate": (3, 5),
|
| 34 |
+
"emergency_prob": 0.05,
|
| 35 |
+
"switch_penalty": 0.2,
|
| 36 |
+
"starvation_threshold": 8,
|
| 37 |
+
"burst_prob": 0.35,
|
| 38 |
+
"burst_multiplier": 2.0,
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
def strict_score(x: float) -> float:
|
| 42 |
+
x = (float(x) + 1.0) / 2.0
|
| 43 |
+
return max(0.001, min(0.999, x))
|
| 44 |
+
|
| 45 |
+
def build_client():
|
| 46 |
+
api_base_url = os.environ.get("API_BASE_URL")
|
| 47 |
+
api_key = os.environ.get("API_KEY")
|
| 48 |
+
model_name = os.environ.get("MODEL_NAME", "gpt-4o-mini")
|
| 49 |
+
|
| 50 |
+
if api_base_url and api_key:
|
| 51 |
+
client = OpenAI(base_url=api_base_url, api_key=api_key)
|
| 52 |
+
return client, model_name, True
|
| 53 |
+
|
| 54 |
+
return None, model_name, False
|
| 55 |
+
|
| 56 |
+
def choose_action(client, model_name, state):
|
| 57 |
+
prompt = f"""
|
| 58 |
+
You are controlling a traffic signal at a 4-way intersection.
|
| 59 |
+
|
| 60 |
+
Current state:
|
| 61 |
+
{state}
|
| 62 |
+
|
| 63 |
+
Available actions:
|
| 64 |
+
0 = keep current signal phase
|
| 65 |
+
1 = switch signal phase
|
| 66 |
+
|
| 67 |
+
Reply with only one number: 0 or 1
|
| 68 |
+
""".strip()
|
| 69 |
+
|
| 70 |
+
response = client.chat.completions.create(
|
| 71 |
+
model=model_name,
|
| 72 |
+
messages=[
|
| 73 |
+
{"role": "system", "content": "Reply with only 0 or 1."},
|
| 74 |
+
{"role": "user", "content": prompt},
|
| 75 |
+
],
|
| 76 |
+
temperature=0,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
content = response.choices[0].message.content.strip()
|
| 80 |
+
|
|
|
|
| 81 |
try:
|
| 82 |
+
action = int(content)
|
| 83 |
+
if action not in (0, 1):
|
| 84 |
+
action = 0
|
| 85 |
+
except Exception:
|
| 86 |
+
action = 0
|
| 87 |
+
|
| 88 |
+
return action
|
| 89 |
+
|
| 90 |
+
def run_task(task_name, config, client, model_name, use_llm):
|
| 91 |
+
env = TrafficEnv(config)
|
| 92 |
+
state = env.reset()
|
| 93 |
+
|
| 94 |
+
print("[START]", flush=True)
|
| 95 |
+
|
| 96 |
+
done = False
|
| 97 |
+
step_idx = 0
|
| 98 |
+
total_reward = 0.0
|
| 99 |
+
|
| 100 |
+
while not done:
|
| 101 |
+
action = choose_action(client, model_name, state) if use_llm else 0
|
| 102 |
+
state, reward, done, info = env.step(action)
|
| 103 |
+
|
| 104 |
+
step_score = strict_score(reward)
|
| 105 |
+
print(
|
| 106 |
+
f"[STEP] task={task_name}, step={step_idx}, score={step_score:.3f}, done={done}",
|
| 107 |
+
flush=True,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
total_reward += reward
|
| 111 |
+
step_idx += 1
|
| 112 |
+
|
| 113 |
+
avg_reward = total_reward / max(1, step_idx)
|
| 114 |
+
final_score = strict_score(avg_reward)
|
| 115 |
+
|
| 116 |
+
print(f"[END] task={task_name}, score={final_score:.3f}", flush=True)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
client, model_name, use_llm = build_client()
|
| 120 |
+
|
| 121 |
+
tasks = [
|
| 122 |
+
("easy", EASY_CONFIG),
|
| 123 |
+
("medium", MEDIUM_CONFIG),
|
| 124 |
+
("hard", HARD_CONFIG),
|
| 125 |
+
]
|
| 126 |
+
|
| 127 |
+
for task_name, config in tasks:
|
| 128 |
+
run_task(task_name, config, client, model_name, use_llm)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|