Spaces:
Sleeping
Sleeping
Update server/app.py
Browse files- server/app.py +32 -10
server/app.py
CHANGED
|
@@ -1,14 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import uvicorn
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from env import TrafficEnv
|
|
|
|
| 3 |
|
| 4 |
+
CONFIG = {
|
| 5 |
+
"max_steps": 20,
|
| 6 |
+
"max_queue": 20,
|
| 7 |
+
"arrival_rate": (0, 2),
|
| 8 |
+
"discharge_rate": (3, 5),
|
| 9 |
+
"emergency_prob": 0.02,
|
| 10 |
+
"switch_penalty": 0.2,
|
| 11 |
+
"starvation_threshold": 10,
|
| 12 |
+
"burst_prob": 0.1,
|
| 13 |
+
"burst_multiplier": 1.2,
|
| 14 |
+
}
|
| 15 |
|
| 16 |
+
app = FastAPI()
|
| 17 |
+
env = TrafficEnv(CONFIG)
|
| 18 |
|
| 19 |
+
@app.get("/")
|
| 20 |
+
def root():
|
| 21 |
+
return {"message": "Traffic Env Running"}
|
| 22 |
|
| 23 |
+
@app.post("/reset")
|
| 24 |
+
def reset():
|
| 25 |
+
return env.reset()
|
| 26 |
+
|
| 27 |
+
@app.post("/step")
|
| 28 |
+
def step(action: dict):
|
| 29 |
+
action_value = action.get("action", 0)
|
| 30 |
+
next_state, reward, done, info = env.step(action_value)
|
| 31 |
+
return {
|
| 32 |
+
"next_state": next_state,
|
| 33 |
+
"reward": reward,
|
| 34 |
+
"done": done,
|
| 35 |
+
"info": info,
|
| 36 |
+
}
|