Skip to content
Advertisement

Too many combinations

Hi I’m trying to generate all possible combinations of workers to buildings. (let me explain my scenario):

I’m playing MineColonies on minecraft. In this mod you have colonists whom can be assigned jobs at buildings. These workers have skills and a score assigned to them. (like Agility: 20, Strength:5 etc) and the work at the buildings are performed better when assigned a colonists whose skills compliment it…

So I’ve created a database of all the workers and buildings and want to optimize which workers work at which buildings.

JavaScript

Sorry for the long code block and yes I realize the ids aren’t necessarily correct(wanted to make it reproducible).

So I’m using itertools.permutations to get all combinations of workers to buildings:

JavaScript

(I plan to score the combinations afterwards)

This of evidently has never completed running since it’s something like 27! = 1×10²⁸. I’m wondering whether there’s another solution for my problem or a way to determine the best solution without going through every combination. (I’m willing to work in other coding languages)

Thanks!

Advertisement

Answer

I am assuming that you want to maximize the sum of total production. For example, when no workers are assigned, the total production is zero (or some constant that does not depend on worker assignment). If you pair up a worker with Agility 2 and Focus 3 with a building with attributes [Agility, Focus], you add 2+3=5 to the total production.

Problems like this are usually solved with linear programming. I will use pulp to help formulate the linear programming problem. I would also recommend checking out the Julia package JuMP.

The actual rule for computing total production can be more complicated. You can still use linear programming if (1) you can define some analog of the production matrix and (2) total production can be expressed as sum of productions of (worker, building) pairs.

Here are two ways to solve this problem. The first allows multiple workers per building, the second does not.

Setup

JavaScript

Allowing Multiple Workers per Building

JavaScript

At most one worker per building

JavaScript

We can see that the total production is higher when we allow multiple workers per building. This is expected, since the maximization problem has fewer constraints.

We can also compare the optimized production to production when workers are assigned to buildings at random. Vertical lines correspond to optimal production. It looks like we are doing alright.

enter image description here

Advertisement