-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiway_partitions.R
105 lines (93 loc) · 2.95 KB
/
multiway_partitions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
library(gurobi)
library(data.table)
library(dplyr)
multiway_partitions <- function(I, K) {
sizes <- data.table(i=1:length(I),size=I)
combos <- data.table(crossing(sizes,k=1:K))
model = list()
A <- NULL; b <- NULL;
sense <- NULL;
current.row=1;
#Each item can only be in one partition
rows <- combos[,i]
columns <- combos[,(i-1)*max(k)+k]
entries <- rep(1,times=combos[,.N])
new.for.b <- rep(1,times=length(I))
new.for.sense <- rep("=",times=length(I))
b <- c(b, c(new.for.b));
sense <- c(sense,new.for.sense)
new.for.A = data.frame(cbind(rows, columns, entries));
A <- rbindlist(list(A,new.for.A), use.names=FALSE, fill=FALSE, idcol=NULL)
new.for.A <- c(); new.for.b <- c(); new.for.sense <- c();
rows <- c(); columns <- c(); entries <- c();
current.row <- max(A$rows)+1
#Define size of each partition
rows <- c(current.row-1 + combos[,k],
current.row-1 + 1:K)
columns <- c(combos[,(i-1)*max(k)+k],
combos[,.N] + 1:K)
entries <- c(combos[,size],
rep(-1,times=K))
new.for.b <- rep(0,times=K)
new.for.sense <- rep("=",times=K)
b <- c(b, c(new.for.b));
sense <- c(sense,new.for.sense)
new.for.A = data.frame(cbind(rows, columns, entries));
A <- rbindlist(list(A,new.for.A), use.names=FALSE, fill=FALSE, idcol=NULL)
new.for.A <- c(); new.for.b <- c(); new.for.sense <- c();
rows <- c(); columns <- c(); entries <- c();
current.row <- max(A$rows)+1
#Break symmetry and define min/max sized partitions
rows <- c(current.row-1 + 1:(K-1),
current.row-1 + 1:(K-1))
columns <- c(combos[,.N] + 1:(K-1),
combos[,.N] + 2:K)
entries <- c(rep(-1,times=K-1),
rep(1,times=K-1))
new.for.b <- rep(0,times=K-1)
new.for.sense <- rep(">=",times=K-1)
b <- c(b, c(new.for.b));
sense <- c(sense,new.for.sense)
new.for.A = data.frame(cbind(rows, columns, entries));
A <- rbindlist(list(A,new.for.A), use.names=FALSE, fill=FALSE, idcol=NULL)
new.for.A <- c(); new.for.b <- c(); new.for.sense <- c();
rows <- c(); columns <- c(); entries <- c();
current.row <- max(A$rows)+1
model$vtype <- c(
rep('B',times=combos[,.N]),
rep('C',times=K))
model$lb <- c(
rep(0,times=combos[,.N]),
rep((sizes[,sum(size)]/K)*0,times=K))
model$ub <- c(
rep(1,times=combos[,.N]),
rep((sizes[,sum(size)]/K)*5,times=K))
model$obj <- c(
rep(0,times=combos[,.N]),
-1,
rep(0,times=K-2),
1
)
model$A <- sparseMatrix(i=A$rows,j=A$columns,x=A$entries)
model$modelsense <- 'min'
model$rhs <- b
model$sense <- sense
rm(A); rm(b); rm(sense);
gc();
solution <- gurobi(model,params=list(
TimeLimit=2700,
Heuristics=0.5,
Threads=6,
PreDepRow=1,
Presolve=2,
PreDual=2,
PrePasses=20,
PreSparsify=1,
Cuts=2,
CutPasses=500,
CutAggPasses=1
))
if(length(solution$x) == length(model$obj)){
return(which(solution$x[1:combos[,.N]] ==1)%%K + 1)
}
}