File size: 2,650 Bytes
80c0c44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys

# Add the project root to sys.path
project_root = r"c:\Users\megaa\Downloads\drone_env"
if project_root not in sys.path:
    sys.path.append(project_root)

from server.grid_world_environment import DroneDeliveryEnvironment
from models import DroneAction

def test_tree_flyover():
    env = DroneDeliveryEnvironment()
    obs = env.reset()
    
    # Find a tree on the grid
    tree_pos = None
    for y, row in enumerate(env._grid):
        for x, cell in enumerate(row):
            if cell == "🌳":
                tree_pos = (x, y)
                break
        if tree_pos:
            break
            
    if not tree_pos:
        print("No tree found on small grid, retrying reset...")
        for _ in range(10):
            obs = env.reset()
            for y, row in enumerate(env._grid):
                for x, cell in enumerate(row):
                    if cell == "🌳":
                        tree_pos = (x, y)
                        break
                if tree_pos: break
            if tree_pos: break

    if not tree_pos:
        print("Still no tree found. Test aborted.")
        return

    print(f"Drone start: ({env._drone_x}, {env._drone_y})")
    print(f"Tree found at: {tree_pos}")

    # Move the drone to be adjacent to the tree if possible, or just teleport for testing
    # Actually, we can just simulate the step logic or manually set position and then step
    
    # Let's find direction to the tree
    tx, ty = tree_pos
    dx, dy = env._drone_x, env._drone_y
    
    # Teleport to adjacent
    if tx > 0:
        env._drone_x = tx - 1
        env._drone_y = ty
        direction = "RIGHT"
    elif tx < env._cfg["width"] - 1:
        env._drone_x = tx + 1
        env._drone_y = ty
        direction = "LEFT"
    elif ty > 0:
        env._drone_x = tx
        env._drone_y = ty - 1
        direction = "DOWN"
    else:
        env._drone_x = tx
        env._drone_y = ty + 1
        direction = "UP"
        
    print(f"Teleported drone to adjacent: ({env._drone_x}, {env._drone_y})")
    
    # Perform move into tree
    action = DroneAction(direction=direction)
    obs = env.step(action)
    
    print(f"Action: {direction}")
    print(f"New Pos: ({env._drone_x}, {env._drone_y})")
    print(f"Reward: {obs.reward_last}")
    print(f"Message: {obs.message}")
    
    assert env._drone_x == tx and env._drone_y == ty, "Drone should have moved onto the tree cell"
    assert obs.reward_last == -0.1, f"Reward should be -0.1, got {obs.reward_last}"
    print("Test passed! Flying over tree works and reward is exactly -0.1")

if __name__ == "__main__":
    test_tree_flyover()