VBfx / Common / Fuzzy Logic


So you heard of that Fuzzy Logic (*wooo*) thing and don't know what it means? Well here you are. Note that there are several ways of implementing Fuzzy Logic systems, I will however teach you the basic idea and how to build up a simple AI system upon the theory. Once you understood the technique you're ready to keep on reading more in-depth articles and understand further tutorials. Just keep on reading...

The idea behind it

The idea behind Fuzzy Logic is quite easy to understand: In fixed state systems you have one state describing the action, eg. action = searching, attacking or defending. Fuzzy Logic however splits this action up into several states that are handled as numeric values. Taking the example from before you'd have something like: search = 7, attack = 28 and defend = 20. Now instead of just setting the new action you increase one of the states, eg. if the player attacks the unit then attack is increased by 10. To make this working you need to check the results each time you change one or more states.

The function that checks the states might already discard the states that are below a specific treshold, let's take the average (which is 18 here) for example. In this case search (7) would be discared while defend (20) and attack (28) would remain possible. From this remaining list you can now chose one item randomly or just take the one with the biggest value - the exact decision which one to chose can be a very complex task and you're supposed to find your own method here ;) Chosing randomly will allow the AI to react more spontaneously but the actions may not fit the in-game character.

Now that the unit decided to perform a action you might want to raise the state so it doesn't just switch to another action in the next frame. This is done by adjusting the action's state variable. If the action came out to be defend for example, you'd add 50 to the defend state making sure it won't switch over to attack in the next frame and you may also lower the attack state. Basically you should just make sure that opposite actions stay below the treshold for a longer time.

Simulated life

A very common concept to get more realistic decisions is to include constantly incremented states just as hunger or sleepyness. Remember that whenever a state reaches the treshold it'll be a possible action, this means that a unit may stand around for long time and suddenly go off for a meal, that's when hunger goes over guard for example. However if the AI is attacked while eating, the attack value will soon be pushed over hunger and the unit will fight as excepted.

So what..?

What happens above surely isn't true intelligence and you could easily find another way to implement the behaviour of that AI somehow else. But the important thing here is that Fuzzy Logic gives us quite a complex system with only 3 states. You could easily add some more states making the AI look more complex without changing much code.

The dark side however is the lack of control since you don't know from the beginning what the AI will do. But there's always ways to force your needs so just be creative when combining Fuzzy Logic with any other system you prefer. It's not a big deal to change the action or states to the ones you need.


The attached demo project shows a simple AI system that holds 4 different states (attack, defend, hunger and sleepyness) for each unit. The units don't affect each other but you can clearly see the randomness in their actions.