Assigning people to buildings while respecting preferences?

前端 未结 3 1527
醉酒成梦
醉酒成梦 2021-02-05 09:51

A friend asked me a question today about an assignment problem. I found a quite straightforward solution, but I feel that it can be made simpler and faster. Your help would be a

3条回答
  •  轮回少年
    2021-02-05 10:38

    templatetypedef gave a very nice proof of why the problem is NP-hard and has no (known) polynomial time solution.

    However as with many NP-hard problems, that does not mean you cannot solve it efficiently in practice or that you cannot improve upon a brute force solution.

    In particular, you should look into constraint satisfaction problems. They are a more general class of problems that describe precisely what you are trying to solve: Given a set of constraints, what are the values that satisfy all the constraints?

    The book AIMA has a very nice chapter on how to solve these types of problems. I suggest you read up on that as there is a lot of good information there and it's very accessible as the book was designed for the undergraduate level. I'll give you some of the big ideas here:

    The major questions are:

    • Which student should be assigned next in your recursion?
    • In what order should the buildings be considered for that student?

    Here are two heuristics for this:

    • Minimum remaining values (MRV) heuristic: When choosing which student to assign to a building next in your recursion, choose the student with the fewest choices left.
    • Least constraining value (LCV) heuristic: After deciding what student to look at, assign the building that rules out the fewest choices for the remaining students

    For the MRV heuristic, break ties by choosing the student that has the most constraints on the other students. The idea behind these heuristics is that you want to pick search paths that are most likely to succeed (LCV). But given a particular search path, you want to fail as early as possible to reduce the amount of time spent on that path (MRV).

    Also, instead of naive recursion with basic forward checking, you should use a more advanced search algorithm like AC-3 that looks farther ahead.

    Seeing as constraint satisfaction problems are so common in many software engineering applications, there are already a ton of open libraries out there that solve can solve them efficiently. Of course, this is given that the problem you're trying to solve is for a real-world application and not a homework assignment of sorts.

提交回复
热议问题