Algorithm for optimal distribution of objects on a numberline

1k Views Asked by At

I need to distribute objects with a defined width on a numberline, which is already populated with other objects. There should be no overlap of objects and I have several constraints. E.g. no two green blocks in a row. The objective is to distribute them in a way that all colored blocks are as left on the numberline as possible. The following picture illustrates this problem.

How to fit the colored blocks in the gaps between the grey ones

Is there a name for problems like this, and where to look for solutions and algorithms?

1

There are 1 best solutions below

0
On

The basic form of this problem seems like the multiple knapsack problem. The idea of this problem is to fit items with certain weights and values into multiple knapsacks each with certain weight limitations in such a way as to maximize value. If you just want to fit as many items as possible, you can set all the values equal (say $\$1$) and then maximizing the value is equivalent to maximizing the number of items.

In your case we can think of the knapsacks as the spaces between the given blocks (with knapsack capacity equal to the space between the blocks), and the items as the colored blocks with weight equal to the size of the blocks.

I don't know what to do about your other constraints, but this should be a good start.