|
|
|
|
|
|
using System.Linq; |
|
|
|
using Unity.MLAgents.Extensions.Teams; |
|
|
|
using Unity.MLAgents.Sensors; |
|
|
|
using System; |
|
|
|
Dictionary<Agent, bool> m_AgentDone = new Dictionary<Agent, bool> { }; |
|
|
|
float m_RewardDiscount = 1; |
|
|
|
|
|
|
|
// -1 means not done
|
|
|
|
Dictionary<Agent, int> m_AgentDoneStep = new Dictionary<Agent, int> { }; |
|
|
|
m_AgentDone[agent] = false; |
|
|
|
m_AgentDoneStep[agent] = -1; |
|
|
|
m_AgentDone[agent] = true; |
|
|
|
m_AgentDoneStep[agent] = agent.StepCount; |
|
|
|
foreach (var agent in m_AgentDone.Keys.ToList()) |
|
|
|
foreach (var agent in m_AgentDoneStep.Keys.ToList()) |
|
|
|
if (m_AgentDone[agent]) |
|
|
|
if (m_AgentDoneStep[agent] >= 0) |
|
|
|
m_AgentDone[agent] = false; |
|
|
|
m_AgentDoneStep[agent] = -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
foreach (var agent in m_AgentDone.Keys) |
|
|
|
int maxAgentStep = m_AgentDoneStep.Values.Max(); |
|
|
|
foreach (var agent in m_AgentDoneStep.Keys) |
|
|
|
if (m_AgentDone[agent]) |
|
|
|
if (m_AgentDoneStep[agent] >= 0) |
|
|
|
agent.AddRewardAfterDeath(reward); |
|
|
|
agent.AddRewardAfterDeath(reward * (float)Math.Pow(m_RewardDiscount, maxAgentStep - m_AgentDoneStep[agent])); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void SetRewardDiscount(float discountRate) |
|
|
|
{ |
|
|
|
m_RewardDiscount = discountRate; |
|
|
|
} |
|
|
|
} |