浏览代码

use directory for model overrides instead (#4003)

* use directory for model overrides instead

* fix maxEpisodes setting
/docs-update
GitHub 5 年前
当前提交
ef530217
共有 4 个文件被更改,包括 131 次插入37 次删除
  1. 47
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  2. 81
      Project/Assets/ML-Agents/Examples/Soccer/Prefabs/StrikersVsGoalieField.prefab
  3. 15
      Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea.prefab
  4. 25
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs

47
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


public class ModelOverrider : MonoBehaviour
{
const string k_CommandLineModelOverrideFlag = "--mlagents-override-model";
const string k_CommandLineModelOverrideDirectoryFlag = "--mlagents-override-model-directory";
const string k_CommandLineQuitAfterEpisodesFlag = "--mlagents-quit-after-episodes";
const string k_CommandLineQuitOnLoadFailure = "--mlagents-quit-on-load-failure";

// Assets paths to use, with the behavior name as the key.
Dictionary<string, string> m_BehaviorNameOverrides = new Dictionary<string, string>();
string m_BehaviorNameOverrideDirectory;
// Max episodes to run. Only used if > 0
// Will default to 1 if override models are specified, otherwise 0.

get { return m_PreviousNumSteps + m_NumSteps; }
}
public bool HasOverrides
{
get { return m_BehaviorNameOverrides.Count > 0 || !string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory); }
}
public static string GetOverrideBehaviorName(string originalBehaviorName)
{
return $"Override_{originalBehaviorName}";
}
/// <summary>
/// Get the asset path to use from the commandline arguments.
/// </summary>

var key = args[i + 1].Trim();
var value = args[i + 2].Trim();
m_BehaviorNameOverrides[key] = value;
}
else if (args[i] == k_CommandLineModelOverrideDirectoryFlag && i < args.Length-1)
{
m_BehaviorNameOverrideDirectory = args[i + 1].Trim();
}
else if (args[i] == k_CommandLineQuitAfterEpisodesFlag && i < args.Length-1)
{

}
}
if (m_BehaviorNameOverrides.Count > 0)
if (HasOverrides)
{
// If overriding models, set maxEpisodes to 1 or the command line value
m_MaxEpisodes = maxEpisodes > 0 ? maxEpisodes : 1;

m_Agent = GetComponent<Agent>();
GetAssetPathFromCommandLine();
if (m_BehaviorNameOverrides.Count > 0)
if (HasOverrides)
{
OverrideModel();
}

m_NumSteps++;
}
NNModel GetModelForBehaviorName(string behaviorName)
public NNModel GetModelForBehaviorName(string behaviorName)
{
if (m_CachedModels.ContainsKey(behaviorName))
{

if (!m_BehaviorNameOverrides.ContainsKey(behaviorName))
string assetPath = null;
if (m_BehaviorNameOverrides.ContainsKey(behaviorName))
Debug.Log($"No override for BehaviorName {behaviorName}");
return null;
assetPath = m_BehaviorNameOverrides[behaviorName];
}
else if(!string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory))
{
assetPath = Path.Combine(m_BehaviorNameOverrideDirectory, $"{behaviorName}.nn");
var assetPath = m_BehaviorNameOverrides[behaviorName];
if (string.IsNullOrEmpty(assetPath))
{
Debug.Log($"No override for BehaviorName {behaviorName}, and no directory set.");
return null;
}
byte[] model = null;
try

catch(IOException)
{
Debug.Log($"Couldn't load file {assetPath}", this);
Debug.Log($"Couldn't load file {assetPath} at full path {Path.GetFullPath(assetPath)}", this);
// Cache the null so we don't repeatedly try to load a missing file
m_CachedModels[behaviorName] = null;
return null;

$"and that the model file exists"
);
Application.Quit(1);
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#endif
m_Agent.SetModel($"Override_{behaviorName}", nnModel);
m_Agent.SetModel(GetOverrideBehaviorName(behaviorName), nnModel);
}
}

