我想在R中选择使用MDPtoolbox的最佳策略,我使用了闪亮的R,我在global.r中定义了一个全局变量状态,如下所示:
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