Isn't "Keep people either together or apart" an NP-Hard problem, and basically unsolvable for more than a couple preferences?

Solving this problem to optimality (for whatever constraints and objective function one is interested in -- a precise formulation was not given by the O.P.) is presumably NP-hard indeed, meaning that the computational cost grows exponentially with the size of the problem (in practice: the number of invitees).

However,

1. This is only an asymptotic argument. For "small" size, these problems can be tackled. People routinely solve this type of problem (namely: mixed-integer optimization problems), with up to hundreds of thousands of variables, to proven optimality, in a matter of minutes (see [1]). In this case, it looks like an assignment problem in which the number of variables would be (number of invitees) times (number of tables), so for up to 1000 invitees and 100 tables, there is a chance the problem can be solved optimally.

2. The O.P. is most likely looking heuristically for good solutions, not optimal ones. And I can't really blame them for that. Do we really care about optimality here?

[1] https://plato.asu.edu/ftp/milp.html

Yep, NP-hard! This is using an optimization engine. It's not brute forcing. But hopefully it's getting pretty good approximate results.

You can keep trying harder from the state, just click again on the Optimize buttons.

I admit, I haven't optimized the optimizer yet.

What do you mean by optimization engine? Did you write a solver yourself, or did you use something off-the-shelf? In either case, what are the algorithms involved?

I'm using Optaplanner as the engine under the covers, which is open source and off the shelf.

https://www.optaplanner.org/

It's probably an optimization problem with multiple parameters so you're never looking for an exact solution.

But as the NP-hardness of that problem goes: Say you have 400 guests on 50 tables and you probably expect multiple solutions, you're looking at maybe 20K variables in a SAT encoding. That's an industrial scale SAT problem but very much not untractable.

The question is, whether we would usually expect a number of solutions in the thousands or whether we expect just a handful. I'd guess its the former, as many people on the "cheap tables" are really interchangeable and not subject to many constraints. So usually you'd looking at just a handful of tables and guests that are difficult to place. That begs the question whether the problem OP's trying to solve is really a problem people have difficulties solving (or isn't easily solved by adding a table or two).

I had to write a piece of code to manage 200+ dogs at a kenneling facility, sorted by temperament, across a Gantt chart, with lists of friends and enemies which could stay together or must never be together - coming and going at different times. Additionally, dogs and kennels are both color-coded and certain types of dogs can only stay in certain types of kennels. Some dogs cannot share a kennel at all; kennels have a max limit of 1, 2 or 3 dogs; and some kennels are premium-priced.

In practice, the "optimal" solution depends on what you're optimizing for. In my case, it was maximizing the number of dogs that could board on any given night. This involved looking for open paths - since dogs can be moved around daily if necessary. Then rule out any based on the constraints: Prevent enemy conflict, limit to max occupancy, etc. After that, the preference is for boarding them in a kennel with a friend if possible, however there are trade-offs here in whether this creates too many extra moves... e.g. if a friend is coming in for one night of a dog's 7-night stay, we don't prefer to move. So the third priority, which occasionally overrides the second priority, is minimizing the number of moves which add work for employees. To do that, we look for the longest stretch open at each move branch, ranking them by number of friends descending, barring any with enemies.

As you can imagine, this was way too complicated and branching, so the system prunes the option tree at each necessary move down to a few dozen possibilities and continues down each of those paths, then ranks all the paths for the user to choose from.

In other words, a lot of the decisions for optimizing it (in the non-mathematical sense) were about limiting its output to a manageable number of reasonably good choices. Enemies create a hard limit on the number of spots available. Friends are optional (but good for business).

I was once asked to code a people-matching function where participants had long "don't match me with her" lists and coded a very naive brute force solver that absolutely pegged the CPU on my dev box until I wised up that it was an NP-Hard problem.

Yeah, that's a different and larger problem space. A reservation-maximizing algo doesn't need to come up with a solution for all-vs-all, it only needs to solve for the new reservations you're trying to book vs what's already there; essentially one-vs-all. As it is, searching the tree can take 1-2 seconds in-browser on reasonable hardware, and that's with a good deal of pruning. Trying to massively re-optimize the whole calendar after the fact would be undesirable anyway, considering there are a lot of human choices being made along the way.

That sounds amazingly high tech for a kenneling facility. How did you do the optimization - usign an off-the-shelf solver?

I wrote it completely from scratch, basically a custom type of tree search. Admittedly there are naive aspects and opinionated optimizations and magical constants involved in the final code, but in practice it's worked without any problems for 4-5 years, and it's relatively fast for the amount of data it's churning.

[Edit: An example of a magical constant is the weighting of "friends" over a period of time versus "number of moves" required to achieve optimal friendships]

Impressive. Also hilarious that this kennel is probably better optimized than a lot of big logistics companies. ;0)

I think arbitary weightings are unavoidable when optimizing 'soft' (social) problems like kennel allocation or seating plans. The optimal solution is not well defined.

Thanks! It was quite a fun problem to dive into... the company hired animal behaviorists and put a lot of thought into the "soft" issues that it wanted the software to be able to solve, while allowing for flexible decisions from managers and employees, so it was challenging as understanding the domain... and yes I would say animal management is quite a tricky logistical problem that most people never even consider.

In a larger way, though, 90% of this kind of work is pencil and paper, drawing out all possibilities you can think of, and then asking questions like "what if X and Y are friends staying together, and Y and Z and Q are friends, but X and Z are enemies, and Z+Q is showing up on night 4 of X+Y stay, should Y move in with Z if there are 2 nights left? 3? A majority? Ever?" I would get into long, long conference calls where I would ask them all these scenarios and graph them out before I started writing the code. So the code just expresses what they would hopefully wish to do based on their domain knowledge and best practices. And that is the real trick of optimization... making the clients happy ;)

I have a friend who had 400+ guests and 50+ tables. But they paid by the table, so saving whole tables would have saved lots of money. And friend stayed up all night before wedding stressing about who to put where.

>Isn't "Keep people either together or apart" an NP-Hard problem,

Yes.

>and basically unsolvable for more than a couple preferences?

Our PerfectTablePlan table planner can optimize seating for thousands of guests with thousands of preference constraints (between individuals and groups) on a standard desktop PC/Mac. It uses a genetic algorithm to quickly produce a 'good enough' (but not guaranteed to be optimal) solution. For seating plans it isn't really clear what the optimum is anyway. It is more important that than Bob sits next to Jill or not next to Jack or not next to an empty seats? Debatable.

Finding solutions that are not necessarily optimal but good in practice is often a tractable problem.

For sure: a traveling salesman route under length L, for instance. But what is good enough when combining incompatible people? Less than three vendettas per table? :)

I agree with grandparent that non-optimal solutions are often enough.

But just to be complete, since TSP was mentioned here:

We could solve TSP instances with >80k cities to optimality back in 2006:

https://www.math.uwaterloo.ca/tsp/

The trick is that some types of problems, like TSP or assignment problems, have structure that can be exploited. Of course they remain NP-hard, so as size grows, at some point the computational cost becomes unsustainable. But the point at which this happens is often way further (i.e. for much larger instances) than most imagine.