Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

FSM or Behaviour Tree or ???

Discussion in 'General Discussion' started by StopAskingForAccounts, Dec 14, 2014.

  1. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    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?
     
  2. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,886
    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.

    }
     
    frosted likes this.
  3. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    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.
     
  4. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    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)
     
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    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.
     
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,886

    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.
     
    der_r likes this.
  7. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    Thanks, I was hoping to avoid neural networks as I think they might be overkill in this scenario. I'll do it anyway later ;)
     
  8. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    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? :)

     
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,886
    Haha that's funny.

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

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    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.
     
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    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 :p I'm frightened by the lack of effort, concern and intelligence I see in some people at college.
     
    GarBenjamin likes this.
  12. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    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.
     
    GarBenjamin likes this.
  13. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    You'll need booleans and functions.
    Code (csharp):
    1.  
    2. if(tired)
    3. Sleep();
    4.  
    5. void Sleep()
    6. {
    7.   //Play sleep animation
    8. }
    9.  
    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.
    Code (csharp):
    1.  
    2. public enum States{Sleep,Eat,Wander}
    3. public class AIClass{
    4. public States state;
    5.  
    6. void Update()
    7. {
    8.   if(state==States.Sleep)
    9.        Sleep();
    10. else if(state==States.Eat)
    11.      Eat();
    12. }
    13. }
    14.  
    15.  
    You can use switch statements for clean if else code, but you don't have to.
     
  14. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    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.
     
  15. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    @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.
     
  16. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    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 :)
     
  17. Limyc

    Limyc

    Joined:
    Jan 27, 2013
    Posts:
    29
    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/IAonA...lectures-on-utility-theory-free-on-gdc-vault/

    Articles

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

    http://www.gamasutra.com/blogs/Migu...ial_Intelligence_Utility_builds_Character.php

    https://alastaira.wordpress.com/201...unctions-for-modelling-utility-based-game-ai/
     
  18. RaiuLyn

    RaiuLyn

    Joined:
    Sep 14, 2014
    Posts:
    39
    andrew-fray likes this.
  19. Limyc

    Limyc

    Joined:
    Jan 27, 2013
    Posts:
    29
  20. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    Limyc - Ah, that's what I was searching for, thanks! I knew it must exists somewhere, I just didn't know the proper name :)

    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.
     
  21. RaiuLyn

    RaiuLyn

    Joined:
    Sep 14, 2014
    Posts:
    39
    Limyc - Yeah, $45 is a bit too steep from what I'm looking at. Maybe whoever made it will expand upon that to justify the $45 price tag (and more once it comes to the asset store with its price increased)

    StopAskingForAccounts - Cool! I would like to see how you implemented since I'm working on something similar myself for my game. Chances are I'll probably scrap it since I don't have time to make something big like it.
     
  22. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    Actually I think $45 is not bad price for such cool AI. I'll explain below, because first I wanted to show what I got after two days of work.

    In short, what I currently have is something along the lines:

    Actor has multiple Decisions on the same object. Each tick each decision return Action for specific decision, with specific priority. All those actions are then put into PriorityQueues of specific Systems, and checked for completeness of system. Then each selected Action is Tick'ed.

    To expand as last part might be confusing, what I mean is if Action is not on top of PriorityQueue of all of it's affected systems, it won't be activated. Example using Actors from first post:

    System: Act, Body
    Decision: NeedsEat, NeedsRelax
    Action (priority/systems): Eat (10/act), Dance(5/act,body), Nothing(1)

    Last part ensures that when Eat action happens, Dance action will stop, even thought it's highest priority in Body system.

    Now, please assume that in real game I have way more actions than just this, as this could be handled with other AI's. It's just simplified example :)

    Now, why I think $45 is worth price for this AI:

    I'm missing a LOT here compared to the asset. I'm making all actions in code, I'm having IF checks to see if action should be selected (if hunger < 50 etc drill) and not graphs ones, I'm probably missing more things. There are no easily accessible resources for it, as opposite to FSM or BT. I didn't see the Asset thought, so it's still hard to say, I only saw screenshots and demos (and picked idea for myself there, he he :) ). Maybe it is, as Limyc said, very simple implementation after all (and all the power is in ready to go Editor editor)
     
  23. Limyc

    Limyc

    Joined:
    Jan 27, 2013
    Posts:
    29
    I think what you're describing is the same or similar to what's called Dual Utility. So you have buckets of action categories which are scored and sorted, and then when it comes time to make a decision, you start with the bucket that has the highest score. If none of the sub-actions are valid (as in, everything scores some low value like 0.1), then you move onto the next bucket.

    So your buckets would look like this:

    [Hunger Bucket]
    -Eat at Table
    -Drink Juice
    -Make Sushi

    [Fun Bucket]
    -Watch TV
    -Play Video Games
    -Dance
    The only comparison statements you should have is "if score > threshold then do action/bucket". You should never need to compare actual values like hunger or health. When you have direct comparisons with world data, then your decision function will be brittle. All you need to do is evaluate the curve you defined, which should give you a normalized value from 0 to 1 (or any scale you choose), and then sort it with all other action scores.

    I can't really share production code with you, but I converted a sample implementation from a book (Game AI Pro) and posted it on my Github. You're welcome to take a look and use what's there. It's just a matter of making it work with Unity's component system in a way that makes sense to you. I also took pictures of the 2.5 page section in the book that explains the curves used in the example code, so that should help.

    Repository: link
    Album: link
     
    StopAskingForAccounts likes this.
  24. StopAskingForAccounts

    StopAskingForAccounts

    Joined:
    Nov 26, 2013
    Posts:
    20
    Thanks! Looking into it now :)

    Btw thanks for mentioning the book, seems worth buying as looks a bit more advanced than FSM or BT that can usually find in AI books
     
    Last edited: Dec 17, 2014
  25. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Hi,

    I know this is an old thread, but some of you guys might be still around.

    If you are interested, I made a scripting tool based on Behaviour Tree.

    More info here: http://www.pandabehaviour.com
     
    Stardog likes this.
  26. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    I used both FSM and Behaviour Trees plugins. I find FSM lot more natural and easy to use , while Behaviour tree would suit some dialog system better.
     
  27. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    About dialog system, I'm suspecting you are confusion Behaviour Tree with Decision Tree. Behaviour Tree is entirely a different beast.

    I agree that the fundamentals FSM is simpler to learn and to understand. FSM is perfectly suited for simple and well define logic. But as soon as your AI grows in complexity (let's say more that 5 FSM states), FSM becomes a pain to maintain due to the states being highly connected and interdependent because of the transitions. Also FSM lacks reusabilty: you often find yourself creating a FSM from scratch instead of reusing an existing one.

    Whereas Behaviour Tree provides you with the tools to handle complexity and to modify your logic much more easily. Because Behaviour Tree is intrinsically hierarchical and the transitions between task are implicit; the logic is defined by the structure of the tree rather then the connection between the tasks (the tasks are completely independent from each other). Which allows you to easily delete/insert/move a tasks anywhere in tree or group tasks into a sub-tree to reuse it somewhere else.
     
  28. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    You can do the same using FSM.
     
  29. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    No, you can't easily delete/insert/move a state in an FSM. Any of theses actions requires first to fiddle with the transitions. You can indeed group state into a sub-FSM if you are using an HFSM. But you can't easily reuse a part of an FSM. If you want to reuse a part of it, first you need to isolate it, again by fiddling with the transitions, then copy-paste it to another (H)FSM.

    Whereas with Behaviour Tree, nothing is required to delete/insert/move a task around. You just do it. Again the transitions in a BT are implicit, they are defined by the tree structure. It's like programming: you don't specify the transition from an instruction to another, you just write the program, the transitions are implicit and defined by the semantic structure of the program.

    In fact Panda BT is a script based Behaviour Tree engine. That is you define your Behaviour Trees by writing scripts.

    More details about it on the website:

    http://www.pandabehaviour.com/
     
    Last edited: Mar 22, 2016
    MV10 likes this.