# How to build a math module and simulate a system?

For example, in the classical game SimCity:

• an economic model to simulate the supply and demand of the city, then calculate how many people live in each building, how many people work there.
• a traffic model to simulate the commute time of the citizens,
• in another game, there'd be a whole stock market, where the player and sim-citizens can buy and sell.

Obviously, we cannot simulate every single citizen, every single car, every single trader in the market, we need some math modules and formulas.

I'm making a fake SimCity and almost finished the graphics parts. Now I'd like to know how to do the math parts, or how to build such math modules.

-
"Getting started on X" is off-topic. Do you have a specific question? – Anko Mar 7 '13 at 13:01
oh sry, i've updated the question. – namiheike Mar 7 '13 at 13:06
You could simulate each citizen. Or more exactly, simulate citizens with particular things in common at the same time. Since you're first of all building a strategy game, I recommend taking a look at the Flyweight Pattern en.wikipedia.org/wiki/Flyweight_pattern – user15805 Mar 7 '13 at 13:22
Voting to close as too broad. How to do the math to simulate an entire city is too much for one question. Break it down into smaller parts. – Byte56 Mar 7 '13 at 16:35

I will speak about the economic part. In a game I want to develop I want to introduce real economics subjects as well.

There are two ways you can do that.

You can actually use macroeconomics mathematical models to describe the world (description of people behaviour at aggregate level). There are models on stock exchanges (on why and when people prefer stocks to bonds and viceversa), on commodity international exchange as well. There are also models on migration between countries. The subject is called "international economics" and "monetary economics". I suggest you to google that and study starting from the trivial models.

But I thought I can do even something better. I asked myself the following question: exactly why I cannot simulate each single person to describe their behaviour in the economy and then aggregate it? This by starting from microeconomics principles! Well because of execution time in my game. So my idea is this one, and I will try to implement it in the near future. If you want to simulate 100.000 people behaviour with microeconomics decisions, just create a different thread that will run in another core, so that I will not affect the real time of the game. Than every second I will compute the behaviour and choises of 2000 people, so that in less every 60 seconds all the behaviour of my 100.000 will be assessed and taken into account. Every second the game will incorporate the decision of the people that have already decided what to do and then the economy will evolve by people that keep deciding through out the game. This will move prices of financial assets and commodities, will make people migrate from countries... in short will simulate all the economical world from a microeconomics prospective rather than a macroeconomics one.

Hope I will be able to implement this and hope I gave you at least a nice idea.

-
Have you considerd downloading this to yor GPGPU, since you are running it in background anyways. Even a moderately old NVIDIA card provides 3072 simultaneous threads today. Check out CUDA on the NVIDIA site. – Pieter Geerkens Mar 7 '13 at 23:43
How inefficient is your code that you cant simulate 100.000 actors? If you said you can't simulate 100M actors, fine. But your approach is the right one, for micro economic sim. Depending on the simulation time vs real time, but each actor in the system does not need to be simulated so often. In real world single actors, trader/banks don't change their strategies radically. This can be used, compute the strategic orientation "once a day" and the tactical decisions "once a hour". Also, you probably have more than two cores to work with. – rioki Mar 8 '13 at 15:06
100k was just a random number I said to describe the idea that if you have a lot of actors to consider individually then it can be convinient (without losing reality) not to compute all their behaviours at the same time but to split it across the time. Moreover you do not know how complex the algorithm that describe behaviour is. With a complex algorithm maybe 100k requires a lot of time to compute (even with an efficient code)! – Marco Mar 8 '13 at 16:07

I agree with Bogdan Marginean that you wouldn't always want to focus on a single person. In case of SimCity it's the group that matters (be it citizens of a neighbourhood). They have a similar problem with commuting, sewage , similar happiness level etc.

However, the flyweight pattern isn't actually applicable here. The common part is immutable. Instead of creating entities that very just a bit it will be better to consider a group as a primary unit.

-