我正在尝试在 R 图中显示导航。时间一或 (t1) 中的当前状态设置为 val$currentstatus 并且 (t2) 中的下一个状态希望根据用户从支票簿中选择的操作显示在图表中。然后我想画一条线来显示这条路径。我写的代码如下
output$navigation<-renderPlot({
#initial state of X and Y
if(is.element("Within", vals$currentstatus))
x <- 1
y <- 2
if(is.element("Out", vals$currentstatus)) {
x <- 1
y <- 1
}
action<-c(input$action1,input$action2)
x<-1:4
y<-1:2
rewards<-matrix(rep(0,8),nrow=2)
rewards[1,4]<- -1
rewards[2,4]<- 1
values<-rewards#initial Values
states<-expand.grid(x=x,y=y)
#Transition probability
transition <- list( input$action1= c("within" = 0.8, "out" = .2),
input$action2= c("within" = 0.3, "out" = .7))
# The value of an action (e.g. move toward benchmark )
action.values <- list(input$action1 = c("x" = 1, "y" = 0),
input$action1 = c("x" = 1, "y" = 0))
# act() function serves to move the agent to go to new position
act <- function(action, state) {
action.value <- action.values[[action]]
new.state <- state
#
if(state["x"] == 4 && state["y"] == 1 || (state["x"] == 4 && state["y"] == 2))
return(state)
#
new.x = state["x"] + action.value["x"]
new.y=if(transition["within">"out"]){state["y"==2]}
if(transition["within"<"out"]){state["y"==1]}
}
plot(x, y, xaxt='n',yaxt='n',cex=10,pch=19,
col=ifelse(y==1,"red","green"),ylab="status",xlab="period",
xlim=c(0,4), ylim=c(0,3))
axis(1,at=1:4,labels=c("t1","t2","t3","t4"))
axis(2,at=1:2,labels=c("out bench","within bench"))
如果下一个位置在替补席内,则应为绿色并连接到先前的状态,如果不在替补席上,则应为红色并连接到先前的状态。此外,我想在两个状态之间的连接线上查看所选操作的名称。此外,我想知道如何更新新位置并将其用于计算下一个时期(t3)的下一个状态等。类似于下图: