Playing with Embedded

by Stéphane Maruéjouls | Sep 15, 2015 | Tech | AI  Behavior Trees  Connected Device  IoT  Smart Home  Virtual Assistant 

Playing with Embedded

As you have read in our latest BT 101 blog article, craft ai provides you with a variety of nodes to design the Behavior Trees. To go further in the BT’s expressiveness , and to ease the design and the management of your BTs, we added a new powerful node: the ‘Embedded Behavior Node’, that we often shorten to embedded node or just embedded.

What is it?

The embedded node is a special node in our BT grammar that allows you to integrate a subtree inside another BT. Any BT can be part of another BT. The embedded node will have the same status (ie running, succeeded, failed ) as the root of subtree it refers to.

What is it for?

Work isolation

One of the direct benefit of an embedded node is that it will allow you to isolate your BTs and manage them in a more convenient way. Like in classical programming paradigm, splitting your BTs will enable a better organization of your application.

Imagine you want to create a smart assistant that will bring you some weather information, gathering them from some web service that need authentication. A typical BT of such an assistant created with craft ai will have 3 main phases:

  • initialization (setting up some Knowledge base value, doing some authentication to other web services, etc…),
  • main execution (main function of the BT),
  • termination (cleaning up resources, etc..),

which are basically set up under a sequence.

A good use of embedded in this case is to split those 3 phases in 3 different BTs.

Splitting BT in 3 parts

Moreover, as the main BT is split, more people can work on various subtrees, allowing a better use of your team time.

Reusability

Continuing with programming language analogy, having subtrees will allow you to reuse BTs in different places, either in the same BT or in other BTs, just as you create functions in your code. As soon as you identify a pattern you are likely to reuse, it is just better to create it in a dedicated subtree. This way you’ll save time, changes or bug fixing in the pattern will have to be made in only one place.

Parametrization

As with functions, it would be quite limiting if you could not parametrize the call. Fortunately, with the embedded node you can pass parameters to your subtrees. Those parameters will be available to your subtrees node.

Static vs Dynamic

One of the true feature of the embedded implementation in craft ai is that the name of the subtree to execute can either be a plain string value (static, known at edition time) or be a value from any of the blackboards (dynamic, known at runtime). This open new possibilities. As with the programing metaphor, it is not just a function call, but a pointer to function or delegate that can be changed during execution.

Now, we can have fun with Embedded!

Smart Objects

Smart Objects have been introduced by Thalmann & al in 1998, the concept was successfully applied in Video Game in The Sims from Maxis around 2000. The basic premise is to, instead of storing all the AI in the Non Playable Character (NPC), the system hold all the AI the NPC need to use when interacting with an object in this object. Then the system inject the behavior into the NPC. Such AI system could be easily be build with craft ai and the embedded node.

With craft ai, you can easily create the same approach with the embedded node. Your NPC can have a general AI to wander and select object, the selected object can then just hold the subtree to be executed by the NPC using the embedded node. Smart Object behavior

A similar AI can be built for an IoT application, imagine an app on your smartphone that when located next to a connected device will react accordingly. The app only needs to have the general BT and a way to retrieve the ‘use object’ BT (by wifi, bluetooth, NFC, etc.) from the object to behave correctly.

The main benefit will be that the subtree can be created after the application has shipped. You end up with an application that can be shipped and still be ready to accept future devices yet to be created!

Recursion

As with functions in a programming language, recursion can be made with embedded. It is a funny side effect we did not think of when designing the grammar, but it proved to be quite useful in some interesting cases.

For example, if you need to access a WebService that provides pagination in its reply (reddit for example), you can create a simple subtree that gets the nth page (in parameters) and if there is a n+1 page, recurse by using the same subtree.

Chuck Norris

But be aware, as with any recursion system, this can lead to the infinite recursion bug. You might be great, but not Chuck Norris great.

Conclusion

Here at craft ai, we like to take care of our developers. By enhancing the BT grammar, hence allowing them to better and faster organize their behaviors, we help them creating smarter applications and services.

Have fun (and don’t forget to join the beta if you haven’t yet)!

Share