基于preference名单分配组(坐3)分配、名单、preference

2023-09-11 23:31:28 作者:依恋冰雪

这是那已经从一个纯粹的算法点开始的问题第三次迭代,而现在已经翻在地绝望的使命找到一个code样品,我可以理解。

我所要做的是创造的先按g 的群体的人的名单中,试图满足尽可能多的preferences越好。每个人给顶的 N 的其他人的名单,他们希望能与分配的未排序列表。它(我的计划)需要考虑相互请求,作为较有影响力的是单向的,而应该(希望)找到接近最优的解决方案。

我需要某种一个code样品(在,真的,任何基于C语言或详细的伪code),这样我也能理解所需要的算法和编写程序。我的第一个问题,我已经确定,这将可能需要某种变体在稳定的婚姻问题,但我一直无法找到一个完整的例子在任伪code,或者我可以理解一个实际的语言。

我问$ P $关于算法pvious问题HERE和HERE,但没有拿出任何东西(我认为这是由于用户对这些SE论坛极低的号码)。现在,我问一个问题,在这里,就希望加上一个编程导向问题高得多的视野率将得到我一个答案。

是的,我当然知道,这样的问题已经被问过,但没有都适用,并回答了。

有谁知道我能做到这一点?

要添加更多的细节(回应评论): 我对人的名单。对于每个人,我有其他人只包括其他人在主力名单中的顶部preferences的列表。没有列表的排序以任何方式。一个preference名单上的一个名字意味着请求愿与请求人。我要寻找一种方式来创建组,我有我的主列表中的每个只含有人指定数量。我想集团以纳入preference名单。所以应尽量把人分在同一小组,如果他们都要求对方在他们的名单。它还应该包括请求,其中一人(人A,在本实施例中)请求别人(人B)中,但B不请求A.在这种情况下,尽管它仍然应该计数的请求,它应该算作低级优先将不是一个共同的要求。

编辑: 此外,根据要求,这里是一个JS函数,将(希望)有助于解释我的目标:

  / *
分数可能的分组
小组将一个物体,其中键是名称和值是preferences阵列。例如:

    {
        人1:人2]
        人2:人1],
        人3:人4],
        4人:人2]
    }

