File size: 1,277 Bytes
5516cba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from env import TrafficEnv
from tasks import get_config
from baseline_agent import RuleBasedAgent
import os

app = FastAPI()
env = TrafficEnv(get_config("medium"))
agent = RuleBasedAgent()

class Action(BaseModel):
    action: int

@app.get("/", response_class=HTMLResponse)
def root():
    with open("index.html", "r", encoding="utf-8") as f:
        return f.read()

@app.post("/reset")
def reset():
    state = env.reset()
    try:
        state = state.tolist()
    except:
        pass
    agent.reset()
    return {"state":state}

@app.post("/step")
def step(data:Action):
    state,reward,done,info = env.step(data.action)
    try:
        state = state.tolist()
    except:
        pass
    return {
        "state":state,
        "reward":reward,
        "done":done,
        "info":info
    }

@app.post("/auto_step")
def auto_step():
    state_dict = env.get_state()
    action = agent.select_action(state_dict)
    state,reward,done,info = env.step(action)
    try:
        state = state.tolist()
    except:
        pass
    return {
        "state":state,
        "reward":reward,
        "done":done,
        "info":info,
        "action_taken": action
    }