81
Project/Assets/ML-Agents/Examples/Soccer/Prefabs/StrikersVsGoalieField.prefab


- component: {fileID: 114492261207303438}
- component: {fileID: 114320493772006642}
- component: {fileID: 9152743230243588598}
- component: {fileID: 9171805407464329310}
m_Layer: 0
m_Name: PurpleGoalie
m_TagString: purpleAgent

m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 0
numStackedVectorObservations: 1
vectorActionSize: 030000000300000003000000
vectorActionDescriptions: []
vectorActionSpaceType: 0
VectorObservationSize: 0
NumStackedVectorObservations: 1
VectorActionSize: 030000000300000003000000
VectorActionDescriptions: []
VectorActionSpaceType: 0
m_Model: {fileID: 11400000, guid: e9c10c18f4eb745d19186a54dbe3ca2e, type: 3}
m_InferenceDevice: 0
m_BehaviorType: 0

m_ObservableAttributeHandling: 0
--- !u!114 &114492261207303438
MonoBehaviour:
m_ObjectHideFlags: 0

agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
maxStep: 3000
MaxStep: 3000
team: 0
area: {fileID: 114559182131992928}
position: 1

m_EditorClassIdentifier:
DecisionPeriod: 5
TakeActionsBetweenDecisions: 1
--- !u!114 &9171805407464329310
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1095606497496374}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a6da8f78a394c6ab027688eab81e04d, type: 3}
m_Name:
m_EditorClassIdentifier:
debugCommandLineOverride:
--- !u!1 &1100217258374548
GameObject:
m_ObjectHideFlags: 0

- component: {fileID: 114850431417842684}
- component: {fileID: 114516244030127556}
- component: {fileID: 404683423509059512}
- component: {fileID: 5776656799174978208}
m_Layer: 0
m_Name: BlueStriker
m_TagString: blueAgent

m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 0
numStackedVectorObservations: 1
vectorActionSize: 030000000300000003000000
vectorActionDescriptions: []
vectorActionSpaceType: 0
VectorObservationSize: 0
NumStackedVectorObservations: 1
VectorActionSize: 030000000300000003000000
VectorActionDescriptions: []
VectorActionSpaceType: 0
m_Model: {fileID: 11400000, guid: 75a830685bf8e43918adc4783a2abebf, type: 3}
m_InferenceDevice: 0
m_BehaviorType: 0

m_ObservableAttributeHandling: 0
--- !u!114 &114850431417842684
MonoBehaviour:
m_ObjectHideFlags: 0

agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
maxStep: 3000
MaxStep: 3000
team: 1
area: {fileID: 114559182131992928}
position: 0

m_EditorClassIdentifier:
DecisionPeriod: 5
TakeActionsBetweenDecisions: 1
--- !u!114 &5776656799174978208
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1131626411948014}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a6da8f78a394c6ab027688eab81e04d, type: 3}
m_Name:
m_EditorClassIdentifier:
debugCommandLineOverride:
--- !u!1 &1141134673700168
GameObject:
m_ObjectHideFlags: 0

- component: {fileID: 5379409612883756837}
- component: {fileID: 2562571719799803906}
- component: {fileID: 1018414316889932458}
- component: {fileID: 493348456414434994}
m_Layer: 0
m_Name: BlueStriker (1)
m_TagString: blueAgent

m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 0
numStackedVectorObservations: 1
vectorActionSize: 030000000300000003000000
vectorActionDescriptions: []
vectorActionSpaceType: 0
VectorObservationSize: 0
NumStackedVectorObservations: 1
VectorActionSize: 030000000300000003000000
VectorActionDescriptions: []
VectorActionSpaceType: 0
m_Model: {fileID: 11400000, guid: 75a830685bf8e43918adc4783a2abebf, type: 3}
m_InferenceDevice: 0
m_BehaviorType: 0

m_ObservableAttributeHandling: 0
--- !u!114 &5379409612883756837
MonoBehaviour:
m_ObjectHideFlags: 0

agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
maxStep: 3000
MaxStep: 3000
team: 1
area: {fileID: 114559182131992928}
position: 0

