0

I want to select best policy using MDPtoolbox in R and I have used shiny R , I defined a global variable of state in global.r as follows to identify the states:

state<-c("s0","s1","s2","s3")

I wrote a reactive function of BestAction to select the best policy as follows:

     BestAction<-reactive({
    P <- array(0, c(4,4,4)) 
    P[,,1] <- matrix(c(0.5, 0.5, 0, 0,0.5, 0.5, 0, 0,0.5, 0.5, 0, 0,0.5, 0.5, 0, 0), 4, 4, byrow=TRUE)
    P[,,2] <- matrix(c(0, 1, 0, 0,0.5, 0.5, 0, 0,0.5, 0.5, 0, 0,0.5, 0.5, 0, 0), 4, 4, byrow=TRUE)
    P[,,3] <- matrix(c(0, 0, 0.8, 0.2,0, 0.8, 0.2,0,0.5, 0, 0,0.5,0.5, 0, 0,0.5), 4, 4, byrow=TRUE)
    P[,,4] <- matrix(c(0, 1, 0, 0,0, 1, 0, 0,0, 0, 0, 1,0, 0, 0.1, 0.9), 4,4, byrow=TRUE)
      R <- matrix(c(5, 10, -1, 2,5, 10, -1, 2,5, 10, -1, 2,5, 10, -1, 2), 4, 4, byrow=TRUE)
      pol<-mdp_value_iteration(P, R,0.9,c(0,0,0,0))

pol <-pol$policy
cs<-"s0"
    if(state[1]==cs){
          str1<-paste( "action ",pol[1])   
          }else if(state[2]==cs){

         str2<-paste("action ", pol[2])

        }else if (state[3]==cs){    
           str3<-paste("action ", pol[3])

            }
            else {str4<-paste( "action ",pol[4])}
})

The BestAction function will find the best action to take, and then based on the current state (cs) I want to select the policy.

Then I called BestAction() in a renderinfobox to show the put put:

and the warning continues like a for loop and never stop.

output$finalaction <- renderInfoBox({
  infoBox(
    "The best policy is to take",  HTML(paste( BestAction(), sep='<br/>')), icon = icon("hand-rock-o"),
    color = "purple", fill = TRUE,width = 50
  )
})

and ui.r is as follows:

fluidRow(
  box(
    title = "Best Policy to take", status = "primary", solidHeader = TRUE,
    collapsible = TRUE,

    infoBoxOutput("finalaction")

  )

Bu when I run the code I face to the following error continuously:

Warning in if (variation < thresh) { :
  the condition has length > 1 and only the first element will be used
user
  • 592
  • 6
  • 26
  • 1
    Difficult to debug since `if(variation – neilfws Mar 29 '17 at 21:45
  • I can update my code but because it is written in shiny R I used some reactive function and I do not know if I have to provide all of functions here ? – user Mar 29 '17 at 21:48
  • 2
    You get the error because `if` can only evaluate a logical vector of length 1. You could use nested `ifelse` instead. – Miha Mar 29 '17 at 21:50
  • I add some codes to revise the questions based on your comments – user Mar 29 '17 at 22:10

0 Answers0