FSM or Behaviour Tree or ???

Hi,

I’m learning concepts of AI, and I’m stuck on something I found hard to do with basic ideas mentioned above. Let’s introduce what I think should be simple to achieve with AI system in place.

I have an Actor that has number of params and a set of actions with different priorities - Walk, Eat, Relax.

If Actor is hungry, should be able to eat (“Eat”)

If Actor is tired, should be able to relax (“Dance”)

If nothing special, just Walk. (“wander”)

Simple enough so far. First twist: Actions should be able to run in groups, and only actions with highest priority per group.

So walk is Body action, eat is Act action, and Relax is BOTH. That way Actor can Walk and Eat, but cannot Walk and Relax or Eat and Relax - as Dance occupies Body and Act groups. With this simplified Actor, we have flow like this:

Actor Walk, then when he’s hungry he Eats while Walking. When he feels tired, he Relax. If during Relax he became hungry, he will Eat (and stop relaxing!), then continue Relaxing.

With such system, I can add new Actions by defining priority and which groups it belongs to. Let’s say I want to add hmm Toilet action :). This one is highest priority and occupies all groups. That way whatever Actor is doing, he’ll drop it and run to Toilet.

Ok, concept aside. With the above, how am I supposed to implement it?

I think FSM are out instantly. Maybe multiple FSM, one per group? But then the Actions has to be split into BodyRelaxAction and ActRelaxAction, and they need to know about each other as they can run only together.
BT can run actions in parallel, so one parallel action per group, but then above applies again - actions need to know what other parallel actions are running.

Any ideas which of the AI concepts covers above situation?

Don’t focus on the type of AI it is, just make it.

You an easily make anything happen with bools.

if(PlayerTired)&&(Hungry){

Relax = true;

}

if(Relax = true) && (!Hungry){

Sleep = true;

}

if(!Sleep){

//Decide randomly what you want it to do lol.

}

1 Like

Sounds to me like you have thought pretty deeply about what you want your AI to do. Just roll your own system and use all the tools you know about. You can have multiple FSMs or Trees as sub-systems in your overall AI system. For example, you can use a behavior tree to control the current strategy of your AI and then FSMs to handle the operational and more subtle stuff.

The reason why I’m asking is because I think it sounds like quite common for AI to behave (let Enemy move as needed but also reload at the same time) so I wanted to compare it with how others solved above problems. As mentioned, I’m doing it as an exercise in AI.

(Actually I did built it and it works so far with a bunch of different actions. Now I need to find a reason to use it in more complicated scenario than letting Actors work, eat, party, and clog the pipes :). I think I’ll let them fight with each other in mazes hm)

you can switch over a bitfield, but that’s still basically having a long list of bools and ifs. You can organize a few bitfields so they have different priority groups.

Learn Neural Networking, not sure if Unity can do moderate neural networking, but hey if it can, then you can sell that and have have number 1 AI system for Unity lol.

Nothing better than a computer that can learn on its own from experience

Trust me, it’s very rewarding to actually do it, I know.
I’ve been doing just basic Neural Networking for sometime now, I’ve actually gotten it to look at a picture and know what a certain thing is, I’ve got it to virtually have a conversation with you vocally, if it doesn’t know what something means, it will ask you and whatever you tell it, it will store into it’s artificial neurons, now of course it can be a bit subjective, because someone else might tell it something else, so I programmed a back end to it, to look up what words mean as well.

I’ve even gave it the signs of basic emotions (Get mad at you, get sad, be happy, get worried) but it’s just very basic.
I’ve got it to understand what a weather radar is and set predetermined areas to scan, if it notices anything different, it will send off an alert to your phone through SMS to let you know which zone was effected and how far away the possible threat is.

It’s something I’ve been working on for my Company Intelligence Division in the State Guard, so I can’t say everything.

1 Like

Thanks, I was hoping to avoid neural networks as I think they might be overkill in this scenario. I’ll do it anyway later :wink:

So, how do we know it’s you talking and not the AI stealing your identity? Just kidding, the project sounds awesome. Do you have it on Github or somewhere? :slight_smile:

Haha that’s funny.

Oh no, I can’t give it away, it’s a State-Side Military Program lol.

Sounds straightforward. I’d just program it! Like Warhead said just do it.

You don’t need some complex methodology or AI system to do what you are describing. Actually I have not seen anything yet that requires some “advanced” complex system. My personal philosophy on this subject is (basically a twist on Einstein’s quote “if you can’t explain it to a 6-year-old then you don’t understand it yourself”)… if you can’t figure out how to implement something with the most basic design & implementation then you don’t really understand the problem. Because everything can and should be broken down to its most basic elements. Every complex thing developers want to pull off is actually composed of very basic systems and data structures. They often just don’t take the time to break things down and instead focus on the system as a whole.