m_EditorClassIdentifier:
DecisionPeriod: 5
TakeActionsBetweenDecisions: 1
--- !u!114 &493348456414434994
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8360301818957399454}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a6da8f78a394c6ab027688eab81e04d, type: 3}
m_Name:
m_EditorClassIdentifier:
debugCommandLineOverride:

15
Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea.prefab


m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 4
numStackedVectorObservations: 6
vectorActionSize: 03000000030000000300000002000000
vectorActionDescriptions: []
vectorActionSpaceType: 0
VectorObservationSize: 4
NumStackedVectorObservations: 6
VectorActionSize: 03000000030000000300000002000000
VectorActionDescriptions: []
VectorActionSpaceType: 0
m_Model: {fileID: 11400000, guid: 0468bf44b1efd4992b6bf22cadb50d89, type: 3}
m_InferenceDevice: 0
m_BehaviorType: 0

m_ObservableAttributeHandling: 0
--- !u!114 &114925928594762506
MonoBehaviour:
m_ObjectHideFlags: 0

agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
maxStep: 2000
MaxStep: 2000
noWallBrain: {fileID: 11400000, guid: fb2ce36eb40b6480e94ea0b5d7573e47, type: 3}
smallWallBrain: {fileID: 11400000, guid: fb2ce36eb40b6480e94ea0b5d7573e47, type: 3}
bigWallBrain: {fileID: 11400000, guid: 0468bf44b1efd4992b6bf22cadb50d89, type: 3}

m_EditorClassIdentifier:
DecisionPeriod: 5
TakeActionsBetweenDecisions: 1
offsetStep: 0
--- !u!114 &7445449404652947848
MonoBehaviour:
m_ObjectHideFlags: 0

m_Script: {fileID: 11500000, guid: 3a6da8f78a394c6ab027688eab81e04d, type: 3}
m_Name:
m_EditorClassIdentifier:
debugCommandLineOverride:
--- !u!1 &1264699583886832
GameObject:
m_ObjectHideFlags: 0

25
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs


using Unity.MLAgents;
using Unity.Barracuda;
using Unity.MLAgents.Sensors;
using Unity.MLAgentsExamples;
public class WallJumpAgent : Agent
{

Vector3 m_JumpTargetPos;
Vector3 m_JumpStartingPos;
string m_NoWallBehaviorName = "SmallWallJump";
string m_SmallWallBehaviorName = "SmallWallJump";
string m_BigWallBehaviorName = "BigWallJump";
EnvironmentParameters m_ResetParams;
public override void Initialize()

spawnArea.SetActive(false);
m_ResetParams = Academy.Instance.EnvironmentParameters;
// Update model references if we're overriding
var modelOverrider = GetComponent<ModelOverrider>();
if (modelOverrider.HasOverrides)
{
noWallBrain = modelOverrider.GetModelForBehaviorName(m_NoWallBehaviorName);
m_NoWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_NoWallBehaviorName);
smallWallBrain = modelOverrider.GetModelForBehaviorName(m_SmallWallBehaviorName);
m_SmallWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_SmallWallBehaviorName);
bigWallBrain = modelOverrider.GetModelForBehaviorName(m_BigWallBehaviorName);
m_BigWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_BigWallBehaviorName);
}
}
// Begin the jump sequence

m_ResetParams.GetWithDefault("no_wall_height", 0),
localScale.z);
wall.transform.localScale = localScale;
SetModel("SmallWallJump", noWallBrain);
SetModel(m_NoWallBehaviorName, noWallBrain);
}
else if (config == 1)
{

localScale.z);
wall.transform.localScale = localScale;
SetModel("SmallWallJump", smallWallBrain);
SetModel(m_SmallWallBehaviorName, smallWallBrain);
}
else
{

height,
localScale.z);
wall.transform.localScale = localScale;
SetModel("BigWallJump", bigWallBrain);
SetModel(m_BigWallBehaviorName, bigWallBrain);
}
}
}
正在加载...
取消
保存