-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKnapsack solution for resource allocation.R
155 lines (124 loc) · 3.86 KB
/
Knapsack solution for resource allocation.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
rm(list = ls()) #clear the variables (just in case)
this_dir <-
dirname(parent.frame(2)$ofile) # find the directory from which the R Script is being sourced
setwd(this_dir)
library(rJava)
.jinit(parameters = "-Xmx16g") # choose system RAM limit
library(xlsx)
library(snow)
library(parallel)
data <-
read.xlsx("laptop allocation.xlsx", 1, stringsAsFactors = FALSE)
colnames(data) <-
c(
"Div",
"Types",
"0",
"<=1",
"<=2",
"<=3",
"<=4",
">4",
"Unit Price of Laptop"
)
data_ratio <- data
#Compute based on ratio of staff to hours required
## Calculate the number of cores
no_cores <- detectCores()
cl <- makeSOCKcluster(no_cores)
clusterExport(cl, list("data_ratio"))
laptop_count_ratio <- clusterApplyLB(
cl,
1:nrow(data_ratio),
fun = function(i) {
temp <- as.numeric(data_ratio[i, 4:8])
number <-
ceiling(temp[1] / 8) + ceiling(temp[2] / 4) + ceiling(temp[3] / 2) + ceiling(temp[4] / 2) + temp[5]
}
)
stopCluster(cl)
laptop_count_ratio <- unlist(laptop_count_ratio)
data_ratio$`No of Laptops Required` <- laptop_count_ratio
data_ratio$`Total Cost` <-
as.numeric(data_ratio$`Unit Price of Laptop`) * as.numeric(data_ratio$`No of Laptops Required`)
print(paste(
"The number of laptops required based on ratio is",
sum(data_ratio$`No of Laptops Required`)
))
print(paste("The total cost is $", sum(data_ratio$`Total Cost`)))
#Compute the number of high-end laptops needed per div for heavy users
data_h <- data[which(data$Types == "H"),]
## Calculate the number of cores
no_cores <- detectCores()
cl <- makeSOCKcluster(no_cores)
clusterExport(cl, list("data_h"))
laptop_count_h <- clusterApplyLB(
cl,
1:nrow(data_h),
fun = function(i) {
library(adagio)
temp <- as.numeric(data_h[i, 4:8])
count <- 0
#Solving knapsack problem iteratively
w <- rep(1:4, times = c(temp[1], temp[2], temp[3], temp[4]))
p <-
rep(c(0.1, 0.25, 0.5, 0.7), times = c(temp[1], temp[2], temp[3], temp[4]))
k <- 8
if (length(w) != 0)
is <- knapsack(w, p, k)
while (length(w) != 0) {
count <- count + 1
w <- w[-is$indices]
p <- p[-is$indices]
if (length(w) != 0)
is <- knapsack(w, p, k)
}
count <- count + temp[5]
}
)
stopCluster(cl)
laptop_count_h <- unlist(laptop_count_h)
data_h$`No of Laptops Required` <- laptop_count_h
data_h$`Total Cost` <-
as.numeric(data_h$`Unit Price of Laptop`) * as.numeric(data_h$`No of Laptops Required`)
data_l <- data[which(data$Types == "L"),]
## Calculate the number of cores
no_cores <- detectCores()
cl <- makeSOCKcluster(no_cores)
clusterExport(cl, list("data_l"))
laptop_count_l <- clusterApplyLB(
cl,
1:nrow(data_l),
fun = function(i) {
library(adagio)
temp <- as.numeric(data_l[i, 4:8])
count <- 0
#Solving knapsack problem iteratively
w <- rep(1:4, times = c(temp[1], temp[2], temp[3], temp[4]))
p <-
rep(c(0.1, 0.25, 0.5, 0.7), times = c(temp[1], temp[2], temp[3], temp[4]))
k <- 8
if (length(w) != 0)
is <- knapsack(w, p, k)
while (length(w) != 0) {
count <- count + 1
w <- w[-is$indices]
p <- p[-is$indices]
if (length(w) != 0)
is <- knapsack(w, p, k)
}
count <- count + temp[5]
}
)
stopCluster(cl)
laptop_count_l <- unlist(laptop_count_l)
data_l$`No of Laptops Required` <- laptop_count_l
data_l$`Total Cost` <-
as.numeric(data_l$`Unit Price of Laptop`) * as.numeric(data_l$`No of Laptops Required`)
data <- rbind(data_h, data_l)
data <- data[order(as.numeric(rownames(data))), ]
print(paste(
"The least number of laptops required based on constraints and resources is",
sum(data$`No of Laptops Required`)
))
print(paste("The total cost has been reduced to $", sum(data$`Total Cost`)))