emergency-resource-env / test_random.py
Sudeeksha-07's picture
Deploy Emergency Resource Environment
7266370
Raw
History Blame Contribute Delete
1.57 kB
import requests
import random
import time
BASE_URL = "http://127.0.0.1:7860"
def run_random_agent(task="easy"):
print(f"\n=== Running Random Agent on {task} task ===")
# Reset
response = requests.post(f"{BASE_URL}/reset", params={"task": task})
if response.status_code != 200:
print(f"Failed to reset: {response.status_code}")
return 0
obs = response.json()
done = False
step = 0
total_reward = 0
actions = ["move", "allocate"]
directions = ["up", "down", "left", "right"]
while not done and step < 50:
action_type = random.choice(actions)
if action_type == "move":
action = {
"action_type": "move",
"direction": random.choice(directions)
}
else:
action = {"action_type": "allocate"}
response = requests.post(f"{BASE_URL}/step", json=action)
result = response.json()
total_reward += result['reward']['score']
done = result['done']
step += 1
print(f"Step {step}: {action_type} -> Reward: {result['reward']['score']:.2f}, Done: {done}")
time.sleep(0.1)
response = requests.get(f"{BASE_URL}/grade")
grade = response.json()
print(f"\nCompleted in {step} steps")
print(f"Grade score: {grade['score']:.2f}")
return grade['score']
if __name__ == "__main__":
print("Testing Random Agent on easy task...")
score = run_random_agent("easy")
print(f"\nFinal Score: {score:.2f}")