Multithreading approach for many AI

Hi guillaumevds,

This will not be easy if you don’t have a nice multithreading approach first. The keyword is “interactive” and “reactive”.

Abstract first,

By interactive, you want your game, say graphics and sounds display while your characters AI computing its calculation and later return results (path finding, stragegy). Let see this like AI computing unit are sending load of data into graphics and sound pipelines. Like you playing a movie in your media player.

Now let say you did organize your AI into “agents”, and “unit of works” that may let you computing their “costs”. There is also a “budget” of time that you allow AI framework to take each frame and in a span of 30 frames. You will now see the problem is to anwser: “how to control the amount of data and how much data that I send to graphics pipeline, to balance with time I compute the data and time I play the data arcordingly.”

To this point, balance need profiling (a lot of profiling and tweaking of course), but in general, you should count on some framework to make your life easier. I really recommend you to take a look at RxJava and the way they implements their pipeline controllers.

In my framework, I used RxJava and structure my AI computing cost into “agents” - “actor”, “unit of work”, “layers” and “data stream - data pack”. In general, those concepts allow me to wrap my AI algorithm with any orders, any parallel mechanism I wish to… That’s a big win.

[Edit after re-reading] Above sentences means: I can send data between actors, between layers, push them all into a pipe and warranty the observers will receive the signals after some ticks. The streaming continues in pipelines that transparent to developer, and they have thinks of the “static overall order” carefully, ask themself if there is a reason to use it at all. As my experience after re structing, there is no point to use a static order in almost game instead using independent orders of streams… So, AI don’t have to wait for graphics, graphics don’t have to wait AI, main character don’t have to wait for enemy, enemy also don’t have to wait… They are independent agents and just work with “Signals”!

To corporate with AI in level design, our level designer have to put “helper” around the map. This helper signal useful hints that may broadcast to agents around that point. You can see navmesh, obstacles, tactic point are these kind of helper and they send signals all the time. As I write a FPS for example, the bugdet is really depend in where my main character is standing. This kind of balancing along with concern about Level of detail make you bring down some kind of “filter”. And thank to RxJava, there is also concepts about “filter” to help you descrease the load of the stream.

You see I just talking about data structure and general design (observer pattern from RxJava in particular) over some case of AI programming.

In the real game, there are a lot of places where general design WILL NOT WORK. And you have to optimize much into the dark evil low level code (poor AI programmer)… But at first, you can and should use RxJava, if you in your road of learning.


I will check out JavaRx, thanks for input.

I only update the orders list in single thread or rather, using the thread that is executing the orders, and adding or removing persons is performed in another loop after this.

I intuitively use “push messages” (the concept at least) a lot but didn’t even know RxJava existed.
RxJava’s more formal (and more well thought) approach is probably a better idea. I’ll look under RxJava’s hood in the next few days. Thank you !

RXJava really?
I went in Devoxx France a month ago to a presentation about it… and it made me the worst impression ever.
Starting by the 9k lines Observable class with its 150 static methods…
Multithreading with it looked like a nightmare…
And from the results exposed, compared to java 8 streams it seems very very slow…
@atomix Did you ever consider anything else?

@atomix: I’m now diving into RxJava and if it does what I think it can do I’m going to like it :smile:

@nehon: do you think there is an alternative to RxJava I should consider ?

It’s because it’s for Java 6+, the lib try to mimic functional facilities which consider very verbose in java 6… Let’s make a fair conclusion that it’s bad in java 6 syntax but is super cool in java 8 and groovy (and scala). And note:

Rx is an API for creating and processing observable sequences. The Streams API is for processing iterable sequences. Rx sequences are push-based; you are notified when an element is available. A Stream is pull-based; it “asks” for items to process. They may appear similar because they both support similar operators/transforms, but the mechanics are essentially opposites of each other. [from stack overflow because i’m lazy]

