浏览代码

[Fix] Fix the unit tests in C# (#394)

* [Fix] Fix the unit tests in C# since the academy now resets in the first fixed update and not the awake method in inference

* [Fix] Adding comments to the unit tests

* [Comments] Improving the comments
/develop-generalizationTraining-TrainerController
GitHub 7 年前
当前提交
9cd1666d
共有 1 个文件被更改,包括 141 次插入64 次删除
  1. 205
      unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs

205
unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs


}
}
// This is an empty class for testing the behavior of agents and academy
// It is left empty because we are not testing any brain behavior
// TODO : Mock a brain
}

BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
Assert.AreEqual(1, aca.initializeAcademyCalls);
Assert.AreEqual(1, aca.episodeCount);
Assert.AreEqual(0, aca.episodeCount);
Assert.AreEqual(1, aca.academyResetCalls);
Assert.AreEqual(0, aca.academyResetCalls);
Assert.AreEqual(0, aca.AcademyStepCalls);
}

Assert.AreEqual(false, agent1.IsDone());
Assert.AreEqual(false, agent2.IsDone());
//agent1 was not enabled when the academy started
// agent1 was not enabled when the academy started
// The agents have been initialized
Assert.AreEqual(1, agent2.agentResetCalls);
Assert.AreEqual(0, agent2.agentResetCalls);
Assert.AreEqual(1, agent1.initializeAgentCalls);
Assert.AreEqual(1, agent2.initializeAgentCalls);
Assert.AreEqual(0, agent1.agentActionCalls);

MethodInfo AcademyStepMethod = typeof(Academy).GetMethod("_AcademyStep",
BindingFlags.Instance | BindingFlags.NonPublic);
int numberReset = 0;
Assert.AreEqual(1, aca.episodeCount);
Assert.AreEqual(numberReset, aca.episodeCount);
Assert.AreEqual(1, aca.academyResetCalls);
Assert.AreEqual(numberReset, aca.academyResetCalls);
// The reset happens at the begining of the first step
if (i == 0)
{
numberReset += 1;
}
}
}

AgentEnableMethod.Invoke(agent1, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AgentEnableMethod.Invoke(agent2, new object[] { aca });
int numberAgent1Reset = 0;
int numberAgent2Initialization = 0;
Assert.AreEqual(1, agent1.agentResetCalls);
Assert.AreEqual(0, agent2.agentResetCalls);
Assert.AreEqual(numberAgent1Reset, agent1.agentResetCalls);
// Agent2 is never reset since intialized after academy
Assert.AreEqual(0, agent2.agentResetCalls);
Assert.AreEqual(1, agent2.initializeAgentCalls);
Assert.AreEqual(numberAgent2Initialization, agent2.initializeAgentCalls);
if (i % 3 == 0)
// Agent 1 resets at the first step
if (i == 0)
numberAgent1Reset += 1;
}
//Agent 2 is only initialized at step 2
if (i == 2)
{
AgentEnableMethod.Invoke(agent2, new object[] { aca });
numberAgent2Initialization += 1;
}
// We are testing request decision and request actions when called
// at different intervals
if ((i % 3 == 0) && (i > 2))
{
//Every 3 steps after agent 2 is initialized, request decision
else if (i % 5 == 0)
else if ((i % 5 == 0) && (i > 2))
// Every 5 steps after agent 2 is initialized, request action
requestAction += 1;
agent2.RequestAction();
}

