Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## app.R ##
- library(shinydashboard)
- library(lpSolve)
- library(lpSolveAPI)
- ui <- dashboardPage(
- dashboardHeader(title = "Basic dashboard"),
- dashboardSidebar(),
- dashboardBody(
- # Boxes need to be put in a row (or column)
- fluidRow(
- #box(
- # numericInput("nbr_people", "Antal personer",value = NULL)
- #),
- #box(
- # numericInput("nbr_activites", "Antal aktiviteter",value = NULL)
- #),
- box(width = 4,
- textInput("person_1", "Person 1",value = "A"),
- numericInput("time_pers_1", "Tillgänglig tid: ", value = 240),
- checkboxGroupInput("act_pers_1", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
- textInput("person_2", "Person 2",value = "B"),
- numericInput("time_pers_2", "Tillgänglig tid: ", value = 240),
- checkboxGroupInput("act_pers_2", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
- textInput("person_3", "Person 3",value = "C"),
- numericInput("time_pers_3", "Tillgänglig tid: ", value = 240),
- checkboxGroupInput("act_pers_3", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
- textInput("person_4", "Person 4",value = "D"),
- numericInput("time_pers_4", "Tillgänglig tid: ", value = 240),
- checkboxGroupInput("act_pers_4", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T),
- textInput("person_5", "Person 5",value = "E"),
- numericInput("time_pers_5", "Tillgänglig tid: ", value = 240),
- checkboxGroupInput("act_pers_5", "Aktiviteter", choices = c("1", "2", "3", "4"), selected = "1", inline = T)
- ),
- box(width = 4,
- numericInput("act_1", "Tid aktivitet 1",value = 50),
- radioButtons("rel_act_1", "Relation aktivitet 1", choices = c("=", "<"), selected = "=", inline = T),
- numericInput("act_2", "Tid aktivitet 2",value = 60),
- radioButtons("rel_act_2", "Relation aktivitet 2", choices = c("=", "<"), selected = "=", inline = T),
- numericInput("act_3", "Tid aktivitet 3",value = 100),
- radioButtons("rel_act_3", "Relation aktivitet 3", choices = c("=", "<"), selected = "=", inline = T),
- numericInput("act_4", "Tid aktivitet 4",value = 40),
- radioButtons("rel_act_4", "Relation aktivitet 4", choices = c("=", "<"), selected = "=", inline = T)
- ),
- box(width = 4,
- #tableOutput("out_opt"),
- tableOutput("out_opt2")
- )
- #,
- #conditionalPanel(
- # condition = "input.nbr_people > ''",
- # box(
- # uiOutput("people_form")
- # )
- #)
- )
- )
- )
- server <- function(input, output) {
- optimering <- reactive({
- #Input
- person <- c(input$person_1, input$person_2, input$person_3, input$person_4, input$person_5)
- pers_time <- c(input$time_pers_1, input$time_pers_2, input$time_pers_3, input$time_pers_4, input$time_pers_5)
- pers_act <- list(input$act_pers_1, input$act_pers_2, input$act_pers_3, input$act_pers_4, input$act_pers_5)
- #if("1" %in% pers_act[[1]]){
- # print("JAAA")
- # print(pers_act[1])
- #}
- #Input activities
- acts_time <- c(input$act_1, input$act_2, input$act_3, input$act_4)
- act_rel <- c(input$rel_act_1, input$rel_act_2, input$rel_act_3, input$rel_act_4)
- #input_act <- cbind(acts, act_rel)
- #Fejk input
- #person <- c("a", "b", "c", "d", "e")
- #pers_time <- rep(240,5)
- #acts_time <- rep(40,4)
- #act_rel <- c("=", "=", "=", "=")
- # Preparation
- nbr_pers <- length(person)
- nbr_act <- length(acts_time)
- # OPTIMIZATION
- nbr_var <- nbr_pers * nbr_act
- lprec <- make.lp(0, nbr_var)
- # Goal function:
- #TODO: Unclear what optimazation should be done?
- #set.objfn(lprec, c())
- # Constraints:
- # Add person time constraints
- for(p in (1:nbr_pers)){
- vector <- c(rep(0, nbr_var))
- for(a in (1:nbr_act)){
- # Check if filled in
- if(a %in% pers_act[[p]]){
- vector[(p-1)*nbr_act+a] = 1
- }
- }
- print(vector)
- print("-------------")
- add.constraint(lprec, vector, "<=", pers_time[p])
- }
- write.lp(lprec, "lpfilename.lp", "lp")
- # Add activity constraints
- for(a in (1:nbr_act)){
- vector <- rep(0, nbr_var)
- for(p in (1:nbr_pers)){
- if(a %in% pers_act[[p]]){
- vector[(p-1)*nbr_act+a] = 1
- #print(vector)
- }
- }
- #print(vector)
- #print("-------------")
- add.constraint(lprec, vector, act_rel[a], acts_time[a])
- }
- # All variables positive
- set.bounds(lprec, lower = c(rep(0,nbr_var)), columns = c(seq(nbr_var)))
- #set.type(lprec, columns = c(seq(nbr_var)), "integer")
- #write.lp(lprec, "lpfilename.lp", "lp")
- #RowNames <- c("THISROW", "THATROW", "LASTROW")
- #ColNames <- c("COLONE", "COLTWO", "COLTHREE", "COLFOUR")
- #dimnames(lprec) <- list(RowNames, ColNames)
- # Solve
- a <- solve(lprec)
- req(a != "2")
- get.objective(lprec)
- variables <- get.variables(lprec)
- #get.constraints(lprec)
- # Present result
- result_table <- NULL
- row_names <- NULL
- col_names <- NULL
- for(p in (1:nbr_pers)){
- activity <- NULL
- for(a in (1:nbr_act)){
- activity <- c(activity, variables[(p-1)*nbr_act+a])
- }
- result_table <- rbind(result_table, activity)
- row_names <- c(row_names,person[p])
- }
- rownames(result_table) <- row_names # TODO: FIX
- colnames(result_table) <- c("A1", "A2", "A3", "A4") # TODO: FIX
- #result_table <- addmargins(result_table, margin = seq_along(dim(result_table)), FUN = sum, quiet = FALSE)
- output <- list(variables, result_table)
- })
- output$out_opt <- renderTable({
- dt <- optimering()[1]
- })
- output$out_opt2 <- renderTable({
- dt <- optimering()[2]
- })
- output$people_form <- renderUI({
- #nbr <- as.numeric(input$nbr_people)
- #if(nbr == 1){
- # output <- textInput("test1", "Person 1", value = "")
- #}
- #if(nbr == 2){
- # output <- tabPanel("",
- # textInput("test1", "Person 1", value = ""),
- # textInput("test2", "Person 2", value = "")
- # )
- #}
- #if(nbr == 3){
- # output <- tabPanel("",
- # textInput("test1", "Person 1", value = ""),
- # textInput("test2", "Person 2", value = ""),
- # textInput("test3", "Person 3", value = "")
- # )
- #}
- #output
- })
- }
- shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement