Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. ## app.R ##
  2. library(shinydashboard)
  3. library(lpSolve)
  4. library(lpSolveAPI)
  5.  
  6. ui <- dashboardPage(
  7. dashboardHeader(title = "Basic dashboard"),
  8. dashboardSidebar(),
  9. dashboardBody(
  10. # Boxes need to be put in a row (or column)
  11. fluidRow(
  12. #box(
  13. # numericInput("nbr_people", "Antal personer",value = NULL)
  14. #),
  15. #box(
  16. # numericInput("nbr_activites", "Antal aktiviteter",value = NULL)
  17. #),
  18. box(width = 4,
  19. textInput("person_1", "Person 1",value = "A"),
  20. numericInput("time_pers_1", "Tillgänglig tid: ", value = 240),
  21. checkboxGroupInput("act_pers_1", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
  22. textInput("person_2", "Person 2",value = "B"),
  23. numericInput("time_pers_2", "Tillgänglig tid: ", value = 240),
  24. checkboxGroupInput("act_pers_2", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
  25. textInput("person_3", "Person 3",value = "C"),
  26. numericInput("time_pers_3", "Tillgänglig tid: ", value = 240),
  27. checkboxGroupInput("act_pers_3", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
  28. textInput("person_4", "Person 4",value = "D"),
  29. numericInput("time_pers_4", "Tillgänglig tid: ", value = 240),
  30. checkboxGroupInput("act_pers_4", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
  31. textInput("person_5", "Person 5",value = "E"),
  32. numericInput("time_pers_5", "Tillgänglig tid: ", value = 240),
  33. checkboxGroupInput("act_pers_5", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T)
  34. ),
  35. box(width = 4,
  36. numericInput("act_1", "Tid aktivitet 1",value = 50),
  37. radioButtons("rel_act_1", "Relation aktivitet 1", choices = c("=", "<"), selected = "=", inline = T),
  38. numericInput("act_2", "Tid aktivitet 2",value = 60),
  39. radioButtons("rel_act_2", "Relation aktivitet 2", choices = c("=", "<"), selected = "=", inline = T),
  40. numericInput("act_3", "Tid aktivitet 3",value = 100),
  41. radioButtons("rel_act_3", "Relation aktivitet 3", choices = c("=", "<"), selected = "=", inline = T),
  42. numericInput("act_4", "Tid aktivitet 4",value = 40),
  43. radioButtons("rel_act_4", "Relation aktivitet 4", choices = c("=", "<"), selected = "=", inline = T)
  44. ),
  45. box(width = 4,
  46. #tableOutput("out_opt"),
  47. tableOutput("out_opt2")
  48. )
  49. #,
  50. #conditionalPanel(
  51. # condition = "input.nbr_people > ''",
  52. # box(
  53. # uiOutput("people_form")
  54. # )
  55. #)
  56. )
  57. )
  58. )
  59.  
  60. server <- function(input, output) {
  61.  
  62. optimering <- reactive({
  63. #Input
  64. person <- c(input$person_1, input$person_2, input$person_3, input$person_4, input$person_5)
  65. pers_time <- c(input$time_pers_1, input$time_pers_2, input$time_pers_3, input$time_pers_4, input$time_pers_5)
  66. pers_act <- list(input$act_pers_1, input$act_pers_2, input$act_pers_3, input$act_pers_4, input$act_pers_5)
  67.  
  68. #if("1" %in% pers_act[[1]]){
  69. # print("JAAA")
  70. # print(pers_act[1])
  71. #}
  72.  
  73. #Input activities
  74. acts_time <- c(input$act_1, input$act_2, input$act_3, input$act_4)
  75. act_rel <- c(input$rel_act_1, input$rel_act_2, input$rel_act_3, input$rel_act_4)
  76. #input_act <- cbind(acts, act_rel)
  77.  
  78. #Fejk input
  79. #person <- c("a", "b", "c", "d", "e")
  80. #pers_time <- rep(240,5)
  81. #acts_time <- rep(40,4)
  82. #act_rel <- c("=", "=", "=", "=")
  83.  
  84. # Preparation
  85. nbr_pers <- length(person)
  86. nbr_act <- length(acts_time)
  87.  
  88. # OPTIMIZATION
  89. nbr_var <- nbr_pers * nbr_act
  90. lprec <- make.lp(0, nbr_var)
  91. # Goal function:
  92. #TODO: Unclear what optimazation should be done?
  93. #set.objfn(lprec, c())
  94. # Constraints:
  95. # Add person time constraints
  96. for(p in (1:nbr_pers)){
  97. vector <- c(rep(0, nbr_var))
  98. for(a in (1:nbr_act)){
  99. # Check if filled in
  100. if(a %in% pers_act[[p]]){
  101. vector[(p-1)*nbr_act+a] = 1
  102. }
  103. }
  104. print(vector)
  105. print("-------------")
  106. add.constraint(lprec, vector, "<=", pers_time[p])
  107. }
  108. write.lp(lprec, "lpfilename.lp", "lp")
  109. # Add activity constraints
  110. for(a in (1:nbr_act)){
  111. vector <- rep(0, nbr_var)
  112. for(p in (1:nbr_pers)){
  113. if(a %in% pers_act[[p]]){
  114. vector[(p-1)*nbr_act+a] = 1
  115. #print(vector)
  116. }
  117. }
  118. #print(vector)
  119. #print("-------------")
  120. add.constraint(lprec, vector, act_rel[a], acts_time[a])
  121. }
  122.  
  123. # All variables positive
  124. set.bounds(lprec, lower = c(rep(0,nbr_var)), columns = c(seq(nbr_var)))
  125. #set.type(lprec, columns = c(seq(nbr_var)), "integer")
  126. #write.lp(lprec, "lpfilename.lp", "lp")
  127. #RowNames <- c("THISROW", "THATROW", "LASTROW")
  128. #ColNames <- c("COLONE", "COLTWO", "COLTHREE", "COLFOUR")
  129. #dimnames(lprec) <- list(RowNames, ColNames)
  130.  
  131. # Solve
  132. a <- solve(lprec)
  133. req(a != "2")
  134. get.objective(lprec)
  135. variables <- get.variables(lprec)
  136. #get.constraints(lprec)
  137.  
  138. # Present result
  139. result_table <- NULL
  140. row_names <- NULL
  141. col_names <- NULL
  142. for(p in (1:nbr_pers)){
  143. activity <- NULL
  144. for(a in (1:nbr_act)){
  145. activity <- c(activity, variables[(p-1)*nbr_act+a])
  146. }
  147. result_table <- rbind(result_table, activity)
  148. row_names <- c(row_names,person[p])
  149. }
  150. rownames(result_table) <- row_names # TODO: FIX
  151. colnames(result_table) <- c("A1", "A2", "A3", "A4") # TODO: FIX
  152.  
  153. #result_table <- addmargins(result_table, margin = seq_along(dim(result_table)), FUN = sum, quiet = FALSE)
  154.  
  155. output <- list(variables, result_table)
  156. })
  157.  
  158. output$out_opt <- renderTable({
  159. dt <- optimering()[1]
  160. })
  161.  
  162. output$out_opt2 <- renderTable({
  163. dt <- optimering()[2]
  164. })
  165.  
  166. output$people_form <- renderUI({
  167. #nbr <- as.numeric(input$nbr_people)
  168. #if(nbr == 1){
  169. # output <- textInput("test1", "Person 1", value = "")
  170. #}
  171. #if(nbr == 2){
  172. # output <- tabPanel("",
  173. # textInput("test1", "Person 1", value = ""),
  174. # textInput("test2", "Person 2", value = "")
  175. # )
  176. #}
  177. #if(nbr == 3){
  178. # output <- tabPanel("",
  179. # textInput("test1", "Person 1", value = ""),
  180. # textInput("test2", "Person 2", value = ""),
  181. # textInput("test3", "Person 3", value = "")
  182. # )
  183. #}
  184. #output
  185. })
  186. }
  187.  
  188. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement