Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for finding the best routes for food distribution in game

I'm designing a city building game and got into a problem.

Imagine Sierra's Caesar III game mechanics: you have many city districts with one market each. There are several granaries over the distance connected with a directed weighted graph. The difference: people (here cars) are units that form traffic jams (here goes the graph weights).

Note: in Ceasar game series, people harvested food and stockpiled it in several big granaries, whereas many markets (small shops) took food from the granaries and delivered it to the citizens.

The task: tell each district where they should be getting their food from while taking least time and minimizing congestions on the city's roads.

Map example

Example graph diagram

Suppose that yellow districts need 7, 7 and 4 apples accordingly. Bluish granaries have 7 and 11 apples accordingly.

Suppose edges weights to be proportional to their length. Then, the solution should be something like the gray numbers indicated on the edges. Eg, first district gets 4 apples from the 1st and 3 apples from the 2nd granary, while the last district gets 4 apples from only the 2nd granary.

Here, vertical roads are first occupied to the max, and then the remaining workers are sent to the diagonal paths.

Question

What practical and very fast algorithm should I use? I was looking at some papers (Congestion Games: Optimization in Competition etc.) describing congestion games, but could not get the big picture.

like image 472
TautrimasPajarskas Avatar asked May 10 '10 18:05

TautrimasPajarskas


4 Answers

You want to look into the Max-flow problem. Seems like in this case it is a bipartite graph, which should make things easier to visualize.

like image 120
Larry Avatar answered Oct 21 '22 07:10

Larry


This is a Multi-source Multi-sink Maximum Flow Problem which can easily be converted into a simple Maximum Flow Problem by creating a super source and a super sink as described in the link. There are many efficient solutions to Maximum Flow Problems.

like image 34
mathmike Avatar answered Oct 21 '22 06:10

mathmike


One thing you could do, which would address the incremental update problem discussed in another answer and which might also be cheaper to computer, is forget about a globally optimal solution. Let each villager participate in something like ant colony optimization.

Consider preventing the people on the bottom-right-hand yellow node in your example from squeezing out those on the far-right-hand yellow node by allowing the people at the far-right-hand yellow node to bid up the "price" of buying resources from the right-hand blue node, which would encourage some of those from the bottom-right-hand yellow node to take the slightly longer walk to the left-hand blue node.

like image 30
Doug McClean Avatar answered Oct 21 '22 08:10

Doug McClean


I agree with Larry and mathmike, it certainly seems like this problem is a specialization of network flow.

On another note, the problem may get easier if your final algorithm finds a spanning tree for each market to its resources (granaries), consumes those resources greedily based on shortest path first, then moves onto the next resource pile.

It may help to think about it in terms of using a road to max capacity first (maximizing road efficiency), rather than trying to minimize congestion.

This goes to the root of the problem - in general, it's easier to find close to optimal solutions in graph problems and in terms of game dev, close to optimal is probably good enough.

Edit: Wanted to also point out that mathmike's link to Wikipedia also talks about Maximum Flow Problem with Vertex Capacities where each of your granaries can be thought of as vertices with finite capacity.

like image 26
reshen Avatar answered Oct 21 '22 07:10

reshen