English 中文(简体)
解决区域任务时间安排或双包装优化
原标题:Solving task scheduling or bin-packing optimizations in R

我有一个选择问题。 它含有20个部分的产品(生产 does的顺序)。 我用3台类似的机器生产所有20个部件。

我看一看20个部分的分数(即生产第一部分为3分钟,生产第二部分为75分钟)。

ItemTime<-c(3,75,55,12,45,55,11,8,21,16,65,28,84,3,58,46,5,84,8,48)

因此,生产1种产品需要730毫升。

sum(ItemTime)

目的是通过向三台机器分配好物品,尽量减少一个产品的生产。

sum(ItemTime/3)

因此,我实际上需要接近243.333 min(730/3)。

可能性巨大

我猜测,解决办法多种多样。 我希望,R能给我大家。 我不需要完全知道需要机器1 2和3的时间: 我还需要知道向机器1、机器2和手提3提供哪些物品。

或者,如果它耗时太长,我想选择一个尽可能合理、不重复的样本......

我能否用R语解决我的问题?

最佳回答

我认为,你的问题是多Knapsack 问题(MKP)的一个近似变体,它是一个先验的问题,不是一件ake事。

然而,你的层面很小,这个问题可以作为一个混合分类方案加以解决。 我用<代码>Rglpk将其解决。 如果你能够接触到CPLEX,我会非常建议你使用code>,否则就会吸烟。

ItemTime<-c(3,75,55,12,45,55,11,8,21,16,65,28,84,3,58,46,5,84,8,48)
N <- length(ItemTime)
M <- 3

Problem formulation:

# variables are in this order:
# z: slack variable for the max of (s1, s2, s3)
# s1: sum of times for machine 1
# s2: sum of times for machine 2
# s3: sum of times for machine 3
# a1-a20: booleans for assignment to machine1
# b1-b20: booleans for assignment to machine1
# c1-c20: booleans for assignment to machine1

obj <- c(1, rep(0, 3 + 3*N))

mat <- rbind(
  c(1, -1, 0, 0, rep(0, M*N)),                      # z >= s1
  c(1, 0, -1, 0, rep(0, M*N)),                      # z >= s2
  c(1, 0, 0, -1, rep(0, M*N)),                      # z >= s3
  c(0, -1, 0, 0, ItemTime,  rep(0, N), rep(0, N)),  # s1 = ...
  c(0, 0, -1, 0, rep(0, N), ItemTime,  rep(0, N)),  # s2 = ...
  c(0, 0, 0, -1, rep(0, N), rep(0, N), ItemTime),   # s3 = ...
  cbind(matrix(0, N, 4), diag(N), diag(N), diag(N)) # a_i + b_i + c_i = 1
)

dir <- c( ">=", ">=", ">=", "==", "==", "==" , rep("==", N))

rhs <- c(rep(0, 2*M), rep(1, N))

types <- c(rep("C", 1+M), rep("B", M*N))

现在请解决:

Rglpk_solve_LP(obj, mat, dir, rhs, types, max=FALSE, verbose=TRUE)

# GLPK Simplex Optimizer, v4.47
# INTEGER OPTIMAL SOLUTION FOUND
# $optimum
# [1] 244
# 
# $solution
#  [1] 244 243 243 244   0   1   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   1   0   0   0   0   1   1   0   0
# [31]   1   1   1   0   1   0   0   0   1   0   1   0   1   0   1   0   0   0   1   1   0   0   0   1   0   1   0   1   0   1
# [61]   0   0   0   1
# 
# $status
# [1] 0
问题回答

<>strong>Edit:显然,这个问题与我所记得的问题略有不同,因为正如“@han”所示,这一算法并不理想,只是近似值(尽管有保证,该算法中的“基数”从4/3起降至4/3,* 总的说来最大化,11/9 是3台机器的最佳选择)。

@han提供的链接与,多处理器编程第条,这完全相当于这一问题。 该条款告诉我们,问题实际上是法国的。 哪一种意思是没有综合时间算法来计算最佳答案(如我们在这里所做的那样,减去O(nlog n))。


你们只能使用贪 gr算法:通过该清单,把最长时间的工作分配给目前分配给它的工作最少的机器。

