edbeeching / godot_rl_agents_plugin

The Godot plugin asset for the Godot RL Agents library
MIT License
121 stars 16 forks source link

Action names must be alphabetically ordered for ONNX inference to work. #25

Closed Ivan-267 closed 11 months ago

Ivan-267 commented 11 months ago

This was checked with Godot_v4.2-rc2_mono_win64.

Issue: When defining the action space, action names need to be defined in alphabetic order, e.g.


func get_action_space():
    return {
        "1" : {
        "size": 1,
            "action_type": "continuous"
        },
        "2" : {
            "size": 1,
            "action_type": "continuous"
        },        
    }

Otherwise the action space order sent to Python by the sync node will be different (it seems to be sorted alphabetically) after the dictionary goes through the JSON.stringify method: https://github.com/edbeeching/godot_rl_agents_plugin/blob/main/addons/godot_rl_agents/sync.gd#L178

So if e.g. the order defined in Godot is:

func get_action_space():
    return {
        "2" : {
        "size": 1,
            "action_type": "continuous"
        },
        "1" : {
            "size": 1,
            "action_type": "continuous"
        },        
    }

When doing Python training or inference, because of the conversion, the value for action "1" will be sent to action "2" instead.

When doing onnx inference, the actions will have the same order as defined in the action space method, which means there will be a mismatch when trying to run onnx inference in any env where the action names are not ordered alphabetically.

I ran into this issue in one of my experiments, and was able to solve it by changing the order of actions as defined to be alphabetic. The same issue happens when trying to run inference on e.g. FlyBy where the actions are not ordered alphabetically.