Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. id <- 1:100
  2. min <- sample(0:500, 100)
  3. weight <- (1:100)/sum(1:100)
  4. data <- data.frame(id, min, weight)
  5.  
  6. require(lpSolveAPI)
  7.  
  8. #Set up matrix to hold results; each row represents day
  9. r <- 5
  10. c <- 10
  11. row <- 1
  12.  
  13. results <- matrix(0, nrow = r, ncol = c)
  14. rownames(results) <- format(seq(Sys.Date(), by = "days", length.out = r), "%Y-%m-%d")
  15.  
  16. for (i in 1:r){
  17. for(j in 1:c){
  18. lp <- make.lp(0, nrow(data))
  19. set.type(lp, 1:nrow(data), "binary")
  20. set.objfn(lp, rep(1, nrow(data)))
  21. lp.control(lp, sense = "max")
  22. add.constraint(lp, data$min, "<=", 400)
  23. set.branch.weights(lp, data$weight)
  24.  
  25. solve(lp)
  26. a <- get.variables(lp)*data$id
  27. b <- a[a!=0]
  28.  
  29. tryCatch(results[row, 1:length(b)] <- b, error = function(x) 0)
  30.  
  31. if(dim(data[!data$id == a,])[1] > 0) {
  32. data <- data[!data$id== a,]
  33. row <- row + 1
  34. }
  35. break
  36.  
  37. }
  38. }
  39.  
  40. sum(results > 0)
  41.  
  42. barplot(results) #View of scheduled IDs
  43.  
  44. id min weight
  45. 1 67 1
  46. 2 72 2
  47. 3 36 3
  48. 4 91 4
  49. 5 80 5
  50. 6 44 6
  51. 7 76 7
  52. 8 58 8
  53. 9 84 9
  54. 10 96 10
  55. 11 21 11
  56. 12 1 12
  57. 13 41 13
  58. 14 66 14
  59. 15 89 15
  60. 16 62 16
  61. 17 11 17
  62. 18 42 18
  63. 19 68 19
  64. 20 25 20
  65. 21 44 21
  66. 22 90 22
  67. 23 4 23
  68. 24 33 24
  69. 25 31 25
  70.  
  71. Day 1 67 72 36 91 80 44 76
  72. Day 2 58 84 96 21 1 41 66 89
  73. Day 3 62 11 42 68 25 44 90 4 33 31
  74.  
  75. df = read.table(header=T,text="
  76. id min weight
  77. 1 67 1
  78. 2 72 2
  79. 3 36 3
  80. 4 91 4
  81. 5 80 5
  82. 6 44 6
  83. 7 76 7
  84. 8 58 8
  85. 9 84 9
  86. 10 96 10
  87. 11 21 11
  88. 12 1 12
  89. 13 41 13
  90. 14 66 14
  91. 15 89 15
  92. 16 62 16
  93. 17 11 17
  94. 18 42 18
  95. 19 68 19
  96. 20 25 20
  97. 21 44 21
  98. 22 90 22
  99. 23 4 23
  100. 24 33 24
  101. 25 31 25")
  102. # assume sorted by weight
  103.  
  104. daynr = 1
  105. daymax = 480
  106. dayusd = 0
  107. for (i in 1:nrow(df))
  108. {
  109. v = df$min[i]
  110. dayusd = dayusd + v
  111. if (dayusd>daymax)
  112. {
  113. daynr = daynr + 1
  114. dayusd = v
  115. }
  116. df$day[[i]] = daynr
  117. }
  118.  
  119. > df
  120. id min weight day
  121. 1 1 67 1 1
  122. 2 2 72 2 1
  123. 3 3 36 3 1
  124. 4 4 91 4 1
  125. 5 5 80 5 1
  126. 6 6 44 6 1
  127. 7 7 76 7 1
  128. 8 8 58 8 2
  129. 9 9 84 9 2
  130. 10 10 96 10 2
  131. 11 11 21 11 2
  132. 12 12 1 12 2
  133. 13 13 41 13 2
  134. 14 14 66 14 2
  135. 15 15 89 15 2
  136. 16 16 62 16 3
  137. 17 17 11 17 3
  138. 18 18 42 18 3
  139. 19 19 68 19 3
  140. 20 20 25 20 3
  141. 21 21 44 21 3
  142. 22 22 90 22 3
  143. 23 23 4 23 3
  144. 24 24 33 24 3
  145. 25 25 31 25 3
  146. >
  147.  
  148. > solve(lp)
  149. [1] 0
  150. > x <- get.variables(lp)
  151. > weightx <- data$weight * x
  152. > sum(x)
  153. [1] 14
  154. > sum(weightx)
  155. [1] 0.5952381
  156.  
  157. > solve(lp)
  158. [1] 0
  159. > x <- get.variables(lp)
  160. > weightx <- data$weight * x
  161. > sum(x)
  162. [1] 14
  163. > sum(weightx)
  164. [1] 0.7428571
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement