huypham37 / AIML-UM-14

0 stars 0 forks source link

"Discrete Action size of the model does not match. the Brainparameters expects 4 but the model contain 3" #7

Closed huypham37 closed 3 days ago

huypham37 commented 3 days ago

Discrete Action Size Mismatch: BrainParameters Expects 4 but Model Contains 3

Description

I'm encountering an issue where the discrete action size between the ML-Agents script and the Unity environment build does not match. The BrainParameters in Unity expects 4 discrete action spaces, but the model contains 3. This leads to a mismatch error when trying to run the environment.

Error Message

Discrete Action size of the model does not match. The Brainparameters expects 4 but the model contains 3.

Environment

Steps to Reproduce

  1. Set up the environment with an action size mismatch between the Unity environment build and the ML-Agents script.
  2. Run the environment.
  3. Observe the mismatch error.

Solution

I solved the issue by configuring the default discrete action spaces of the agents using OnValidate in the Unity script. Here is the code snippet that resolved the problem:

void OnValidate()
{
    // Configure action space in editor
    var behaviorParameters = GetComponent<BehaviorParameters>();
    if (behaviorParameters != null)
    {
        behaviorParameters.BrainParameters.ActionSpec = ActionSpec.MakeDiscrete(3, 3, 3, 2);
    }
}

After applying the above configuration, I rebuilt the model with the correct discrete action spaces, and the problem was resolved.

Expected Behavior

The discrete action sizes between the Unity environment and the script should match, preventing the error from occurring.

Additional Context

This issue is caused by the mismatch between the number of discrete action spaces expected by BrainParameters and the number in the ML-Agents model. Modifying the action space via OnValidate and rebuilding the model resolved the problem.