public int initializeAgentCalls ;
public int collectObservationsCalls ;
public int collectObservationsCallsSinceLastReset ;
public int collectObservationsCallsForEpisode ;
public int agentActionCallsSinceLastReset ;
public int agentReset Calls ;
public int agentActionCallsForEpisode ;
public int agentOnEpisodeBegin Calls ;
public int heuristicCalls ;
public TestSensor sensor1 ;
public TestSensor sensor2 ;
public override void CollectObservations ( VectorSensor sensor )
{
collectObservationsCalls + = 1 ;
collectObservationsCallsSinceLastReset + = 1 ;
collectObservationsCallsForEpisode + = 1 ;
sensor . AddObservation ( 0f ) ;
}
agentActionCallsSinceLastReset + = 1 ;
agentActionCallsForEpisode + = 1 ;
agentReset Calls + = 1 ;
collectObservationsCallsSinceLastReset = 0 ;
agentActionCallsSinceLastReset = 0 ;
agentOnEpisodeBegin Calls + = 1 ;
collectObservationsCallsForEpisode = 0 ;
agentActionCallsForEpisode = 0 ;
}
public override float [ ] Heuristic ( )
agentGo2 . AddComponent < TestAgent > ( ) ;
var agent2 = agentGo2 . GetComponent < TestAgent > ( ) ;
Assert . AreEqual ( 0 , agent1 . agentReset Calls ) ;
Assert . AreEqual ( 0 , agent2 . agentReset Calls ) ;
Assert . AreEqual ( 0 , agent1 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( 0 , agent2 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( 0 , agent1 . initializeAgentCalls ) ;
Assert . AreEqual ( 0 , agent2 . initializeAgentCalls ) ;
Assert . AreEqual ( 0 , agent1 . agentActionCalls ) ;
// agent1 was not enabled when the academy started
// The agents have been initialized
Assert . AreEqual ( 0 , agent1 . agentReset Calls ) ;
Assert . AreEqual ( 0 , agent2 . agentReset Calls ) ;
Assert . AreEqual ( 0 , agent1 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( 0 , agent2 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( 1 , agent1 . initializeAgentCalls ) ;
Assert . AreEqual ( 1 , agent2 . initializeAgentCalls ) ;
Assert . AreEqual ( 0 , agent1 . agentActionCalls ) ;
agent1 . LazyInitialize ( ) ;
var numberAgent1Reset = 0 ;
var numberAgent1Episodes = 0 ;
var numberAgent2Episodes = 0 ;
Assert . AreEqual ( numberAgent1Reset , agent1 . agentResetCalls ) ;
// Agent2 is never reset since initialized after academy
Assert . AreEqual ( 0 , agent2 . agentReset Calls ) ;
Assert . AreEqual ( numberAgent1Episodes , agent1 . agentOnEpisodeBeginCalls ) ;
Assert . AreEqual ( numberAgent2Episodes , agent2 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( 1 , agent1 . initializeAgentCalls ) ;
Assert . AreEqual ( numberAgent2Initialization , agent2 . initializeAgentCalls ) ;
Assert . AreEqual ( i , agent1 . agentActionCalls ) ;
// Agent 1 resets at the first step
// Agent 1 starts a new episode at the first step
numberAgent1Reset + = 1 ;
numberAgent1Episodes + = 1 ;
// Since Agent2 is initialized after the Academy has stepped, its OnEpisodeBegin should be called now.
Assert . AreEqual ( 0 , agent2 . agentOnEpisodeBeginCalls ) ;
Assert . AreEqual ( 1 , agent2 . agentOnEpisodeBeginCalls ) ;
numberAgent2Episodes + = 1 ;
}
// We are testing request decision and request actions when called
agent2 . LazyInitialize ( ) ;
var numberAgent1Reset = 0 ;
var numberAgent2Reset = 0 ;
var numberAgent1Episodes = 0 ;
var numberAgent2Episodes = 0 ;
var agent2StepSinceReset = 0 ;
var agent2StepForEpisode = 0 ;
for ( var i = 0 ; i < 5 0 0 0 ; i + + )
{
Assert . AreEqual ( acaStepsSinceReset , aca . StepCount ) ;
Assert . AreEqual ( agent2StepSinceReset , agent2 . StepCount ) ;
Assert . AreEqual ( numberAgent1Reset , agent1 . agentResetCalls ) ;
Assert . AreEqual ( numberAgent2Reset , agent2 . agentResetCalls ) ;
Assert . AreEqual ( numberAgent2Episodes , agent2 . agentOnEpisodeBeginCalls ) ;
Assert . AreEqual ( agent2StepForEpisode , agent2 . StepCount ) ;
// Agent 2 and academy reset at the first step
// Agent 2 and academy reset at the first step
Assert . AreEqual ( numberAgent2Episodes , agent2 . agentOnEpisodeBeginCalls ) ;
numberAgent2Reset + = 1 ;
numberAgent2Episodes + = 1 ;
Assert . AreEqual ( numberAgent1Episodes , agent1 . agentOnEpisodeBeginCalls ) ;
numberAgent1Episodes + = 1 ;
Assert . AreEqual ( numberAgent1Episodes , agent1 . agentOnEpisodeBeginCalls ) ;
Assert . AreEqual ( numberAgent1Episodes , agent1 . agentOnEpisodeBeginCalls ) ;
numberAgent1Reset + = 1 ;
numberAgent1Episodes + = 1 ;
Assert . AreEqual ( numberAgent1Episodes , agent1 . agentOnEpisodeBeginCalls ) ;
// Resetting agent 2 regularly
// Ending the episode for agent 2 regularly
Assert . AreEqual ( numberAgent2Episodes , agent2 . agentOnEpisodeBeginCalls ) ;
numberAgent2Reset + = 1 ;
agent2StepSinceReset = 0 ;
numberAgent2Episodes + = 1 ;
agent2StepForEpisode = 0 ;
Assert . AreEqual ( numberAgent2Episodes , agent2 . agentOnEpisodeBeginCalls ) ;
}
// Request a decision for agent 2 regularly
if ( i % 3 = = 2 )
}
acaStepsSinceReset + = 1 ;
agent2StepSinceReset + = 1 ;
agent2StepForEpisode + = 1 ;
aca . EnvironmentStep ( ) ;
}
}
agent1 . LazyInitialize ( ) ;
agent2 . SetPolicy ( new TestPolicy ( ) ) ;
var expectedAgent1ActionSinceReset = 0 ;
var expectedAgent1ActionForEpisode = 0 ;
expectedAgent1ActionSinceReset + = 1 ;
if ( expectedAgent1ActionSinceReset = = agent1 . maxStep | | i = = 0 )
expectedAgent1ActionForEpisode + = 1 ;
if ( expectedAgent1ActionForEpisode = = agent1 . maxStep | | i = = 0 )
expectedAgent1ActionSinceReset = 0 ;
expectedAgent1ActionForEpisode = 0 ;
Assert . LessOrEqual ( Mathf . Abs ( expectedAgent1ActionSinceReset * 1 0.1f - agent1 . GetCumulativeReward ( ) ) , 0.05f ) ;
Assert . LessOrEqual ( Mathf . Abs ( expectedAgent1ActionForEpisode * 1 0.1f - agent1 . GetCumulativeReward ( ) ) , 0.05f ) ;
Assert . LessOrEqual ( Mathf . Abs ( i * 0.1f - agent2 . GetCumulativeReward ( ) ) , 0.05f ) ;
agent1 . AddReward ( 1 0f ) ;
agent1 . LazyInitialize ( ) ;
var expectedAgentStepCount = 0 ;
var expectedReset s = 0 ;
var expectedEpisode s = 0 ;
var expectedAgentActionSinceReset = 0 ;
var expectedAgentActionForEpisode = 0 ;
var expectedCollectObsCallsSinceReset = 0 ;
var expectedCollectObsCallsForEpisode = 0 ;
expectedAgentActionSinceReset + = 1 ;
expectedAgentActionForEpisode + = 1 ;
expectedCollectObsCallsSinceReset + = 1 ;
expectedCollectObsCallsForEpisode + = 1 ;
expectedReset s + = 1 ;
expectedEpisode s + = 1 ;
expectedAgentActionSinceReset = 0 ;
expectedCollectObsCallsSinceReset = 0 ;
expectedAgentActionForEpisode = 0 ;
expectedCollectObsCallsForEpisode = 0 ;
Assert . AreEqual ( expectedReset s , agent1 . agentReset Calls ) ;
Assert . AreEqual ( expectedEpisode s , agent1 . agentOnEpisodeBegin Calls ) ;
Assert . AreEqual ( expectedAgentActionSinceReset , agent1 . agentActionCallsSinceLastReset ) ;
Assert . AreEqual ( expectedAgentActionForEpisode , agent1 . agentActionCallsForEpisode ) ;
Assert . AreEqual ( expectedCollectObsCallsSinceReset , agent1 . collectObservationsCallsSinceLastReset ) ;
Assert . AreEqual ( expectedCollectObsCallsForEpisode , agent1 . collectObservationsCallsForEpisode ) ;
}
}