I’ve been playing around with learning agents in C++ and I have hit a wall when trying to add my own arrays of observations.
For context, I am trying to send a series of raycasts out of the front of the vehicle.
I have two approaches. The first is a pointer array
.h
UFloatObservation* SweepCollisionObservation;
UFloatObservation** SweepCollisionObservations;
.cpp SetupObservations_Implementation() function
// Sweep collision detection
int32 numArray = 12;
//TrackCollisionLookAheadObservation = UFloatArrayObservation::AddFloatArrayObservation(this, "TrackCollisionLookAheadObservation", numArray, 1.0f);
SweepCollisionObservation = UFloatObservation::AddFloatObservation(this, "SweepCollisionObservation");
SweepCollisionObservations = new UFloatObservation*[numArray];
for (int i = 0; i < numArray; i++)
{
SweepCollisionObservations[i] = UFloatObservation::AddFloatObservation(this, FName(*FString::Printf(TEXT("SweepCollisionObservation_%d"), i)));
}
then the implementation is here
.cpp SetObservations_Implementation
for (int i = 0; i < 12; i++)
{
if (CarAgent->PotentialCollisionRayCast(velocity.Size(), (i - 6) * 10, ActorsToIgnore)) {
SweepCollisionObservations[i]->SetFloatObservation(AgentId, 1.0f);
}
else {
SweepCollisionObservations[i]->SetFloatObservation(AgentId, 0.0f);
}
}
For the first approach, I get the following warnings (for each agent)
LogLearning: Warning: Agent Interactor: Observation SweepCollisionObservation_97 for agent with id 0 has not been set (got iteration 0, expected iteration 1) and so agent will not have observations encoded.
LogLearning: Warning: Agent Policy: Agent with id 0 has not made observations so policy will not be evaluated for it
LogLearning: Warning: Agent Interactor: Agent with id 0 does not have an encoded action vector so actions will not be decoded for it. Was EvaluatePolicy or EncodeActions run?
and the second approach is a
.h
UFloatArrayObservations* TrackCollisionLookAheadObservation;
.cpp SetupObservations_Implementation()
TrackCollisionLookAheadObservation = UFloatArrayObservation::AddFloatArrayObservation(this, "TrackCollisionLookAheadObservation", numArray, 1.0f);
.cpp SetObservations_Implementation
// Declare a TArray to store float values
TArray<float> CollisionSweepFloatChecks;
for (int i = 0; i < 12; i++)
{
if (CarAgent->PotentialCollisionRayCast(velocity.Size(), (i - 6) * 10, ActorsToIgnore)) {
CollisionSweepFloatChecks.Add(1.0f);
}
else {
CollisionSweepFloatChecks.Add(0.0f);
}
}
TrackCollisionLookAheadObservation->SetFloatArrayObservation(AgentId, CollisionSweepFloatChecks);
I cannot seem to get this approach to even compile. Is there an example implementation of using the ‘ArrayObservation’ in C++?
There is no missing ‘;’ on line 56. I believe I am initializing the array incorrectly.
CompilerResultsLog: Error: C:\Users\lee-k\Documents\Unreal Projects\DriveAgentsCPP\Source\DriveAgentsCPP\LearningAgents\AutonomousVehicleInteractor.h(56) : error C2143: syntax error: missing ‘;’ before ‘*’
CompilerResultsLog: Error: C:\Users\lee-k\Documents\Unreal Projects\DriveAgentsCPP\Source\DriveAgentsCPP\LearningAgents\AutonomousVehicleInteractor.h(56) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
CompilerResultsLog: Error: C:\Users\lee-k\Documents\Unreal Projects\DriveAgentsCPP\Source\DriveAgentsCPP\LearningAgents\AutonomousVehicleInteractor.h(56) : error C2238: unexpected token(s) preceding ‘;’
Anyone else had success doing this?