1
$\begingroup$

So I have a little combinatorics question. Certain units in a game I have been playing recently have a system where you get to choose 'modules' to put in them. Those units are allowed to have a fixed number of modules (that is, you must choose exactly $n$ modules). The modules have several types, but the interesting thing is, you are often allowed to have more than one of the same type of module (with additional effects for doing so), but at the same time, there is usually a limit less than $n$ for how many of a given module can be put into the unit. So the question I have is:

How to count the possibilities? For instance, consider the example of a unit called a Cube:

It has 6 modules.

Up to 1 of which may be a Beam module

Up to 3 of which may be Torpedo modules

Up to 1 of which may be Regeneration modules

Up to 3 of which may be Holding Beam modules

Up to 2 of which may be Tactical Armor modules

So we get to choose a total of 6, but can have multiples. For example, you have have 1 beam, 3 torps, 1 regen, and 2 tac armor, or you could have 1 regen, 3 holding beam, and 2 tac armor. Though brute force counting, I have determined that there are 34 distinct possibilities (module order does not matter, but amount per type does), but I wanna know how to figure that out, in general, using combinatorics rather than straight counting the possibilities.

1 Answers 1

1

Generating functions offer a general approach to problems of this type. In your example, let's say $a_r$ is the number of arrangements with $r$ modules. We define $$f(x) = \sum_{r=0}^{\infty} a_r x^r$$ From the constraints on the numbers of modules of each type, we see $$f(x) = (1+x)^2 \; (1+x+x^2) \; (1+x+x^2+x^3)^2$$ Expanding this product, $$f(x) = 1+5 x+13 x^2+24 x^3+34 x^4+38 x^5+34 x^6+24 x^7+13 x^8+5 x^9+x^{10}$$ (I cheated and used a computer algebra system.) The number of arrangements with 6 modules is the coefficient of $x^6$, i.e. 34.

One of the nice things about the generating function is that we immediately see the solution to questions like "How many arrangements are possible with 8 modules?" (13). It's all there in the generating function.