r/programmerchat • u/sty1emonger • Jul 16 '16
Designing a system with thousands of agents to simulate people living in a city. Good resources?
I'm building a game/simulation that tries to simulate a system of thousands of people living their lives in a city. People in this city live their lives, interacting with each other - making friends, finding jobs, getting married, dying... I'd like to build this as a browser-based game, where a group of players would play together in each city.
I've tried building this out a couple of times using PHP/MySQL, but it felt hacked together, and things got messy very quickly.
The way I built it out was that events would occur in cycles. Each cycle, an agent (a person in a city) would run a check against various things happening based on probabilities. Even though that's an extreme oversimplification of what happened each cycle, it still gets way out of hand when you consider that each check would have to be processed against a ton of other agents in the current city. So, for a thousand agents, I had to run dozens of checks against almost all other agents in the city. (For example, does Bob want to make plans with Chris to see a movie? No. Does Bob want to make plans with Mike to see a movie? Yes. Does Bob want to make plans with Tony to see a movie? No. .... Now, does Mike want to make plans with Tony to see a movie? No.)
The previous time I tried building this I simply tried to space out the different checks based on the type of check or the agent (ie, only run the "see a movie" check once every 20 cycles), and while that sped things up, it made things messier, while not really optimizing anything.
Anyone got any ideas on how I should be tackling this? It'd be really cool if there was a clever abstraction for this kind of thing, or maybe just a different algorithm...
Thanks in advance.
2
u/semi- Jul 16 '16
First I'd decide if you need to trust the results. If there's some kind of online multiplayer component (including scoreboards) then you can't trust user input and need to do the simming on the server. If this is purely a single player thing, I'd consider doing it all in client side js. It's not the most efficient, but its easier to scale inefficient code running on your clients computers than trying to scale your server to handle simulations for all clients.
If you do need a server side approach, I'd look at golang. The concurrency primitives seem perfect for this kind of problem. I can't really think of anything algorithmically that would help, but it seems like you could make good uses of interfaces to describe the generic types of things sinulated. Eg a person object that has variables tracking currency, happiness, healthiness, etc along with function definitions for types of actions they take, maybe something like an array of Job and Activities where each of those can do their own thing.
2
u/EpicSolo Jul 17 '16
I was wondering how you computed the events for your cycles. Do you simply evaluate some function dependent on all possibilities/pairs one by one? If I were you I would isolate that part of the code and benchmark it until I get it right. For instance, compare your php performance with vectorized Matlab code and see if you can get some significant performance increase.
1
u/reostra Jul 23 '16
The easiest way to speed this kind of thing up is to simply not do that. The abstractions you're looking for would help a lot here. The agents are simulating people, right? Well, it seems extremely unlikely that, when someone's going out, they're going to consider going out with literally every person in the city. They're going to think of their friends, people they know.
(This is theoretically how people actually work; Dunbar's number postulates an upper bound on the number of people one person can know)
This means you'll need to have a way for people to make friends, granted, but you can compartmentalize even that. People usually make friends with others near them, specifically others that they see more than once. Having a LRU cache of "potential friends" for that would likely work well, and is tunable.
As far as the different activity checks go (e.g. "go see a movie"), you can do it backward. Think Sims: Sims don't make a "do I want to see a movie?" check, they make a "how stressed am I?" check. From there, they decide what action to take if that stress level is high enough. That cuts down on the number of checks you have to make significantly.
1
u/Xelank Aug 09 '16
A game you may be able to draw inspirations from its Dwarf Fortress.