No, it’s not bad for “multi-threading” because it’s using another paradigm, observables to be non-opinionated about source of concurrency (threads, pools, event loops, fibers, actors, etc) [copy from RxJava wiki] … It’s not just using a lot of thread because using a lot of thread just good for “actual” parallel computing, not intercommunicating objects. The problem here is communicate between things… You can see I use “actors” as the the primitive of concurency. The communication between actors are “signals” go under “streams” in which “operations” working on them on the fly. When the goal reached, the result it’s likely usuable and the persistent states is likely no need. Of course it’s not magically work if you don’t spend sometime dive into the concept. I firstly also find them pretty dumb and non-sense. However, I’ve promised myselft comeback time to time to actually find its good side after a while. The welldefined streams like built by RxJava are actually very good to make real-time applications: games, simulations, web… I think you should read: The introduction to Reactive Programming you’ve been missing

And it’s also not bad in performance at all, if you know how to use it properly. Netflix got a point in writing this, and shouldn’t because of its slowness. I can’t tell you more, but it’s work great with Jme3 in my game also :wink:

When you using RxJava, you will have to write a few more methods to expand observables, then compose them in thousand of ways. Graphics, sounds events, AI computing … for example are likely pieces of water pipes, connect into existed pipeline and let the water (execution) run through

… It’s very abstract… It’s very familiar, JQuery has the same idea and it rule the web :smile:

From my experiment, to make RxJava and other functional reactive pattern run better, let wired the objects and let everything on the strings, no persistent states!

Yes you going to like it. :wink:

Let’s try with groovy to get the taste of things. In groovy, Rx is consise, not as verbose as its Java counter part.

I recommend you to write it with some actors. (akka or gpars)

Then read

I’ve written a groovy example game with gpars and RxJava awhile ago. May be I will put it online somehow :smiley:

Alright. I have no experience in it so I trust you. It’s just that I had a very bad impression during the presentation.

Hi all,

I’m resurecting this thread just to give a feedback on RxJava and thanking @atomix. You changed my life lol. Thank you.
I had to force myself to dive into it multiple times before the “ha !” moment but I think I’m getting it now. (to be honest I had multiple “ha” moments)

It requires a brain twist to stop thinking in a blocking-synchronous-iterable world but it’s worth it. I now feel a bit like a coder-plumber and it’s good !

RxJava is already helping me a lot in my “main” (paying) job (as a mobile dev) and will help me a lot in the future as UI feedbacks+responsiveness & asynchronicity is so important in the mobile world. I now hate the unresponsives and too-lazy-to-give-feedback mobile apps even more.

Looking at my old android’s AsyncTasks and the duct tape I had to put in makes me nauseous. Even with the verbosity of java 7 it’s a lot better than what I did in the past.

Once the game I’m working on (free time only) is done I plan to write one using entities & components and I think RxJava will be a big lever to make it (almost) easy.

If you don’t know Rx, please go learn it !
I’m not saying it’s a magical tool good for everything, no. But damn, it’s doing its job in a very good way.

You may not like it too and that’s ok. Just find good reasons for not liking it.

@atomix : it has nothing to do with the thread but I hate Groovy and Clojure. I even find GNU R and Python more cool. I’ll stick to java even if it’s pretty verbose with android-java7.


Just mentioning this as a sidenote… 1000 Callables don’t result in 1000 threads, the number of actual threads is defined in the executor.

@normen : surely and it’s probably a good thing not to have 1000 threads hanging around :smile:
Sidenote too : with rxjava it’s up to you to choose the threading mechanism (executor, new thread or whatever) fitting your needs.

Hi, hope you enjoy the Reactive trip so far…

In Java8, using Rx is like flying in Ironman suite (as I wish I will build one when I’m rich enough :slight_smile:

Rx’s fun and really useful in quite a lot of things. Did I mentioned playing around with Groovy can be fun too, but as you said you consider your self a Java developer more than a JVM langs dude, well, good for you. As Actors and non-blocking something going to be two technologies that every dude who live by coding use in next year, we should know about it sooner or later. As for game programming, I think I’m having a pleasure time using Reactive and JME and be productive at the same time.