-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectionCode.R
57 lines (38 loc) · 1.2 KB
/
SelectionCode.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
### Replicating the costellization function in R
## Read the data
library(readxl)
library(tidyverse)
library(lpSolve)
file_path <- "GP2017jc.xlsx"
points <- read_excel(file_path, sheet = "Point Allocations", col_names = T)
fmat <- dplyr::select(points, -c(1,2)) %>%
as.matrix()
B <- read_excel(path = file_path, sheet = "Project Maximums", col_names = T) %>%
select(Points) %>%
as.matrix()
k <- dim(B)[1] # Number of projects
N <- dim(fmat)[1]
if (dim(fmat)[2] != k){
print('Matrices are not entered correctly')
}
f <- t(fmat) %>%
as.data.frame() %>%
gather(Project, Points) %>%
select(Points)
A0 <- diag(1, k)
A <- A0
for (i in 1:(N-1)){
A <- cbind(A, A0)
}
Aeq <- matrix(0, N, N*k)
for (i in 1:N) {
Aeq[i, (i*k-k+1):(i*k)] = 1
}
Beq <- matrix(1, N, 1)
const.mat <- rbind(A, Aeq)
const.dir <- c(rep("<=", k), rep("==", N))
const.rhs <- rbind(B, Beq)
solution <- lp(direction = "max", objective.in = f, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs, all.bin = T)
sol_out <- cbind(points$`Last Name`, points$`First Name`, matrix(solution$solution, N, k, byrow = T))
colnames(sol_out) <- colnames(points)
write.csv(x = sol_out, file = "Solutions.csv", row.names = F)