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

126 行
3.2 KiB

using UnityEngine;
using Unity.MLAgentsExamples;
public class CubeWarArea : Area
{
[HideInInspector]
public SmallCubeAgent[] smallAgents;
[HideInInspector]
public LargeCubeAgent[] largeAgents;
public float range;
private CubeWarsTeamManager m_SmallTeamManager;
private CubeWarsTeamManager m_LargeTeamManager;
[Header("Max Environment Steps")] public int MaxEnvironmentSteps = 10000;
private int m_ResetTimer = 0;
void Start()
{
m_SmallTeamManager = new CubeWarsTeamManager();
m_LargeTeamManager = new CubeWarsTeamManager();
range = 1.0f;
smallAgents = GetComponentsInChildren<SmallCubeAgent>();
largeAgents = GetComponentsInChildren<LargeCubeAgent>();
foreach (var agent in smallAgents)
{
agent.SetTeamManager(m_SmallTeamManager);
}
foreach (var agent in largeAgents)
{
agent.SetTeamManager(m_LargeTeamManager);
}
}
void FixedUpdate()
{
m_ResetTimer += 1;
if (m_ResetTimer > MaxEnvironmentSteps)
{
ResetAllAgents(true);
m_ResetTimer = 0;
}
}
public void ResetAllAgents(bool terminated = false)
{
foreach (var smallAgent in smallAgents)
{
if (terminated)
{
smallAgent.EpisodeInterrupted();
}
else
{
smallAgent.EndEpisode();
}
smallAgent.gameObject.SetActive(true);
}
foreach (var largeAgent in largeAgents)
{
if (terminated)
{
largeAgent.EpisodeInterrupted();
}
else
{
largeAgent.EndEpisode();
}
}
}
public bool AgentDied()
{
bool smallAlive = false;
int numLiveAgents = 0;
foreach (var smallAgent in smallAgents)
{
if (!smallAgent.IsDead())
{
smallAlive = true;
numLiveAgents++;
}
}
bool largeAlive = false;
foreach (var largeAgent in largeAgents)
{
if (!largeAgent.IsDead())
{
largeAlive = true;
}
}
if (!smallAlive)
{
Debug.Log("Big Agent Wins");
foreach (var smallAgent in smallAgents)
{
if (smallAgent.gameObject.activeInHierarchy)
{
smallAgent.SetReward(-1.0f);
}
}
foreach (var largeAgent in largeAgents)
{
largeAgent.SetReward(1.0f);
}
ResetAllAgents();
}
else if (!largeAlive)
{
Debug.Log("Small Agents Win");
foreach (var smallAgent in smallAgents)
{
if (smallAgent.gameObject.activeInHierarchy)
{
smallAgent.SetReward(1.0f / numLiveAgents);
}
}
foreach (var largeAgent in largeAgents)
{
largeAgent.SetReward(-1.0f);
}
ResetAllAgents();
}
return smallAlive && largeAlive;
}
}