* /
功能getGroupScore(集团){
    变种totalPoints = 0;

    //添加点为每个请求
    对于(VAR人组){
        对(组VAR的请求[人]){
            如果(请求=未定义&安培;!&安培; request.length大于0)
                totalPoints ++;
        }
    }

    //添加两个点,每个双向请求
    对于(VAR人组){
        对(组VAR的请求[人]){
            如果(集团[人] [要求]!=未定义//字符串验证
                &功放;&安培;组[人] [要求] .length> 0 //更多的字符串验证
                &功放;&安培;组[组[人] [要求]!=未定义//阵列验证
                &功放;&安培;组[组[人] [要求]]的indexOf(人)!= -1)//检查相互请求
                    totalPoints + = 2;
        }
    }

    返回totalPoints;
}

/ *
比较两个possble分组
* /
功能compareGroups(A组,B组){
    VAR scoreA = getGroupScore(A组),
        scoreB = getGroupScore(B组);

    如果(scoreA> scoreB)
        返回1;
    否则,如果(scoreA == ScoreB)
        返回0;
    其他
        返回-1;
}
 
分散非车载充电机需要系数研究

解决方案

好,我一直在想你的问题,我想我可以给你一些指导,我虽然不能完全解决这个问题。

首先我会尝试重组你的问题一点,纠正我,如果我错了。

让有成为一个集合P的人,和preferences如表

4人评为A B C D preferences:
   A B C D
 + --------- +
A | X X |
B | X |
C | X X |
D | X |
 + --------- +

在这个例子中,'A'prefers'C','B'prefers'D'等。无论对角线prefers本人与否将变得无关紧要。

现在你的问题是分区P民为G组不同大小为N,假设P = G *ñ。对于N = 2的例子:

  A C B D
 + ------- |
A | X X | |
C | X | X |
 +  -  + ----
B | | X |
D | | x |
 +  -  +  -  +
这里的分区是{A,C}和{B,D}

的程度的分隔是满意是X的对角块的数目,其中在上面的例子中是的总和3 + 2 = 5。

AFAIK,这个问题最相似的,你要解决什么是构建块对角矩阵,或接近一个作为可能的。在你的情况下,对角块之外的值不计入你。

如果您蛮力的问题,那么你是p!/ N!分区检查。对于P和N个大值小的值,这可能是值得直接实现,但这样做的JS会伤害。如果您的解决方案必须是最佳的结果,那么你可能不得不求助于这样的事情。

如果你能允许pretty的好的解决方案,然后随机的方式可能会得到接近。只是做贪心重排,直到没有重排有助于下去,并重复上述过程多次。

我还发现this螺纹在另一个论坛可能帮助你。大多数与此相关的问题,假设其中的每个preference满足一个解决方案,并归结为一个广度优先搜索。显然,如果这是真的,那么结果将是你的最佳解决方案,但因为它不是一般的真正的你可能会做一些繁重的学习修改已知算法。

最美好的祝愿〜

This is the third iteration of a question that had started from a purely algorithmic point of view, and has now turned in to a desperate mission to find a code sample that I can understand.

What I am trying to do is create g groups of a list of people, attempting to satisfy as many preferences as possible. Each person has given an unranked list of top n other people in the list that they would like to be assigned with. It (my program) needs to consider mutual requests as more influential that one-way, and should (hopefully) find something close to the optimal solution.

I want some sort of a code sample (in, really, any C-based language or detailed pseudo-code) so that I can understand the required algorithms and write my program. After my first question, I have determined that this will probably require some sort of variant on the Stable Marriage Problem, but I have been unable to find a full example in either pseudo-code or an actual language that I can understand.

I have asked previous questions about the algorithm HERE and HERE, but have not come up with anything (I think it's due to the extremely low number of users on those SE forums). Now I am asking a question here, on the hope that the much higher view rate coupled with a programming oriented question will get me an answer.

Yes, I do realize that questions like this have been asked before, but none are both applicable and answered.

Does anyone know how I can do this?

To add more detail (in response to comments): I have a list of people. For each person, I have a list of their top preferences of other people that only includes other people in the main list. No lists are sorted in any way. A name on a preference list means that the requester would like to be with the requested person. I am looking for a way to create a specified number of groups, each containing only people that I have on my main list. I want the groupings to incorporate the preference lists. It should try to put people in the same group if they both request each other in their lists. It should also incorporate requests where one person(person A, in this example) requests someone else (person B), but B does not request A. In this case, although it should still count the request, it should be counted as a lower priority to incorporate than a mutual request.

EDIT: Again, as per request, here is a JS function that will (hopefully) help explain my goal:

/*
Scores a possible grouping
group would be an object, where the keys are names and the values are arrays of preferences. Ex:

    {
        "Person 1": ["Person 2"],
        "Person 2": ["Person 1"],
        "Person 3": ["Person 4"],
        "Person 4": ["Person 2"]
    }

*/
function getGroupScore(group) {
    var totalPoints = 0;

    //Add a point for each request
    for (var person in group) {
        for (var request in group[person]) {
            if(request != undefined && request.length > 0)
                totalPoints++;
        }
    }

    //Add two points for each two-way request
    for (var person in group) {
        for (var request in group[person]) {
            if (group[person][request] != undefined //String validation
                && group[person][request].length > 0 //More string validation
                && group[group[person][request]] != undefined //Array validation
                && group[group[person][request]].indexOf(person) != -1) //Check mutual request
                    totalPoints += 2;
        }
    }

    return totalPoints;
}

/*
Compares two possble groupings
*/
function compareGroups(groupA, groupB) {
    var scoreA = getGroupScore(groupA),
        scoreB = getGroupScore(groupB);

    if (scoreA > scoreB)
        return 1;
    else if (scoreA == ScoreB)
        return 0;
    else
        return -1;
}

解决方案

OK I've been thinking on your problem, and I think I can give you some direction, although I can't quite solve it.

First I'm going to try to rephrase your question a little, correct me if I'm wrong.

Let there be a set of P people, and a table of preferences such as:

Preferences of 4 people named A B C D:
   A B C D
 +---------+
A| X   X   |
B|       X |
C|     X X |
D|   X     |
 +---------+

In this example, 'A' prefers 'C', 'B' prefers 'D', etc. Whether a diagonal prefers himself or not will become inconsequential.

Now your problem is to partition the P people into G distinct sets of size N, assuming P=G*N. For N=2 example:

  A C B D
 +-------|
A|x x|   |
C|  x|  x|
 +---+----
B|   |  x|
D|   |x  |
 +---+---+
Here the partition is {A, C} and {B, D}

The degree to which a partitioning is satisfied is the sum of the number of X in the diagonal blocks, which in the above example is 3+2=5.

AFAIK, the problem most similar to what you are trying to solve is to construct a Block Diagonal Matrix, or as close to one as possible. In your case, values outside the diagonal blocks do not count against you.

If you brute force the problem, then you have P!/N! partitions to check. For small values of P and large values of N, this might be worth implementing directly, although doing it in JS will hurt. If your solution has to be the optimal result, then you might have to resort to something like this.

If you can allow for "pretty good" solutions, then a randomized approach will probably get close. Just do greedy rearrangements until no rearrangement helps any longer, and repeat the process several times.

I've also found this thread on another forum which might help you. Most of the problems related to this assume that a solution where every preference is satisfied exists, and that comes down to a breadth first search. Obviously if that was true then the result would be your optimal solution, but since it isn't true in general you will probably have to do some heavy studying to modify the known algorithms.

Best Wishes~

 
精彩推荐