# How to use a behavior tree based AI (or player controlled pawn) to generate a recording for imitation learning

**URL:** <https://forums.unrealengine.com/t/how-to-use-a-behavior-tree-based-ai-or-player-controlled-pawn-to-generate-a-recording-for-imitation-learning/1296483>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, Imitation-Learning, question\
**Created:** [September 15, 2023, 3:35am UTC](https://forums.unrealengine.com/t/how-to-use-a-behavior-tree-based-ai-or-player-controlled-pawn-to-generate-a-recording-for-imitation-learning/1296483 "2023-09-15T03:35:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirrorclimb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mirrorclimb](https://forums.unrealengine.com/u/mirrorclimb)\
**Post date:** [September 15, 2023, 3:35am UTC](https://forums.unrealengine.com/t/how-to-use-a-behavior-tree-based-ai-or-player-controlled-pawn-to-generate-a-recording-for-imitation-learning/1296483/1 "2023-09-15T03:35:14Z")

</div>

I have followed the tutorial on reinforcement learning and have it working decently in my game in a simple use case, but for more complex use cases I have having issues with the AI learning very slowly, or getting stuck in a local maximum.

In some of these cases, I think starting with some initial imitation learning would drastically accelerate the process and I have behavior tree based AIs that can execute portions of the behavior I am trying to have my learning agents do. I would like to use the behavior tree based AIs (or potentially a player controlled pawn) to record data for imitiation learning.

I am trying to set up the recording like this (here the agent being added is a pawn controlled by a behavior tree):

> **[learning agents recorder setup posted by anonymous | blueprintUE | PasteBin...](https://blueprintue.com/render/mifiypo8/)**
>
> No description provided

then once my actors are all initialized and ready for training I am doing this

> **[Begin Imitation Record posted by anonymous | blueprintUE | PasteBin For...](https://blueprintue.com/blueprint/wtfjy_hy/)**
>
> No description provided

However, I am not clear on what the learning manager class on tick should look like.

I can do this

> **[learning manager recording tick posted by anonymous | blueprintUE | PasteBin...](https://blueprintue.com/blueprint/rq7qa6dp/)**
>
> No description provided

If I print the output of Get Observation Vector everything looks good and it appears to be correctly gathering observations from the agent that is controlled by a behavior tree.

I then added code to call “setFloatAction” for each of the interactors actions and used game data to calculate a float action that is equivalent to what the behavior tree decided to do (such that if a learning agent called getActions with the float actions set to those values then it would do exactly what the behavior tree controlled agent did). I can also print those float actions values and they look proper and match what the behavior tree is doing.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/e/f0edcc07a7ea87ec86f77e5b5bacba6f629552ed.png)

I believe the next step _should_ be to call Add Experience like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/7/5/e75b0008a68c8bf35ac0112d61e256e15d95a779.png)

However, when I make that call I get console errors like this:

`LogLearning: Warning: BP_FighterRecorder: Agent with id 0 has not made observations and taken actions so experience will not be recorded for it.`

If I try to call “getActionVector” I get a related (I think) error

```auto
LogLearning: Error: BP_FighterInteractor: Agent with id 0 has not yet computed an action vector. Have you run EvaluatePolicy or EncodeActions?

```

However, as far as I can tell there is no EncodeActions function and I don’t think calling EvaluatePolicy makes sense (there is no policy, I am just trying to record…

Any help is greatly appreciated, thanks!!!

---

<div class="post-metadata">

**Author:** ![mirrorclimb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mirrorclimb](https://forums.unrealengine.com/u/mirrorclimb)\
**Post date:** [September 15, 2023, 4:48am UTC](https://forums.unrealengine.com/t/how-to-use-a-behavior-tree-based-ai-or-player-controlled-pawn-to-generate-a-recording-for-imitation-learning/1296483/2 "2023-09-15T04:48:11Z")

</div>

Hah I figured out my own problem, but I’ve leave this up incase others are confused by it in the future.

You need to also include a LearningAgentsController component which you can then setup like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/b/4/ab4fa210f3ac575c7437eca3fd79e2523142ee49.png)

The controller is then the object that lets you call EncodeActions

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/6/8/668941c2085fcf0658d69cfd6c2999042fc968c2.png)

For some reason I had expected that EncodeActions and EncodeObservations would both be on the interactor.

With that addition the AddExperience function no longer gives errors and the recording works as expected:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/a/7/fa7d80cfaf22325ef25bcf07d4a1917bbcc1a6bf.png)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [October 15, 2023, 4:48am UTC](https://forums.unrealengine.com/t/how-to-use-a-behavior-tree-based-ai-or-player-controlled-pawn-to-generate-a-recording-for-imitation-learning/1296483/3 "2023-10-15T04:48:14Z")

</div>

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