File size: 2,865 Bytes
fcc38f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import time
import numpy as np

BASE_URL = "http://localhost:5002"

def debug_server():
    print(f"Testing Server at {BASE_URL}...")

    # 1. Health Check
    try:
        resp = requests.get(f"{BASE_URL}/health")
        print("Health Check:", resp.json())
    except Exception as e:
        print(f"Cannot connect to server: {e}")
        return

    # 2. Create Environments (模拟 Batch 创建)
    # 假设你的 env_config.yaml 对应的环境名是 'sokoban' 或者其他 registered env
    # 这里你需要根据你实际的 REGISTERED_ENV 填入正确的 env_name
    # 既然你之前的 log 里有 train86, train94,说明是批量创建的
    
    env_ids = [f"debug_env_{i}" for i in range(4)] # 测试 4 个并行环境
    ids2configs = {
        eid: {
            "env_name": "sokoban",  # <--- 请确认这里是你 yaml 里的正确环境名
            # "task_name": "...",   # 如果需要额外的 config 请补充
        } 
        for eid in env_ids
    }

    print(f"\nCreating {len(env_ids)} environments...")
    resp = requests.post(f"{BASE_URL}/environments", json={"ids2configs": ids2configs})
    if resp.status_code != 200:
        print("Create failed:", resp.text)
        return
    print("Success.")

    # 3. Reset Batch (这是你报错的地方)
    print("\nResetting environments (This usually triggers Vulkan errors)...")
    ids2seeds = {eid: 42 + i for i, eid in enumerate(env_ids)}
    
    start_time = time.time()
    resp = requests.post(f"{BASE_URL}/batch/reset", json={"ids2seeds": ids2seeds})
    
    if resp.status_code != 200:
        print("Reset failed:", resp.text)
    else:
        results = resp.json().get("results", {})
        print(f"Reset success! Took {time.time() - start_time:.2f}s")
        # 打印一下 Observation 的形状确认渲染成功
        first_obs = list(results.values())[0][0]
        # 假设 obs 包含图片
        if isinstance(first_obs, dict) and 'image' in first_obs:
            print(f"Obs Image Shape: {np.array(first_obs['image']).shape}")
        else:
            print("Obs structure keys:", first_obs.keys() if isinstance(first_obs, dict) else "Not a dict")

    # 4. Step Batch
    print("\nStepping environments...")
    # 构造一个随机动作,具体格式取决于你的环境 Action Space
    # 这里假设是一个简单的 Discrete 动作或者 Text 动作
    ids2actions = {eid: "move up" for eid in env_ids} 
    
    resp = requests.post(f"{BASE_URL}/batch/step", json={"ids2actions": ids2actions})
    if resp.status_code == 200:
        print("Step success.")
    else:
        print("Step failed:", resp.text)

    # 5. Clean up
    print("\nClosing environments...")
    requests.post(f"{BASE_URL}/batch/close", json={"env_ids": env_ids})
    print("Done.")

if __name__ == "__main__":
    debug_server()