Unity 机器学习代理工具包 (ML-Agents) 是一个开源项目,它使游戏和模拟能够作为训练智能代理的环境。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

85 行
2.9 KiB

using UnityEngine;
using System.Collections.Generic;
namespace MLAgents
{
/// <summary>
/// The Remote Policy only works when training.
/// When training your Agents, the RemotePolicy will be controlled by Python.
/// </summary>
public class RemotePolicy : IPolicy
{
string m_BehaviorName;
protected ICommunicator m_Communicator;
/// <summary>
/// Sensor shapes for the associated Agents. All Agents must have the same shapes for their Sensors.
/// </summary>
List<int[]> m_SensorShapes;
/// <inheritdoc />
public RemotePolicy(
BrainParameters brainParameters,
string behaviorName)
{
m_BehaviorName = behaviorName;
var aca = Object.FindObjectOfType<Academy>();
aca.LazyInitialization();
m_Communicator = aca.Communicator;
aca.Communicator.SubscribeBrain(m_BehaviorName, brainParameters);
}
/// <inheritdoc />
public void RequestDecision(Agent agent)
{
#if DEBUG
ValidateAgentSensorShapes(agent);
#endif
m_Communicator?.PutObservations(m_BehaviorName, agent);
}
/// <inheritdoc />
public void DecideAction()
{
m_Communicator?.DecideBatch();
}
/// <summary>
/// Check that the Agent Sensors are the same shape as the the other Agents using the same Brain.
/// If this is the first Agent being checked, its Sensor sizes will be saved.
/// </summary>
/// <param name="agent">The Agent to check</param>
void ValidateAgentSensorShapes(Agent agent)
{
if (m_SensorShapes == null)
{
m_SensorShapes = new List<int[]>(agent.sensors.Count);
// First agent, save the sensor sizes
foreach (var sensor in agent.sensors)
{
m_SensorShapes.Add(sensor.GetFloatObservationShape());
}
}
else
{
// Check for compatibility with the other Agents' Sensors
// TODO make sure this only checks once per agent
Debug.Assert(m_SensorShapes.Count == agent.sensors.Count, $"Number of Sensors must match. {m_SensorShapes.Count} != {agent.sensors.Count}");
for (var i = 0; i < m_SensorShapes.Count; i++)
{
var cachedShape = m_SensorShapes[i];
var sensorShape = agent.sensors[i].GetFloatObservationShape();
Debug.Assert(cachedShape.Length == sensorShape.Length, "Sensor dimensions must match.");
for (var j = 0; j < cachedShape.Length; j++)
{
Debug.Assert(cachedShape[j] == sensorShape[j], "Sensor sizes much match.");
}
}
}
}
public void Dispose()
{
}
}
}