I would argue that most people are nothing more than very basic decision trees. Some of them are so predictable that it hurts to be the same species :stuck_out_tongue: I’m frightened by the lack of effort, concern and intelligence I see in some people at college.

1 Like

Most of the time I would generally agree with just doing it, but it’s easy for AI to get complicated quickly. Then once it becomes complicated, it becomes a nightmare to add even one new state/behavior.

The simple advice is to take your time to know what you want out of the system first, then it will be much easier to find the solution you want.

1 Like

You’ll need booleans and functions.

if(tired)
Sleep();

void Sleep()
{
  //Play sleep animation
}

You can use enums too. Declare a public enum out side the class so that it can be accessed everywhere. Also, you can switch the enum state and get the desired result.

public enum States{Sleep,Eat,Wander}
public class AIClass{
public States state;

void Update()
{
  if(state==States.Sleep)
       Sleep();
else if(state==States.Eat)
     Eat();
}
}

You can use switch statements for clean if else code, but you don’t have to.

I agree man. That is what I mean. If a person takes the time to break the “complex” thing down into its core elements only then will they really understand what they are trying to achieve and how best to do it. It’s when the focus is placed on just the high level complex beast that problems begin. Absolutely need to identify what the system is supposed to do. Then break that “huge” high level stuff down into components. And so forth. The high level is too abstract and people trying to develop against that produces a mass of spaghetti code. The more you break it down the more it maps 1 to 1 from design to implementation.

@StopAskingForAccounts
Use a Behaviour Tree, there is a great asset for it from Opsive. However if you have never used a Behaviour tree before, it can be a bit daunting at first, but once you master it you won’t know how you lived without them. They are flexible and nodes can often be made in a very reusable way.

FSM is all well and good, but try writing complex actions and reactions for an AI out of them, it soon becomes a complete mess.

And for goodness sake whatever you do, don’t make a monster class for each AI with 1000’s of if statements! Or a spaghetti mashup of dozens of coroutines. Use something visual for either FSM or BT, you will regret it late in the project if you do not.

Ok, I think my example was too simple, as posters are suggesting to use ifs or bools, which is unmanageable after you got more that dozen of actions.

As stated before, I want Actor to be able to do multiple actions at once, with actions taking over when needed. Behaviour Trees are not a solution, unless I let actions know about other actions - which you should know is bad design decision.

Actor doesn’t have to be bipedal, humanoid, or even intelligent creature. It can be for example a building, with systems like elevators, water, electricity. You want elevators to go to requesting floor, but if there’s fire you want all elevators shut down, go to 1st floor and stop responding to requests.

Thanks for ideas, keep them coming :slight_smile:

You may want to look into utility-based AI. It’s sort of like the neural network approach, but it’s much more flexible since you can add new actions / relations without the need to retrain the NN or, in the case of FSM or BTs, restructure your state logic. Instead, you basically have a bag of possible actions that are scored, normalized, and sorted from high to low. From there, you can either simply take the highest scoring action or maybe pick the action through a weighted random function.

Scoring is done by taking all of the relevant world data and constructing normalized relational curves. So a “Run Away” action curve might be defined as a function of CurrentHealth, CurrentPotions, EnemyMaxDamage. This would most likely be an exponential decay curve because if you could die in one hit and you have no more potions, you probably want to run away pretty bad, but if you had just 1 potion, your desire to run would be way less.

I’m not sure if that makes sense, but if you want to look into it, here are a few resources:

GDC Talks

http://intrinsicalgorithm.com/IAonAI/2013/02/both-my-gdc-lectures-on-utility-theory-free-on-gdc-vault/

Articles

http://intrinsicalgorithm.com/IAonAI/2011/12/getting-more-behavior-out-of-numbers-gdmag-article/

Let me get this straight:

You want an AI system with actions executed based on their ‘needs’ like that of Sims? Something like this?

1 Like

Ah, and there you go. A utility-based AI tool for Unity. Though, I don’t know if the $45 price tag is justified since it looks like a relatively simple implementation.

Limyc - Ah, that’s what I was searching for, thanks! I knew it must exists somewhere, I just didn’t know the proper name :slight_smile:

RaiuLyn - ha! DecisionFlex is what actually inspired me to write my own system, as it looks like something advanced enough to make systems fun while not being neural-networks level complicated. What I’ve got is not as complicated, but hey, it took couple of days to create only.