如何从 2 个列表中确定最佳组合
我正在寻找一种方法来使团体中的人员达到最佳组合。让我概述一下情况。
假设我们有 A、B、C 和 D。此外,我们还有组 1、2、3、4 和 5。两者都是示例,可以更少或更多。每个人都给彼此打分。例如,A 对 B 的评分为 3,C 对 C 的评分为 2,依此类推。每个人还对每个组进行评分。 (假设评分为 0-5)。现在我需要某种算法来将这些人均匀地分布在各个组中,同时让他们尽可能快乐(例如:他们应该在一个高评价的组中,并且有高评价的人)。现在我知道人们不可能处于最佳组中(他们评分为 5 的组),但我需要他们处于整个组的最佳解决方案中。
我认为这是一个困难的问题,如果有人可以指导我了解有关此类问题的更多信息,或者帮助我找到我正在寻找的算法,我会很高兴。
谢谢!
编辑: 我看到了很多很好的答案,但这个问题对我来说太大了,无法正确解决。然而,到目前为止发布的答案为我进一步研究这个主题提供了一个很好的起点。非常感谢!
I'm looking for a way to make the best possible combination of people in groups. Let me sketch the situation.
Say we have persons A, B, C and D. Furthermore we have groups 1, 2, 3, 4 and 5. Both are examples and can be less or more. Each person gives a rating to each other person. So for example A rates B a 3, C a 2, and so on. Each person also rates each group. (Say ratings are 0-5). Now I need some sort of algorithm to distribute these people evenly over the groups while keeping them as happy as possible (as in: They should be in a highrated group, with highrated people). Now I know it's not possible for the people to be in the best group (the one they rated a 5) but I need them to be in the best possible solution for the entire group.
I think this is a difficult question, and I would be happy if someone could direct me to some more information about this types of problems, or help me with the algo I'm looking for.
Thanks!
EDIT:
I see a lot of great answers but this problem is too great for me too solve correctly. However, the answers posted so far give me a great starting point too look further into the subject. Thanks a lot already!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在确定这是 NP-Hard 问题后,我建议作为启发式解决方案:人工智能工具。
一种可能的方法是最陡爬坡 [SAHC]
首先,我们将定义我们的效用函数(设为
u
)。它可以是所有群体的总幸福感的总和。接下来,我们定义我们的“世界”:
S 是所有可能分区的组
。对于 S 的每个合法分区 s,我们定义:
next(s)={将一个人转移到不同组的所有可能性}
我们现在要做的就是随机重新启动运行 SAHC:
它是 任何时间算法,这意味着当你给它更多的运行时间时,它会得到更好的结果,最终[在时间无穷大]它会找到最佳结果。
after establishing this is NP-Hard problem, I would suggest as a heuristical solution: Artificial Intelligence tools.
A possible approach is steepest ascent hill climbing [SAHC]
first, we will define our utility function (let it be
u
). It can be the sum of total happiness in all groups.next,we define our 'world':
S is the group of all possible partitions
.for each legal partition s of S, we define:
next(s)={all possibilities moving one person to a different group}
all we have to do now is run SAHC with random restarts:
It is anytime algorithm, meaning it will get a better result as you give it more time to run, and eventually [at time infinity] it will find the optimal result.
这个问题是 NP 难的:你可以从最大三角形填充(即在图中找到至少 k 个顶点不相交的三角形)减少到有 k 个大小为 3 的组的版本,没有人关心他是哪一组并喜欢每个人的 0 或 1。因此,即使是这种非常特殊的情况也很难。
为了解决这个问题,我会尝试使用 ILP:有二进制变量 g_ik 指示该人我在组 k 中,有限制以确保一个人仅在一个组中,并且组具有适当的大小。此外,二元变量 t_ijk 指示人 i 和 j 一起在组 k 中(由 t_ijk <= 0.5 g_ik + 0.5 g_jk 确保),二元变量 t_ij 指示 i 和 j 一起在任何组中(由 t_ij <= 0.5 g_ik + 0.5 g_jk 确保) ;= sum_k t_ijk)。然后,您可以在这些约束下最大化幸福函数。
这个 ILP 有很多变量,但现代求解器非常好,而且这种方法很容易实现。
The problem is NP-hard: you can reduce from Maximum Triangle Packing, that is, finding at least k vertex-disjoint triangles in a graph, to the version where there are k groups of size 3, no one cares about which group he is in, and likes everyone for 0 or for 1. So even this very special case is hard.
To solve it, I would try using an ILP: have binary variables g_ik indicating that person i is in group k, with constraints to ensure a person is only in one group and a group has an appropriate size. Further, binary variables t_ijk that indicate that persons i and j are together in group k (ensured by t_ijk <= 0.5 g_ik + 0.5 g_jk) and binary variables t_ij that indicate that i and j are together in any group (ensured by t_ij <= sum_k t_ijk). You can then maximize the happiness function under these constraints.
This ILP has very many variables, but modern solvers are pretty good and this approach is very easy to implement.
这是优化问题的示例。这是一个非常好的
研究了解决问题的好方法。读
集体智慧编程更好地解释了这一点
比我。
基本上,任何类型的优化问题都分为三个部分。
得分。
现在问题可以描述为寻找产生的解决方案
最高分。为此,您首先需要制定一种格式
表示评分函数可以的可能解决方案
分数。假设6个人(0-5)和3组(0-2),这个python数据结构
会起作用并且是一个可能的解决方案:
人员 0 和 1 被放入组 0,人员 2 和 3 被放入组 1 等等
在。为了给这个解决方案评分,我们需要知道输入和规则
计算输出。输入可以由该数据表示
结构:
列表中的每个列表代表该人给出的评分。为了
例如,在第一行中,人员 0 给人员 0 评分为 0(您
无法评价自己)、4 给第 1 人、1 给第 2 人、3 给 3、4 给 4 以及
1 到 5 号。然后他或她对组 0-2 进行评分 3、1 和 3
分别。
上面是给定输入的有效解决方案的示例。怎么办
我们得分了吗?问题中没有具体说明,只是
需要“最佳”组合,因此我会任意决定
解决方案的分数是每个人幸福感的总和。每个
一个人的幸福感是通过添加他或她对某个事物的评价来确定的
组中每个人的平均评分,
排除人本身。
这是评分函数:
对于给定的解决方案,它应该返回 37.0 分。现在什么
我们要做的就是生成有效的输出,同时跟踪哪个输出
直到我们满意为止是最好的:
在我的计算机上运行它会产生:
显然,您可能认为随机只是一个非常愚蠢的想法
生成解决方案来解决问题,确实如此。有很多
更复杂的方法来生成解决方案,例如模拟
退火或遗传优化。但它们都建立在相同的基础上
框架如上所述。
This is an example of an optimization problem. It is a very well
studied type of problems with very good methods to solve them. Read
Programming Collective Intelligence which explains it much better
than me.
Basically, there are three parts to any kind of optimization problem.
scoring it.
Now the problem can be stated as finding the solution that produces
the highest score. To do that, you first need to come up with a format
to represent a possible solution that the scoring function can then
score. Assuming 6 persons (0-5) and 3 groups (0-2), this python data structure
would work and would be a possible solution:
Person 0 and 1 is put in group 0, person 2 and 3 in group 1 and so
on. To score this solution, we need to know the input and the rules for
calculating the output. The input could be represented by this data
structure:
Each list in the list represents the rating the person gave. For
example, in the first row, the person 0 gave rating 0 to person 0 (you
can't rate yourself), 4 to person 1, 1 to person 2, 3 to 3, 4 to 4 and
1 to person 5. Then he or she rated the groups 0-2 3, 1 and 3
respectively.
So above is an example of a valid solution to the given input. How do
we score it? That's not specified in the question, only that the
"best" combination is desired therefore I'll arbitrarily decide that
the score for a solution is the sum of each persons happiness. Each
persons happiness is determined by adding his or her rating of the
group with the average of the rating for each person in the group,
excluding the person itself.
Here is the scoring function:
It should return a score of 37.0 for the given solution. Now what
we'll do is to generate valid outputs while keeping track of which one
is best until we are satisfied:
Running this on my computer produces:
Obviously, you may think it is a really stupid idea to randomly just
generate solutions to throw at the problem, and it is. There are much
more sophisticated methods to generate solutions such as simulated
annealing or genetic optimization. But they all build upon the same
framework as given above.