例如,仅考虑将<代码>c(5,3,6,1,2)作为制造时间。 首先,你将这一分类为递减顺序:c(6,5,3,2,1),然后(为了)分配任务。

     Step:  1    2    3    4    5
Machine 1:  6    6    6    6    6
Machine 2:  -    5    5    5    5,1
Machine 3:  -    -    3    3,2  3,2

因此,机器1占6分钟,机器2占1分钟到5分钟,机器3占3分钟。

You can see that in step 4, the machine with the shortest total time is machine 3 so that is why we assigned the 2 to it.

<>记忆中,这一算法实际上是最佳的;尽管我没有这方面的联系。 我也不知道,你是否能够调整,以取得一切可能的最佳结果。


如果你确定需要一份工作的职能和一份现有工作的机器清单,则你可使用rel=“nofollow”。 唯一的工作任务可能包括:

assign.job <- function(machines, job) {
    which.machines <- which.min(lapply(machines, sum))
    machines[[which.machines]] <- c(machines[[which.machines]], job)
    machines
}

(我不喜欢<代码>机关[单位:]行 我确信,有更好的办法修改一份具体指数的清单。

那么,任务就好像:

allocate <- function(num.machines, job.times) {
    machines <- lapply(1:num.machines, function(...) c())
    Reduce(assign.job,
           sort(job.times, decreasing=TRUE),
           machines)
}

(我不喜欢从<条码>机械化”开始的线路;-: 我确信,制定一份长篇名单(n)的方法不易,但我无法找到。

例如:

> allocate(3,ItemTime)
[[1]]
[1] 84 58 46 45  8  3     # total time: 244

[[2]]
[1] 84 55 55 21 16  8  5  # total time: 244

[[3]]
[1] 75 65 48 28 12 11  3  # total time: 242

最后一个步骤是确定哪些工作相当于什么时间:要么在分配时间后工作落后(这可以在大约线上进行,因为从时间到工作时都有相对简单的绘图,而<><>>>则将“<<>/code>和>>>>> > 加以修改,以跟踪工作指数(如果你将这样做的话, 代码。

(应该指出,这一解决办法比另一个办法要快几倍,因为另一个答案是使用更高的电算法,即<>可支配<>>。 由于Im仍然不能100%确保这一算法是最佳的,因此没有为这一问题提供超高技能。

As noted in other answers this is related to the bin packing problem. A simple bin packing algorithm is already implemented in the BBmisc package; we can apply this existing function here for a simple and fast solution:

library(BBmisc)

binlimit <- 3 # specify how many bins
binsize <- ceiling(sum(ItemTime)/binlimit) # calculate the minimum possible bin size (244)
binPack(ItemTime, binsize) # pack the bins

 [1] 3 1 2 3 3 2 2 3 3 3 2 3 1 3 2 3 3 1 3 3

In this case, it instantly produces an optimal solution using 3 bins. We can confirm the solution s bin sizes:

library(dplyr)
df <- data.frame(ItemTime, bins)
df %>% group_by(bins) %>% summarise (time = sum(ItemTime))

  bins time
1    1  243
2    2  244
3    3  243

如果binPack 产生一种使用太多的双倍数的初步解决办法,则可以将其置于对双倍数的加权上,在binPack的输出中,只有不超过3本的两倍的情况下才能找到解决办法。

Interestingly, binPack returns a solution with the same bin sizes as flodel s answer above, but with different assignments in bins 2 and 3:

   ItemTime Rglpk binPack
1         3     3       3
2        75     1       1
3        55     2       2
4        12     2       3
5        45     3       3
6        55     3       2
7        11     2       2
8         8     2       3
9        21     2       3
10       16     3       3
11       65     2       2
12       28     3       3
13       84     1       1
14        3     3       3
15       58     2       2
16       46     3       3
17        5     2       3
18       84     1       1
19        8     2       3
20       48     3       3

虽然binPack为解决这一问题提供了快捷而简单的方法,但其说明指出,这一算法是简单的,可能不会回到最佳解决办法:

Maps numeric items in x into groups with sum less or equal than capacity. A very simple greedy algorithm is used, which is not really optimized for speed. This is a convenience function for smaller vectors, not a competetive solver for the real binbacking problem. If an element of x exceeds capacity, an error is thrown.





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...