浏览代码

Track reward for inference (#3320)

* WIP

* add reward stats

* const, dont write timers on mobile
/asymm-envs
GitHub 4 年前
当前提交
620fa24a
共有 3 个文件被更改,包括 38 次插入6 次删除
  1. 1
      Project/.gitignore
  2. 9
      com.unity.ml-agents/Runtime/Agent.cs
  3. 34
      com.unity.ml-agents/Runtime/Timer.cs

1
Project/.gitignore


/Assets/AssetStoreTools*
/Assets/Plugins*
/Assets/Demonstrations*
/Assets/ML-Agents/Timers*
/csharp_timers.json
# Environemnt logfile

9
com.unity.ml-agents/Runtime/Agent.cs


// Request the last decision with no callbacks
// We request a decision so Python knows the Agent is done immediately
m_Brain?.RequestDecision(m_Info, sensors);
UpdateRewardStats();
// The Agent is done, so we give it a new episode Id
m_EpisodeId = EpisodeIdCounter.GetEpisodeId();
m_Reward = 0f;

public float GetCumulativeReward()
{
return m_CumulativeReward;
}
void UpdateRewardStats()
{
var gaugeName = $"{m_PolicyFactory.behaviorName}.CumulativeReward";
TimerStack.Instance.SetGauge(gaugeName, GetCumulativeReward());
}
/// <summary>

34
com.unity.ml-agents/Runtime/Timer.cs


using UnityEngine.Profiling;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using UnityEngine.SceneManagement;
namespace MLAgents
{

[DataContract]
public class GaugeNode
{
const float k_SmoothingFactor = .25f; // weight for exponential moving average.
[DataMember]
public float value;
[DataMember(Name = "min")]

[DataMember(Name = "weightedAverage")]
public float weightedAverage;
weightedAverage = value;
minValue = value;
maxValue = value;
count = 1;

{
minValue = Mathf.Min(minValue, newValue);
maxValue = Mathf.Max(maxValue, newValue);
// update exponential moving average
weightedAverage = (k_SmoothingFactor * newValue) + ((1f - k_SmoothingFactor) * weightedAverage);
value = newValue;
++count;
}

/// <param name="filename"></param>
public void SaveJsonTimers(string filename = null)
{
if (filename == null)
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
try
var fullpath = Path.GetFullPath(".");
filename = $"{fullpath}/csharp_timers.json";
if (filename == null)
{
var activeScene = SceneManager.GetActiveScene();
var timerDir = Path.Combine(Application.dataPath, "ML-Agents", "Timers");
Directory.CreateDirectory(timerDir);
filename = Path.Combine(timerDir, $"{activeScene.name}_timers.json");
}
var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
SaveJsonTimers(fs);
fs.Close();
var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
SaveJsonTimers(fs);
fs.Close();
catch (IOException)
{
// It's possible we don't have write access to the directory.
Debug.LogWarning($"Unable to save timers to file {filename}");
}
#endif
}
/// <summary>

正在加载...
取消
保存