Spaces:
Sleeping
Sleeping
File size: 7,876 Bytes
a9d8689 | 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 | """
FastAPI server for NGO Multi-Agent Coordination Environment
Provides REST API endpoints for the RL environment
"""
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Dict, List, Optional
import numpy as np
import sys
import os
# Add parent directory to path to import the environment
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ngo_coordination_env import NGOCoordinationEnv
app = FastAPI(
title="NGO Multi-Agent Coordination Environment",
description="OpenEnv-compatible RL environment for multi-agent negotiation",
version="1.0.0"
)
# Store environment instances per session
environments = {}
class ResetRequest(BaseModel):
session_id: str = "default"
seed: Optional[int] = None
class StepRequest(BaseModel):
session_id: str = "default"
actions: List[List[float]] # Shape: (num_agents, 3)
class EnvConfig(BaseModel):
num_agents: int = 3
max_steps: int = 50
@app.get("/")
async def root():
"""Root endpoint with API information"""
return {
"name": "NGO Multi-Agent Coordination Environment",
"version": "1.0.0",
"description": "OpenEnv-compatible RL environment for training multi-agent negotiation strategies",
"theme": "Multi-Agent Interactions",
"endpoints": {
"/": "API information",
"/health": "Health check",
"/env/create": "Create new environment instance",
"/env/reset": "Reset environment",
"/env/step": "Take environment step",
"/env/info": "Get environment information",
"/results/{filename}": "Get training result files"
},
"documentation": "/docs"
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "message": "Environment server is running"}
@app.post("/env/create")
async def create_environment(config: EnvConfig):
"""Create a new environment instance"""
try:
session_id = f"env_{len(environments)}"
env = NGOCoordinationEnv(
num_agents=config.num_agents,
max_steps=config.max_steps
)
environments[session_id] = env
return {
"session_id": session_id,
"num_agents": config.num_agents,
"max_steps": config.max_steps,
"observation_space": str(env.observation_space),
"action_space": str(env.action_space)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/env/reset")
async def reset_environment(request: ResetRequest):
"""Reset the environment"""
try:
# Create environment if it doesn't exist
if request.session_id not in environments:
environments[request.session_id] = NGOCoordinationEnv()
env = environments[request.session_id]
observation, info = env.reset(seed=request.seed)
# Convert numpy arrays and numpy scalars to Python types for JSON serialization
obs_serializable = {}
for key, value in observation.items():
if isinstance(value, np.ndarray):
obs_serializable[key] = value.tolist()
elif isinstance(value, (np.integer, np.floating)):
obs_serializable[key] = value.item()
else:
obs_serializable[key] = value
# Convert info dict as well
info_serializable = {}
for key, value in info.items():
if isinstance(value, np.ndarray):
info_serializable[key] = value.tolist()
elif isinstance(value, (np.integer, np.floating)):
info_serializable[key] = value.item()
else:
info_serializable[key] = value
return {
"observation": obs_serializable,
"info": info_serializable
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/env/step")
async def step_environment(request: StepRequest):
"""Take a step in the environment"""
try:
if request.session_id not in environments:
raise HTTPException(status_code=404, detail="Environment not found. Call /env/reset first.")
env = environments[request.session_id]
actions = np.array(request.actions, dtype=np.float32)
observation, reward, terminated, truncated, info = env.step(actions)
# Convert numpy arrays and numpy scalars to Python types for JSON serialization
obs_serializable = {}
for key, value in observation.items():
if isinstance(value, np.ndarray):
obs_serializable[key] = value.tolist()
elif isinstance(value, (np.integer, np.floating)):
obs_serializable[key] = value.item()
else:
obs_serializable[key] = value
# Convert info dict as well
info_serializable = {}
for key, value in info.items():
if isinstance(value, np.ndarray):
info_serializable[key] = value.tolist()
elif isinstance(value, (np.integer, np.floating)):
info_serializable[key] = value.item()
else:
info_serializable[key] = value
return {
"observation": obs_serializable,
"reward": float(reward),
"terminated": bool(terminated),
"truncated": bool(truncated),
"info": info_serializable
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/env/info")
async def get_environment_info():
"""Get information about the environment"""
env = NGOCoordinationEnv()
return {
"name": "NGO Multi-Agent Coordination Environment",
"num_agents": env.num_agents,
"max_steps": env.max_steps,
"observation_space": {
"urgency": "Discrete(10) - Urgency level 1-10",
"available_resources": "Box(0, 100) - Volunteers available",
"people_affected": "Box(0, 300) - People needing help",
"other_agents_actions": "Box(0, 100) - Other agents' last actions",
"coalition_status": "MultiBinary(3) - Coalition membership",
"communication_channel": "Box(0, 1) - Cooperation signals"
},
"action_space": {
"description": "Box(3) - [allocation%, cooperation_signal, negotiation_bid]",
"allocation_percentage": "0-1: How much resources to deploy",
"cooperation_signal": "0-1: Willingness to cooperate",
"negotiation_bid": "0-1: Negotiation offer"
},
"tasks": [
"Cooperation - Maximize total impact",
"Competition - Individual performance",
"Negotiation - Fair compromises",
"Coalition - Strategic alliances"
]
}
@app.get("/results/{filename}")
async def get_result_file(filename: str):
"""Serve training result files"""
file_path = os.path.join("results", filename)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(file_path)
@app.get("/results/")
async def list_results():
"""List available result files"""
results_dir = "results"
if not os.path.exists(results_dir):
return {"files": []}
files = os.listdir(results_dir)
return {
"files": files,
"urls": {f: f"/results/{f}" for f in files}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|