Search Unity

C# Real Artificial Intelligence Problem (Help) Difficult Math, has taken me 3 days.

Discussion in 'Scripting' started by FreeSide Games, Jul 6, 2015.

  1. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    Hello I am creating a video game with AI's, now what I need help with is walking path's of AI. I understand you can use vector delta time but that is dynamic and not generated paths. My problem with vector delta time is it seem's not real, each day cycle the AI just walks on same path. It's not living or breathing. That is my first problem.

    Possibly could I have layers of nav mesh? each for different AI's but than have different statements for each day of week but have it random choose between 1-7? Keep in mind the player is building the village so nav mesh will than have to generate around building for AI.

    My second problem is having AI's that carry values and switch classes when being attacked into different state.

    For example, AI 1. Is married to Vanessa and AI 1 is a fisher for the village/castle, he is now going on a walk to pond where he is going to fish for that day, day is over now he has caught random value of 1-10 fish now he is going back home with value of 10 Fish. AI 1 does not keep the fish but instead goes to castle and hands the fish to person who collects the fish, now that value is removed and goes back to another statement that it is night time and he is sleeping in bed with his wife Vanessa
    .
    I don't want to swarm you guys with difficult math and my idea's of what I am going to be doing is I am creating living breathing world combined with Strategy, Simulation, Tower Defense, Action, Adventure, Survival, yes it's FPS.

    I am having such hard time figuring out how to have AI 1. walk on a path that is different each day(added realism) and have him remember that he is married to Vanessa for conversation of course and this AI is a fisher that collects fish. Later I will be adding random generation to life of each AI, like him not being married than being married and than having kids like family tree.

    I thought of using a path with collision but that might cause a lot of bugs.. Imagine the player building the village or castle than forgetting to add the path for the AI, and than they are just doing donuts in middle of town.
    I actually tested this, made me want to exit the game...

    Thank you for your help.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Question... why would the path be different every day?

    When I would drive into the office every day, I didn't change my path to work. I would take the same basic path to work every day. Why would I go any other path?

    This is AI... WHY would the AI pick a different path?



    So, one day I changed my path to work. There was construction on the bridge that took the most direct route to work. So instead I had to go a completely different path. But once the bridge was repaired, I went back to original path.

    Some days I needed fuel on my way to work. So I'd stop at the gas station on my way too work. This required a small 100 yard detour off my usual route with a u-turn. But overall, I took the same general path.

    Some days I brought my girlfriend to the train station in the complete OPPOSITE direction of work. Adding 45 minutes onto my commute. So I got up extra early, drove to the train station, and took a way different route to work.



    In all of these situations I changed my route for a reason. They aren't random paths. They were all the most efficient route I could think of relative to accomplishing the needs of my tasks that day. If you want your AI to have a little variation to their day... give them some variation to their day! Don't just randomly make them walk across town for no apparent reason.

    Now... how could you simulate this...

    I don't personally use the built in Unity Navmesh, I use the Aron Granberg A* Project:
    http://arongranberg.com/astar/

    But any pathfinding system worth its salt has a weight/penalty system. So I'm going to assume the navmesh system you're using has weight/penalty system for its navmeshes.

    Now lets say you simulate weather. It's a rainy day in town. You add a slight penalty to any node that is getting hit by rain (not indoors, or undercover). Now your AI will prefer nodes that aren't getting rained on.

    Most pathfinding engines allow a seeking agent to have a unique scale for penalties. Allowing a penalty to be made stronger or weaker by preference. So say one of your AI agents doesn't care about rain (they own an umbrella), they scale rain penalties by a multiplier of '0', which makes them not care about that penalty. Another who LOVES rain might have a '-0.5' multiplier on rain penalities which gives a preference to walking in rain... but still prefers an efficient path for the most part.

    You can also introduce random blockers into the navmesh. I know that the Aron Granberg project has dynamic blockers, so you can have things like doors or other thing that close off previously open paths. Updating the mesh at runtime. Today there is a greenmarket in the center of town, so there's a giant penalty region in the center of town because your character doesn't want to walk there.

    There's all sorts of weird things you can do here.

    Don't make your AI randomly choose stuff. Have random things that occur that make your AI have to reevaluate its choices. Otherwise WHY would it be taking another path? Because today AI 1 thought "hey, lets take the longest possible route to the fishing spot... because I want to be arbitrarily difficult."
     
    roojerry and lordconstant like this.
  3. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19

    You make a very good point, I admire that.
    Thanks for the link by the way I will certainly be checking it out.

    My problem still consists though, my AI can walk but he is brainless. He can't remember a given value and than go to collection AI interact with him and hand over fish than pass value onto collection AI, go home to his wife and sleep.

    Yeah i do agree on your idea, I was planning to make living breathing world. Since it's raining we should light a fire stuff like that.

    Of course my game follow's a single king in medieval age and that player play's in living breathing world shaped around his decision's as a king. I thought having AI that for example.

    -Fish
    -Mine
    -Woman
    -Men
    -Killers
    -Thiefs
    Stuff like that would all interact with each other.
    But yeah enough me rambling!

    Someone please help with this problem, It is hurting my head.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Another thing you can do to give some realism to your agents pathfinding is this.

    1) Have one very simple navmesh for the entire town. This navmesh only has nodes for very general locations in town. This represents your knowledge of the town with out any obstacles or even specific knowledge of town. I'm going to the pond... ok that goes down the alley, through center of town, over the bridge, through the park and to the pond.

    2) have a very detailed navmesh that has all the obstacles included. The mesh is very detailed. It also contains your dynamic obstacles and penalties that simulate the day.

    When your AI starts off for its day it calculates its basic path to the fishing hole. It does not know if their is traffic or accidents or construction. It just knows where it's gotta go, so it uses this simple navmesh to decide that.

    Now as you path from node to node in that path. You create a short range nav calculation on the detailed map. Considering all possible obstacles. The AI agent is now at the bridge pathing across it, it can SEE the bridge logically, so only now can it tell if the bridge has construction. So now it has to decide a path. If NO path can be calculated, then it has to go back to its 'simple navmesh' and calculate a completely new generic route (with a huge penalty on the bridge) to work and start walking that instead... hoping that nothing is wrong that route.

    You can then also introduce an information system that adjusts this simple navmesh. You could have an AI agent learn before hand that the bridge is going to be closed. Say your AI agent has a newspaper subscription that they read in the morning, this is just a personality trait. Not all agents do this... some do. Because construction is usually planned, it's in the paper, so your AI agent goes into it knowing the bridge is closed. So they calculate that initial path on the simple navmesh with a huge penalty on the bridge to begin with.

    You could get really creative here.

    So say you create relationships between AI agents. This could just be done by storing a reference to every AI agent it comes in contact with. The more often those agents interact (near each other) the more their relationship strength increases. Just passing on the street is small, if they're in a shop and ones the shop keep so they interact stronger, it's larger. So on, so forth.

    Then you have a share information value. How important is the sharing of some information that that agent has gathered. Something like traffic issues (the bridge is down) would be told to just about any person you passed (low values). Where as the idea your mum died might only be shared with the closest relationships (large values).

    Now... if an agent goes to the bridge, notices it's closed, and goes to repath. It stores this as information it has. Now whenever it passes another agent, if their relationship is of a certain value... it imparts that information to them.

    Now another AI might find out the bridge is closed before getting there, and reroute accordingly before even getting there (just like the newspaper guy).

    You could even get more flavourful by putting in a value to represent how much you trust the word of this person. So may an AI is untrustworthy, and so ignores the word of this person they passed on the street, and STILL goes to the bridge anyways.
     
    roojerry likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Well then you need to implement memory.

    This has NOTHING to do with pathfinding.

    Why solve your problem of diverse pathing by just putting random values in, when you're desiring to have a more realistic experience?

    As for the maths of it, it's not really complicated maths either. Some basic scalars (floats) signaling the intensity of some personality attribute. And a collection of informational tidbits.
     
  6. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    Could you give me an example on how to implement memory and go about problem.
     
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Didn't read all of it, but skimmed. You're gonna have a big problem if you use the built in navmesh system. You can't build a path at run time. So if you build a house on a path, you're screwed. You can have it recalculate and tell you if it can't complete the path, but you can't draw new paths for it. Eventually it will just get stuck. You will need to use an A* algorithm
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Well technically the built in unity navmesh would use an A* algorithm.

    All A* is, is building a path in reverse order over a network of nodes based on weighted penalties and node to node heuristics.

    That isn't that hard. I implemented the A* algorithm as a hobby years ago just to see if I could do it:
    https://code.google.com/p/lodgamebox/source/browse/trunk/com/lordofduct/engines/ai/AStarMonotonic.as
    The function 'reduce' in there is really all the A* algorithm is.

    What you mean is use a pathfinding engine that allows for dynamic updating of the mesh at runtime.

    Which I will point the OP back at the Aron Granberg project which does support this.
    http://arongranberg.com/astar/
     
    Lahzar and SubZeroGaming like this.
  9. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    I had an AI pathfinding based problem a few days ago. This guy was one of the guys that suggested me to get the A* Project Pro. At first I didn't want to because it cost 100 usd and I was sure I could solve the pathfinding in a week or so, but let me just tell you that for my specific case, getting the full pro version must've saved me nearly a year of working! Atleast give the free version a try!
     
  10. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    Okay thank you!

    How do i go about solving math issues?

    I need the AI to remember he is married to Vanessa for conversation with the player.

    My big problem is mathematical problem, the AI walks but he is brainless. I can't get him to remember how many fish he has caught that day and than go back to castle to give a collector the fish. I don't want them to just brush by but instead stop for bit and play a talking animation and than hand over fish.
     
  11. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    I agree I just bough it and now it's implemented into project. Works well but mathematical problem persists.
     
  12. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    This isn't really an issue with math. It's a logic issue. You're talking about keeping track of his wife...you need a variable that keeps track of his "current" wife lol.

    How many fish did he catch? Have a variable to hold that information, and some game logic that will determine the amount of fish caught.

    This is just programming logic.
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Like I said before, memory isn't a maths problem.

    Now we're getting into behaviour systems, pathfinding is not going to come into play here.

    Behaviour systems can get very complicated, very fast, especially if you need dynamic behaviour.

    I only this past week started writing my behaviour engine:
    http://forum.unity3d.com/threads/ai-behaviour-tree-using-gameobjects-components.335138/

    There's some out there that already exist like RAIN:
    http://rivaltheory.com/rain/

    and AngryAnt's Behave:
    http://angryant.com/behave/


    Of course, you can always adhoc it in code directly as well.

    In an adhoc type of situation you'd just have a script for the fishing logic. And in it would be a float variable for storing the number of fish.


    For something as robust as you want, a dynamic system is going to be very hard. And I don't even think that RAIN or Behave will really fully accomplish what you need (without becoming unwieldly). I personally am brain storming in my head how I'd approach it using what I've been working on, but where the action nodes are streamed in conditionally instead of statically set up. But I don't even know how I'd start explaining it in the setting of this forum.

    I'd suggest just start out with an adhoc system. Write very explicit scripts for the various behaviours of the AI. Change between them like a state machine.
     
  14. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    I haven't read the entire thread yet, but it seems like what you want is to simply store some values to a file, instead of the RAM etc? If so, check this out: http://forum.unity3d.com/threads/how-to-write-a-file.8864/

    Protip: Before you start implementing all of these advanced scenarios like the @lordofduct talked about, you should set up a proof of concept. Start as simple as possible! Otherwise its going to get really really complicated really really fast!
     
    lordofduct likes this.
  15. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19




     
  16. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    That is heavy details on what I am trying to do.

    In simple words I am trying to make living breathing world of medieval age, from A-Z. This kind of game will be next generation AI. No game like mine exists and I can promise you.

    I might be asking a lot of question's here, I hope you can help me figure these out.

    I just can't have scripts interact with each other, look at the pictures I posted for my problem.
     
  17. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Red flag number 1.

    Before going next generation, start in this generation... maybe even in last generation.

    If anything I've said here went over your head, you're not even in this generation (my AI skills are garbage... of course some of what I said may have gone over those skilled in AI just because I was talking out my ass in some part or other).

    Create a proof of concept just like @Lahzar suggested.
     
    Todd-Wasson likes this.
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Build a next generation AI in three days without a clue about how current generation AI works? There is a fine line between genius and insanity. You may have just crossed it.

    I believe there is an asset up called love/hate or something by @TonyLi. This might help working with relationships between characters.
     
    Todd-Wasson likes this.
  19. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    I have no idea what you want, but this is what Im thinking on the memory issue:

    BASICS:
    Making a variable public makes it accessible by other scrips. However you need a refrence to the script.

    So instead of refrencing every single AI in the scene from every other AI in the scene, an easier way to mass spread variables is:

    Have a monobehaviour or simple C# class in your scene that every AI has a refrence to. This class stands for all the storing. When an AI needs to update something that should be Stored in a file instead of the RAM this class does that, like for example:
    A new AI is born in the city. This AI then gets a name, some family etc and sends this info to this master class. Simple polymorhpism I think Its called. This class now stores the important info into a file, for example to "Disk:\\GameInstalledFolders\Storage\#arbitraryID_NameOfAI_PrivateInfo", and now you have this info stored. If this info is ever needed, you can accsess the folderlocation. For example: The AI grew up, so access the folder "Disk:\\GameInstalledFolders\#identicalID_NameOfAI_PrivateInfo" and set the value age from X to Y.

    Lesser important info like how many fishes an AI is carrying, could be handled by the RAM, but saved to a file when the game is exited etc.

    Here is a basic example of Events and you should be able to fill in the blanks:
    So your AI wakes up, he goes to work, he catches 5 fish, the AIs script saves the fish in a variable, he goes to the market, he gives the fish to a seller, they can exchange info easily if you think about it.

    Say the AI goes to the market. Then he goes to the seller with the highest relationship value so that he can get the best possible price. He access a file called "...\#ID_Name_Relations" which stores all the AIs relationship data. Then the master script goes trough all the relationships and find the highest ranked people. Then it accesses those peoples privateInfo file to find out if their job is seller. And eventually it will return the friendliest seller to the fisherman. The fisherman now tells the seller via script 'Hey, add 5 Fish to your inventory and remove 5 gold!" While the fisherman does the opposite. This may the THE MOST inefficient way of doing this, but Its also the SIMPLEST. Come to think of it, you dont actually need to read files from the storage masterclass, as long as you have a logical file Naming system!

    Anyways the fisherman gets his gold, which he spends on something else. Again you could have a really cool advanced 'wishlist' system that decides what to buy and how much to save, but start out simple! Remove the gold for some bread etc, go home. Eat. Sleep. Repeat.
     
  20. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    Regarding the red flag. You need to get tid of that attitude. I might sound like a judgemental prick right now, but I know exactly how you are. Because Ive been in your shoes. You have no experience and you think way too big. Keep that up, but dont act like Its going to revolutionize the industry! This sortof AI HAS been made before, just not in one. Its actually sortof simple. The problem comes from those specific scenarios! Cities: skyline does the whole go to work, realistic path thing and all that, and some other games.. Idk, sims? Does the whole act based on relationships. Ergo, you keeping info from us only means you have to do more work yourself! Unity has probably THE friendliest community in the dev world, so use that!

    I dont get what you mean by you can't accesses scripts, aswell. Do you just not know how to? Google GetComponent<>()! Also what language are you using? I always assume C#
     
    Kiwasi likes this.
  21. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    You got it!

    Thanks.

    For some reason I did not think of doing that.
     
  22. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Thanks @BoredMormon!

    @FreeSide Games - Love/Hate is specifically for relationships, value judgment, and memory.

    Love/Hate doesn't handle pathfinding. If you think of your NPC as having different layers of AI, pathfinding is a different layer. Everyone here has provided good ideas for pathfinding.

    Love/Hate doesn't handle daily schedules, either. This is yet another layer -- several, actually. The easiest way to implement schedules is by layers of finite state machines, although some modern RPGs such as The Witcher have used behavior trees to good effect.

    Again, thinking of layers, consider combat. At the lowest level, your NPC needs AI to swing a sword. You might have a tactical layer above that, which moves the NPC into the right position to swing. Above that, a strategic layer to help the NPC decide which weapon to use and what kind of tactics to employ.

    Above that, you might have a layer that decides whether or not the NPC should fight in the first place. Love/Hate works on this level. It allows the NPC to witness actions, decide what those actions mean to it (good? bad?), and remember those actions and how they make the NPC feel about the actor. It leaves the rest of the work to the other layers.

    I encourage you to design your AI in layers. Make each layer as simple as possible. Complexity comes from stacking lots of simple layers. Perhaps don't start with the Love/Hate level. Instead, maybe start with the navigation level: moving the NPC from his house to the pond. Once you get this working, you can work on the scheduling layers that determine when the NPC should move from house to pond, and so on. Good luck!
     
    Lahzar and Kiwasi like this.
  23. FreeSide Games

    FreeSide Games

    Joined:
    Jun 24, 2014
    Posts:
    19
    Thanks for all good feedback, truly is such great community.
     
  24. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    When dealing with AI, math is not the solution. For example, your fisherman problem wouldn't newer been solved with math, no matter if you knew all about the complex mathematics behind quantum physics. As its already been said; its all about logic.

    While something like pathfinding makes things to look quite impressive as they (NPC) becomes alive and starts walking to their destinations, it also might give you an illusion how easy it is to create next generation AI :) The truth is; adding anything and everything after that, and complexity of your system starts to multiply exponentially.

    Not trying to scare you off, but prepare yourself for much more time consuming problems to solve; three days becomes three weeks pretty soon when dealing with more complex parts and getting them all to work together.

    This thread has huge amount of knowledge, compressed to few pages of easy to understand information. I suggest you to read it through few times. Best thread about general AI i've ever seen. Big thanks for everyone, and especially for @lordofduct . Really enjoyed reading all this.

    I got nothing much to add, but one practical tip to keep it all somewhat organised in your brain, as it all quickly becomes too complex to understand and remember: use flow charts, diagrams and any visual hints to help you to keep the big picture together. And its not just for remembering things, but using visual things really helps for problem solving, planning and designing.

    Not sure what software you used for that diagram few post earlier, but it wasn't all that clear what was happening there. Check out Draw.io its free, and there is desktop version and also Google doc version. Its really easy to use, and you can do diagrams, flow charts, floor planning (level design) and whatever. I've been using it year or so, and its been really big help, not just for code stuff, but for any gamedev related (and other) things.

    I'll show you an example diagram i had for similar situation about what @lordofduct was explaining how to use simple and detailed navmesh to add realism, though i wasn't using the method to add realism, but because of performance reasons. I have marked "Pathfind control - FSM" to obsolete (red) because now when i have pro version of A*, i can use recast graph (navmesh) instead of gridgraphs, and it seems have good enough performance for the level of details i need.

    diagram.jpg

    Level effectors here would be things like raining in your village. In my case they are POIs (point of interest) for adding some realism compared to random wandering of npcs, and events like alarm.

    Room effectors in your case would be traffic or construction like lordofduct used as an example, things what the fisherman would "see" from detailed navmesh.

    So, at the beginning "aStar - seeker" would be the place where you use simple navmesh to get basic path, and inside node to node loop "last node" would be the place where fisherman have reached the pond, and starts fishing routine.

    Condition "can see player" would be the place where in your case you would use detailed navmesh to let the fisherman "see" if there was any obstacles. In my case i use raycast, and if npc sees player it would use detailed navmesh (gridgraph) for cover points etc.

    FightAI would be the subroutine (if there was a construction at the bridge) where fisherman would need to make new decision, be it to get new path or perhaps if you wish to add some more realism; he would spend few moments checking whats happening.

    As you can see, having even simple nonstandard diagram, it was really easy for me to use lordofduct's suggestions and put them in somewhat similar slots. So i'll repeat myself; make some diagrams and flowcharts, and use proper software to create them :)

    Just to show you, it gets more complex. Big red arrow points to diagram. And this is just for procedural level generation and how to deal pathfinding with it. AIs got their own plans with covering systems and tactics. Might look like a mess, but i got my own system :p

    drawio.jpg

    By the way, as you are using Aron Granberg's A*, he is working with something he calls co-operative pathfinding. That would probably add quite a lot realism for village people:



    I haven't check if its available in beta version. Might be worth to check out.
     
    Last edited: Jul 8, 2015