简体   繁体   English

从最大分散重复元素的列表列表中找到最佳组合

[英]Finding the optimal combination from a list of lists that maximally disperses repeating elements

I have a list of lists, where each individual list represents which people are available to take a work schedule shift.我有一个列表列表,其中每个单独的列表代表哪些人可以进行工作时间表轮班。 My actual data is for a list of 50 shifts:我的实际数据是 50 个班次的列表:

[['P3', 'P2', 'P4', 'P5'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1'], ['P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P3', 'P2', 'P4', 'P5'], ['P3', 'P2', 'P4', 'P5'], ['P2', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P5'], ['P2'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P2', 'P4'], ['P1', 'P2', 'P4', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P2', 'P5'], ['P1', 'P3', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P [['P3', 'P2', 'P4', 'P5'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1'], ['P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5' ', 'P2', 'P4'], ['P3', 'P2', 'P4', 'P5'], ['P3', 'P2', 'P4', 'P5'], ['P2 ', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2' ', 'P4'], ['P5'], ['P2'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2' ', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3' ', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4' '],['P1','P3','P4'],['P1','P3','P2','P4'],['P1','P2','P4'],[ 'P1', 'P2', 'P4', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P2', 'P5'], [ 'P1', 'P3', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5' ', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P 5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P2'], ['P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4']] 5'、'P2'、'P4']、['P1'、'P3'、'P5'、'P2'、'P4']、['P2']、['P4']、['P1' , 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', ' P5','P2','P4'],['P1','P3','P5','P2','P4'],['P1','P3','P5','P2' , 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1' , 'P3', 'P2', 'P4']]

I am trying to end in a final list of 50 (ie, ['P3', 'P2', 'P4', 'P1', 'P5', 'P2', ...] with either one of two criteria:我试图以 50 的最终列表结束(即 ['P3', 'P2', 'P4', 'P1', 'P5', 'P2', ...] 有两个标准之一:

Option A: Repeating elements are maximally dispersed选项 A:重复元素最大程度地分散

Option B: Each repeating element is at a minimum spaced apart by 2 index spaces, ie that ['P1', 'P1'] or ['P1', 'P2', 'P1] are not acceptable, but ['P1, 'P2', 'P3', 'P1'] is ok.选项 B:每个重复元素至少间隔 2 个索引空间,即 ['P1', 'P1'] 或 ['P1', 'P2', 'P1] 不可接受,但 ['P1, 'P2', 'P3', 'P1'] 没问题。

I don't know how to approach Option A tbh.我不知道如何处理选项 A tbh。 For Option B, here is what I have so far:对于选项 B,这是我目前所拥有的:

AssignList = []
for ix, x in enumerate(AvailableList):
    randx = random.choice(x)
    if len(AssignList)==1:
        while randx==AssignList[0]:
            randx=random.choice(x)
    if len(AssignList)>1:
        while (randx==AssignList[-1]) | (randx==AssignList[-2]):
            randx=random.choice(x)
    AssignList.append(randx)
print(AssignList)

But the problem with my approach is I think it reaches some lists where there is one choice that causes an infinite loop.但是我的方法的问题是我认为它到达了一些列表,其中有一个选择会导致无限循环。 Any tips for either approach much appreciated!非常感谢任何一种方法的提示!

To implement Option A, I suggest introducing a score or penalty that is assigned to a list.为了实施选项 A,我建议引入分配给列表的分数或罚分。 Depending on how you define "maximally dispersed", you can select the right score or penalty function.根据您如何定义“最大分散”,您可以对 select 进行正确的分数或罚分 function。 By choosing a proper one, Option B can be automatically satisfied.通过选择一个合适的选项,可以自动满足选项 B。 Consider the following system: you assign a penalty point based on:考虑以下系统:您根据以下条件分配罚分:

  1. Penalty(list) = 1/(2-max(dist,2)) , where dist is the distance of the closest elements of the same type Penalty(list) = 1/(2-max(dist,2)) ,其中dist是同类型最近元素的距离
  2. Penalty(list) = Σ_i 1/(2-max(dist_i,2) , and i goes on the same index as P_i, and dist_i is the smallest distance of the P_i instances within the list. Penalty(list) = Σ_i 1/(2-max(dist_i,2) ,并且 i 与 P_i 的索引相同,并且 dist_i 是列表中 P_i 实例的最小距离。
  3. Penalty(list) = Σ_i 1/pow(2-max(dist_i,2),2) , where you apply pow(...,2) on the previous one. Penalty(list) = Σ_i 1/pow(2-max(dist_i,2),2) ,您将 pow(...,2) 应用于前一个。

You can assign the penalty points to each list, and choose the ones with the lowest penalty.您可以将罚分分配给每个列表,并选择罚分最低的那些。 Where distance is 1 or 2, the penalty is infinity.当距离为 1 或 2 时,惩罚为无穷大。

From the 1st to the 2nd, you consider not only the closest 2 points, but you also consider other points which are more distant, and their penalty will also contribute less.从第 1 到第 2,您不仅考虑最近的 2 分,还考虑距离较远的其他点,他们的点球也将贡献较少。

From the 2nd to the 3rd, you penalize closeness even further.从第 2 次到第 3 次,你会进一步惩罚亲密关系。

You can also normalize the penalty, bc it is now trivial that the more points you have, the more penalty you give.您也可以将惩罚标准化,因为现在您拥有的分数越多,您给予的惩罚就越多,这已经是微不足道的了。 Or maybe you don't want to consider this effect, but from this point, it is up to you, what your penalty points look like.或者也许你不想考虑这种影响,但从这一点来看,你的罚分是什么样的取决于你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM