Spaces:
Sleeping
Sleeping
File size: 9,696 Bytes
63dd587 9d69b2d 47a685d 63dd587 47a685d 63dd587 9d69b2d 63dd587 | 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 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | """
FastAPI server for StructuralDesignEnv (OpenEnv HTTP interface).
Endpoints:
GET /health
GET /tasks
POST /reset
POST /step
GET /state?session_id=...
GET /action_schema
"""
from __future__ import annotations
import uuid
from typing import Any, Dict, Optional
from fastapi import Body, FastAPI, HTTPException
from fastapi.responses import HTMLResponse
import os
from pydantic import BaseModel
from structural_design_env.env import StructuralDesignEnv
from structural_design_env.tasks import TASK_REGISTRY
# ---------------------------------------------------------------------------
# Session manager
# ---------------------------------------------------------------------------
class SessionManager:
def __init__(self):
self._sessions: Dict[str, StructuralDesignEnv] = {}
def create(self, session_id: Optional[str] = None) -> tuple[str, StructuralDesignEnv]:
sid = session_id or str(uuid.uuid4())
env = StructuralDesignEnv()
self._sessions[sid] = env
return sid, env
def get(self, session_id: str) -> Optional[StructuralDesignEnv]:
return self._sessions.get(session_id)
def get_or_create(self, session_id: Optional[str]) -> tuple[str, StructuralDesignEnv]:
if session_id and session_id in self._sessions:
return session_id, self._sessions[session_id]
return self.create(session_id)
session_manager = SessionManager()
# ---------------------------------------------------------------------------
# FastAPI app
# ---------------------------------------------------------------------------
app = FastAPI(
title="StructuralDesignEnv",
description="OpenEnv API for steel frame structural engineering RL environment.",
version="1.0.0",
)
# ---------------------------------------------------------------------------
# Request / response models
# ---------------------------------------------------------------------------
class ResetRequest(BaseModel):
task_id: str = "task1_warehouse"
session_id: Optional[str] = None
seed: Optional[int] = None
class StepRequest(BaseModel):
session_id: Optional[str] = None
message: str # JSON-encoded StructuralAction
class ResetResponse(BaseModel):
session_id: str
observation: dict
class StepResponse(BaseModel):
session_id: str
observation: dict
reward: float
done: bool
info: dict
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@app.get("/health")
def health():
return {"status": "ok", "env": "StructuralDesignEnv", "version": "1.0.0"}
@app.get("/", response_class=HTMLResponse)
@app.get("/demo", response_class=HTMLResponse)
def serve_demo():
demo_path = os.path.join(os.path.dirname(__file__), "demo.html")
if os.path.exists(demo_path):
with open(demo_path, "r", encoding="utf-8") as f:
return f.read()
return "demo.html not found."
@app.get("/tasks")
def list_tasks():
tasks = []
for tid, (cfg, _) in TASK_REGISTRY.items():
tasks.append({
"id": tid,
"name": cfg.name,
"difficulty": cfg.difficulty,
"max_steps": cfg.max_steps,
"n_floors": cfg.n_floors,
"site_width_m": cfg.site_width_m,
"site_depth_m": cfg.site_depth_m,
"description": _task_description(tid),
})
return {"tasks": tasks}
@app.post("/reset", response_model=ResetResponse)
def reset_env(body: Dict[str, Any] | None = Body(default=None)):
req = ResetRequest(**(body or {}))
if req.task_id not in TASK_REGISTRY:
raise HTTPException(
status_code=400,
detail=f"Unknown task_id '{req.task_id}'. Valid: {list(TASK_REGISTRY)}",
)
sid, env = session_manager.get_or_create(req.session_id)
obs = env.reset(task_id=req.task_id, seed=req.seed)
return ResetResponse(session_id=sid, observation=obs)
@app.post("/step", response_model=StepResponse)
def step_env(req: StepRequest):
sid, env = session_manager.get_or_create(req.session_id)
obs, reward, done, info = env.step(req.message)
return StepResponse(
session_id=sid,
observation=obs,
reward=round(reward, 6),
done=done,
info=info,
)
@app.get("/state")
def get_state(session_id: Optional[str] = None):
if not session_id:
raise HTTPException(status_code=400, detail="session_id is required")
env = session_manager.get(session_id)
if env is None:
raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found")
return env.state()
@app.get("/action_schema")
def action_schema():
return {
"description": "Actions for StructuralDesignEnv. Send as JSON string in the 'message' field of /step.",
"actions": [
{
"action_type": "place_column",
"description": "Place a steel column at a grid position on a floor.",
"fields": {
"grid_x": "int [0-19] β horizontal grid position (1 cell = 1m)",
"grid_y": "int [0-19] β depth grid position",
"floor": "int [0..n_floors-1] β floor index (0=ground)",
"section": "str β one of HEB140, HEB160, HEB200, HEB240, HEB300, HEB360, HEB400",
},
"example": {
"action_type": "place_column",
"grid_x": 5,
"grid_y": 0,
"floor": 0,
"section": "HEB200",
},
},
{
"action_type": "place_beam",
"description": "Place a steel beam connecting two column nodes at the same floor.",
"fields": {
"from_node_x": "int β x of start column",
"from_node_y": "int β y of start column",
"to_node_x": "int β x of end column (must share x OR y with start)",
"to_node_y": "int β y of end column",
"floor": "int β floor where both columns sit",
"section": "str β one of IPE200, IPE240, IPE300, IPE360, IPE400, IPE450, IPE500",
"orientation": "'x' (horizontal) or 'y' (depth direction)",
},
"example": {
"action_type": "place_beam",
"from_node_x": 0,
"from_node_y": 0,
"to_node_x": 5,
"to_node_y": 0,
"floor": 0,
"section": "IPE300",
"orientation": "x",
},
},
{
"action_type": "upgrade_section",
"description": "Upgrade an element to the next larger standard section.",
"fields": {
"element_id": "str β element ID from placed_elements list (e.g. 'col_5_0_0')",
},
"example": {"action_type": "upgrade_section", "element_id": "col_5_0_0"},
},
{
"action_type": "downgrade_section",
"description": "Downgrade an element to the next smaller standard section.",
"fields": {
"element_id": "str β element ID",
},
"example": {"action_type": "downgrade_section", "element_id": "beam_0_0_5_0_0"},
},
{
"action_type": "remove_element",
"description": "Remove an element from the structure.",
"fields": {"element_id": "str β element ID"},
"example": {"action_type": "remove_element", "element_id": "col_5_0_0"},
},
{
"action_type": "add_wall",
"description": "Add a concrete shear wall between two column nodes.",
"fields": {
"from_node_x": "int",
"from_node_y": "int",
"to_node_x": "int",
"to_node_y": "int",
"floor": "int",
"thickness_m": "float β 0.2 or 0.3",
"orientation": "'x' or 'y'",
},
"example": {
"action_type": "add_wall",
"from_node_x": 0,
"from_node_y": 0,
"to_node_x": 5,
"to_node_y": 0,
"floor": 0,
"thickness_m": 0.2,
"orientation": "x",
},
},
{
"action_type": "done",
"description": "Signal that the design is complete. Triggers final grading.",
"example": {"action_type": "done"},
},
],
"sections": {
"columns": ["HEB140", "HEB160", "HEB200", "HEB240", "HEB300", "HEB360", "HEB400"],
"beams": ["IPE200", "IPE240", "IPE300", "IPE360", "IPE400", "IPE450", "IPE500"],
},
}
def _task_description(tid: str) -> str:
descriptions = {
"task1_warehouse": "Single-story 20Γ10m warehouse. No lateral loads. Score by validity + material efficiency.",
"task2_office": "3-story 20Γ20m office with wind and light seismic. Score by drift + efficiency + torsional balance.",
"task3_hospital": "3-story hospital in seismic Zone 3. Score by seismic drift + budget + redundancy + utilisation.",
}
return descriptions.get(tid, "")
|