R 带约束的一致分裂序列

R 带约束的一致分裂序列,r,algorithm,R,Algorithm,我希望(大致)具有包含/排除约束的一致分割序列。我在R中寻找这样的函数(因此,在R中描述),但它认为这也是一个很好的算法问题:) 以下设置描述了该问题 ## size n = 5 # length of a sequence m = 3 # number of groups ## constraints # inclusion constraints # same index (for example, first two elements) must be in a same group.

我希望(大致)具有包含/排除约束的一致分割序列。我在R中寻找这样的函数(因此,在R中描述),但它认为这也是一个很好的算法问题:)

以下设置描述了该问题

## size
n = 5 # length of a sequence
m = 3 # number of groups

## constraints
# inclusion constraints
# same index (for example, first two elements) must be in a same group.
ic = c(1,1,2,3,4)
# exclusion constraints
# same index (for example, last two elements) must not be in a same group.
ec = c(1,2,3,4,4)

## run
g = uniform_split(n, m, ic, ec) # the function I am looking for!

# possible g when m = 3:
g = c(1,1,2,2,3) # most preferred
g = c(1,1,3,2,3) # most preferred
g = c(1,1,1,2,3) # satisfies constraints, but less balanced.

# possible g when m = 2:
g = c(1,1,2,1,2) # most preferred
g = c(1,1,2,2,1) # most preferred
g = c(1,1,1,1,2) # satisfies constraints, less balanced
g = c(1,1,1,2,1) # satisfies constraints, less balanced
如图所示,可能的序列可能不仅仅是一个,甚至可能不存在。因此,结果不需要完全平衡,也不需要给出所有可能的选择。如果可能的话,一个大致一致的结果就足以满足我的需要。然而,分组的质量可以通过向量的范数来定义:
距离=(组中的计数(1),组中的计数(2),…组中的计数(m))-(n/m,n/m,…,n/m)

此外,您可以假设以下情况,以简化(或可能使问题复杂化)

## size
n = 5 # length of a sequence
m = 3 # number of groups

## constraints
# inclusion constraints
# same index (for example, first two elements) must be in a same group.
ic = c(1,1,2,3,4)
# exclusion constraints
# same index (for example, last two elements) must not be in a same group.
ec = c(1,2,3,4,4)

## run
g = uniform_split(n, m, ic, ec) # the function I am looking for!

# possible g when m = 3:
g = c(1,1,2,2,3) # most preferred
g = c(1,1,3,2,3) # most preferred
g = c(1,1,1,2,3) # satisfies constraints, but less balanced.

# possible g when m = 2:
g = c(1,1,2,1,2) # most preferred
g = c(1,1,2,2,1) # most preferred
g = c(1,1,1,1,2) # satisfies constraints, less balanced
g = c(1,1,1,2,1) # satisfies constraints, less balanced

  • m在合并必须合并在一起的项(并添加它们的权重)之后,该问题是一个分区问题,该分区中存在关于项的一些边约束。由于分区通常是NP难的,我将首先尝试一种贪婪策略,该策略依次考虑不能共存的最大项目组,并将它们打包到负载最少的分区中(最重到最小、第二重到第二轻,等等)。先做“困难”组(先做最大总重量)。如果R有一个合适的优先级队列,那么这应该足够快

    也许有更聪明的组合优化算法,但考虑到开环调度的局限性,我怀疑它们不值得