有没有办法在相邻元素唯一的条件下得到所有排列?
我有以下数据,字母表中的每个字母:
Letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
我的目标是获得长度为 5 的最大排列数,并满足以下 3 个条件:
- 每个排列中字母不得重复一次以上。
- 任何排列在任何位置上都不得与另一个排列共享超过 3 个相同的字母。
- 任何排列不得与另一个排列在相同位置共享超过 2 个相同的相邻字母。
例如排列
['A', 'B', 'C', 'D', 'E']
和['A', 'B', 'F',不应允许使用“G”、“H”]
,因为相同的“A”和“B”在两种排列中都是相邻的。但是,排列['A', 'B', 'C', 'D', 'E']
和['A', 'F', 'B', 'G ', 'H']
应该被允许。
每个排列有 5 个元素,
[元素 1、元素 2、元素 3、元素 4、元素 5]
。元素 1 的邻居仅是元素 2。元素 2 的邻居是元素 1 和元素 3。对于每个排列,相邻对为:[1, 2], [2, 3], [3, 4] ,[4, 5]
。任何排列都不应与另一个 IF 具有相同的相邻对,并且仅当它们位于相同的元素位置时。
另一个类似的示例:
['A', 'B', 'C', 'D', 'E']
和['A', 'B', 'F '、'G'、'H']
。在两个排列的相邻对中,[Element 1, Element 2]
是['A', 'B']
。由于它们在同一元素位置相同,因此该解决方案无效。
如果:
['A', 'B', 'C', 'D', 'E']
和['F', 'A', 'B' ,'G','H']
。在此示例中,尽管它们都有相邻对['A', 'B']
,但它们位于不同的元素位置[Element 1, Element 2]
且分别是[元素2、元素3]
。因此,它们对于该解决方案都是有效的。
我尝试了两种不同的方法来解决此任务 - 使用带有 if 条件的 itertools 和混合整数编程。
在使用 itertools 方面,我无法成功定义条件。我尝试了以下风格的方法,但没有运气:
from itertools import permutations
AllPermutations = list(permutations(Letters, 5))
ActualPermutations = []
for Permutation in AllPermutations:
if Permutation[0:2] not in [Permutation[0:2] for Permutation in ActualPermutations]:
ActualPermutations.append(Permutation)
在使用混合整数编程时,我无法理解目标函数,因为我没有最小化或最大化任何东西。此外,混合整数规划只会给我 1 个符合约束的排列。
I have the following Data, each letter of the Alphabet:
Letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
My goal is to get the maximum number of permutations of a length of 5, with the following 3 conditions:
- No letter shall be repeated more than once in each permutation.
- No permutation shall share more than 3 letters in common with another permutation in any position.
- No permutation shall share more than 2 adjacent letters in common in an identical position with another permutation.
e.g. Permutations
['A', 'B', 'C', 'D', 'E']
and['A', 'B', 'F', 'G', 'H']
should not be permitted as the same 'A' and 'B' are adjacent in both permutations. However, permutations['A', 'B', 'C', 'D', 'E']
and['A', 'F', 'B', 'G', 'H']
should be permitted.
Each permutation has 5 elements,
[Element 1, Element 2, Element 3, Element 4, Element 5]
. The neighbours of Element 1 is only Element 2. The neighbours of Element 2 is Element 1 and Element 3. For each permutation the neighbouring pairs are:[1, 2], [2, 3], [3, 4], [4, 5]
. No permutation should have the same neighbouring pair as another IF and only if they are in the same element position.
Another similar example:
['A', 'B', 'C', 'D', 'E']
and['A', 'B', 'F', 'G', 'H']
. In the neighbouring pair of both permutations,[Element 1, Element 2]
is['A', 'B']
. As they are identical at the same Element location, the solution is not valid.
If however:
['A', 'B', 'C', 'D', 'E']
and['F', 'A', 'B', 'G', 'H']
. In this example, although they both have a neighbouring pair['A', 'B']
, they are found in different Element locations[Element 1, Element 2]
and[Element 2, Element 3]
respectively. Therefore, they are both valid for the solution.
I have tried two different approaches to solve this task - using itertools
with if conditions and mixed integer programming.
In terms of using itertools
, I was not able to successfully define the conditions. I tried the following style of approach, with no luck:
from itertools import permutations
AllPermutations = list(permutations(Letters, 5))
ActualPermutations = []
for Permutation in AllPermutations:
if Permutation[0:2] not in [Permutation[0:2] for Permutation in ActualPermutations]:
ActualPermutations.append(Permutation)
While with mixed integer programming, I was not able to wrap my head around the objective function, as I was not minimizing or maximizing anything. Furthermore, mixed integer programming would only give me 1 permutation that fits with the constraints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能检查一下该解决方案是否符合您的期望吗?如果是这样,我可以提供一些解释。
Can you please check if that solution matches your expectations? If so, I can provide some explanation.