MethodInfo AcademyStepMethod = typeof(Academy).GetMethod("_AcademyStep",
BindingFlags.Instance | BindingFlags.NonPublic);
int numberReset = 1;
int numberReset = 0;
int stepsSinceReset = 0;
for (int i = 0; i < 50; i++)
{

Assert.AreEqual(false, aca.IsDone());
Assert.AreEqual(numberReset, aca.academyResetCalls);
Assert.AreEqual(i, aca.AcademyStepCalls);
// Academy resets at the first step
if (i == 0)
{
numberReset += 1;
}
if (i % 5 == 3)
// Regularly set the academy to done to check behavior
if (i % 5 == 3)
{
aca.Done();
numberReset += 1;

AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AgentEnableMethod.Invoke(agent1, new object[] { aca });
int numberAgent1Reset = 0; // Agent1 was not enabled at Academy start
int numberAgent2Reset = 1;
int numberAcaReset = 1;
int numberAgent1Reset = 0;
int numberAgent2Reset = 0;
int numberAcaReset = 0;
int acaStepsSinceReset = 0;
int agent1StepSinceReset =0;
int agent2StepSinceReset=0;

Assert.AreEqual(numberAgent1Reset, agent1.agentResetCalls);
Assert.AreEqual(numberAgent2Reset, agent2.agentResetCalls);
acaStepsSinceReset += 1;
agent1StepSinceReset += 1;
agent2StepSinceReset += 1;
// Agent 2 and academy reset at the first step
if (i == 0)
{
numberAcaReset += 1;
numberAgent2Reset += 1;
}
//Agent 1 is only initialized at step 2
if (i == 2)
{
AgentEnableMethod.Invoke(agent1, new object[] { aca });
if (i % 100 == 3)
}
// Reset Academy every 100 steps
if (i % 100 == 3)
acaStepsSinceReset = 1;
acaStepsSinceReset = 0;
if (i % 11 == 5)
// Set agent 1 to done every 11 steps to test behavior
if (i % 11 == 5)
if (i % 13 == 3)
// Reseting agent 2 regularly
if (i % 13 == 3)
{
if (!(agent2.IsDone()||aca.IsDone()))
{

numberAgent2Reset += 1;
agent2StepSinceReset = 1;
agent2StepSinceReset = 0;
if (i % 3 == 2)
// Request a decision for agent 2 regularly
if (i % 3 == 2)
else if (i % 5 == 1)
else if (i % 5 == 1)
// Request an action without decision regularly
if (agent1.IsDone() && (((acaStepsSinceReset+1) % agent1.agentParameters.numberOfActionsBetweenDecisions==0)) || aca.IsDone())
if (agent1.IsDone() && (((acaStepsSinceReset) % agent1.agentParameters.numberOfActionsBetweenDecisions==0)) || aca.IsDone())
agent1StepSinceReset = 1;
agent1StepSinceReset = 0;
agent2StepSinceReset = 1;
agent2StepSinceReset = 0;
acaStepsSinceReset += 1;
agent1StepSinceReset += 1;
agent2StepSinceReset += 1;
//Agent 1 is only initialized at step 2
if (i < 2)
{
agent1StepSinceReset = 0;
}
AcademyStepMethod.Invoke((object)aca, new object[] { });

FieldInfo maxStep = typeof(Academy).GetField("maxSteps", BindingFlags.Instance | BindingFlags.NonPublic);
maxStep.SetValue((object)aca, 20);
int numberReset = 1;
int numberReset = 0;
Assert.AreEqual(false, aca.IsDone());
Assert.AreEqual(i, aca.AcademyStepCalls);
Assert.AreEqual(false, aca.IsDone());
Assert.AreEqual(i, aca.AcademyStepCalls);
if ((i % 20 == 0) && (i>0))
// Make sure max step is reached every 20 steps
if (i % 20 == 0)
{
numberReset += 1;
stepsSinceReset = 1;

AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AgentEnableMethod.Invoke(agent1, new object[] { aca });
int numberAgent1Reset = 0; // Agent1 was not enabled at Academy start
int numberAgent2Reset = 1;
int numberAcaReset = 1;
int numberAgent1Reset = 0;
int numberAgent2Reset = 0;
int numberAcaReset = 0;
int acaStepsSinceReset = 0;
int agent1StepSinceReset = 0;
int agent2StepSinceReset = 0;

Assert.AreEqual(acaStepsSinceReset, aca.stepsSinceReset);
Assert.AreEqual(1, aca.initializeAcademyCalls);
Assert.AreEqual(numberAcaReset, aca.episodeCount);
Assert.AreEqual(numberAcaReset, aca.academyResetCalls);
Assert.AreEqual(numberAcaReset, aca.episodeCount);
Assert.AreEqual(numberAcaReset, aca.academyResetCalls);
agent2.RequestDecision(); // we request a decision at each step
acaStepsSinceReset += 1;
agent1StepSinceReset += 1;
agent2StepSinceReset += 1;
//At the first step, Academy and agent 2 reset
if (i == 0)
{
numberAcaReset += 1;
numberAgent2Reset += 1;
}
//Agent 1 is only initialized at step 2
if (i == 2)
{
AgentEnableMethod.Invoke(agent1, new object[] { aca });
}
// we request a decision at each step
agent2.RequestDecision();
if (i % 100 == 0)
// Make sure the academy max steps at 100
if (i % 100 == 0)
acaStepsSinceReset = 1;
agent1StepSinceReset = 1;
agent2StepSinceReset = 1;
acaStepsSinceReset = 0;
agent1StepSinceReset = 0;
agent2StepSinceReset = 0;
numberAcaReset += 1;
numberAgent1Reset += 1;
numberAgent2Reset += 1;

if ((i % 100) % 21 == 0)
//Make sure the agents reset when their max steps is reached
if (agent1StepSinceReset % 21 == 0)
agent1StepSinceReset = 1;
agent1StepSinceReset = 0;
if ((i % 100) % 31 == 0)
if (agent2StepSinceReset % 31 == 0)
agent2StepSinceReset = 1;
agent2StepSinceReset = 0;
acaStepsSinceReset += 1;
agent1StepSinceReset += 1;
agent2StepSinceReset += 1;
//Agent 1 is only initialized at step 2
if (i < 2)
{
agent1StepSinceReset = 0;
}
}
}

brain.brainParameters = new BrainParameters();
// We use event based so the agent will now try to send anything to the brain
agent1.agentParameters.onDemandDecision = false;
// agent1 will take an action at every step and request a decision every steps
// agent1 will take an action at every step and request a decision every 2 steps
agent2.agentParameters.onDemandDecision = true;
agent2.agentParameters.onDemandDecision = true;
//Here we specify that the agent does not reset when done
agent2.agentParameters.resetOnDone = false; //Here we specify that the agent does not reset when done
agent2.agentParameters.resetOnDone = false;
brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new resolution[0];
agent1.GiveBrain(brain);

Assert.AreEqual(agent1ResetOnDone, agent1.agentOnDoneCalls);
Assert.AreEqual(agent2ResetOnDone, agent2.agentOnDoneCalls);
agent2.RequestDecision(); // we request a decision at each step
// we request a decision at each step
agent2.RequestDecision();
acaStepsSinceReset += 1;
if (agent1ResetOnDone ==0)
agent1StepSinceReset += 1;

正在加载...
取消
保存