0

我想在 R 中使用 MDPtoolbox 选择最佳策略并且我使用了闪亮的 R ,我在 global.r 中定义了一个全局变量 state ,如下所示来识别状态:

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

我写了一个 BestAction 的响应函数来选择最佳策略,如下所示:

     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])}
})

BestAction 函数会找到要采取的最佳动作,然后根据当前状态(cs)来选择我想要的策略。

然后我在 renderinfobox 中调用 BestAction() 来显示 put put:

并且警告像for循环一样继续并且永不停止。

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
  )
})

和 ui.r 如下:

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

    infoBoxOutput("finalaction")

  )

但是当我运行代码时,我不断遇到以下错误:

Warning in if (variation < thresh) { :
  the condition has length > 1 and only the first element will be used
4

0 回答 0