Setting up C++ observations

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?

1 Like

The error is in the file “AutonomousVehicleInteractor.h”
in the line 56
an ';" is missing

1 Like

Hi Ivan3z

I think there may be an issue with how I am declaring the array. I do not think it is as simple as a missing ‘;’

If I comment out the array declaration (line 56) and the uses in the .cpp then it compiles fine.

The line 56

UFloatArrayObservation* TrackCollisionLookAheadObservation;

Sometimes (very oftem) the compiler says one thing and then the problem is other thing.
So is you don´t kwow what is happen the microsoft code error webpage can help.

What i see here?

i think the compiler has not idea of what this typename is.
UFloatArrayObservation

Probably an include is missing. This can happen when you program works with DLL sometimes you make changes and the DLL is not updated.


Find in what include file is UFloatArrayObservation declared and add it in the header.

Probably it is called like this “FloatArrayObservation.h”


By the way… when you create and dynamic array in pure C++ is something like this.

int *MyArray = new int[10];

So this really is not an array → UFloatArrayObservation; This must be a array wrapper

And this a siple pointer declaration → UFloatArrayObservation* TrackCollisionLookAheadObservation;

Setting up C++ observations typically involves designing and implementing data structures or code segments to gather and record specific information or events in a C++ program for analysis or debugging purposes.

Hey, thanks for your help.

After a couple of days away your comment helped me see the problem instantly! I will include the solution here in case anyone else misses this. I believe you are correct in that it is acting as an array wrapper.

For reference, I have been using the Unreal Learning Agents plugin.

I had already included the relevant header files but I needed to put the following under the #includes.

class UFloatArrayObservation;

1 Like

This help to avoid circular dependences if you declare like this in your .h file and add the include in your cpp file.

i glad to help you!!
Best Regards!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.