Learn R Programming

MDP2 (version 2.1.2)

plot.HMDP: Plot the state-expanded hypergraph of the MDP.

Description

Plot the state-expanded hypergraph of the MDP.

Usage

# S3 method for HMDP
plot(x, ...)

Value

No return value (NULL invisible), called for side effects (plotting).

Arguments

x

The MDP model.

...

Arguments passed to plotHypergraph(). Moreover, you may use

  • hyperarcColor: A string. If empty string no colors are used (default). If label then use different colors based on the hyperarc/action labels. If policy then use highlight the current policy.

  • nodeLabel: A string. If empty string, then display node labels (default). If sId then display the state ids. If sId:label then display the state ids together with the label. If sIdx:label then display the state index and the label. If weight then display the node weight.

  • hyperarcShow A string. If all then show all hyperarcs (default). If policy then only show the current policy.

See Also

getHypergraph() and plotHypergraph().

Examples

Run this code
## Set working dir
wd <- setwd(system.file("models", package = "MDP2"))

#### A finite-horizon replacement problem ####
mdp<-loadMDP("machine1_")
plot(mdp)
plot(mdp, hyperarcColor = "label")  # colors based on labels
plot(mdp, hyperarcColor = "label", nodeLabel = "sId:label")  # node labels are 'sId: label'
plot(mdp, nodeLabel = "sIdx:label", radx = 0.02)  # adjust radx in nodes
scrapValues <- c(30, 10, 5, 0)  # scrap values (the values of the 4 states at stage 4)
runValueIte(mdp, "Net reward" , termValues = scrapValues)
plot(mdp, hyperarcColor = "policy")  # highlight optimal policy
plot(mdp, hyperarcShow = "policy", nodeLabel = "weight")  # show only optimal policy


#### An infinite-horizon maintenance problem ####
mdp<-loadMDP("hct611-1_")
plot(mdp)  # plot the first two stages
plot(mdp, hyperarcColor = "label")  # colors based on labels
plot(mdp, hyperarcColor = "label", nodeLabel = "sId:label")  # node labels are 'sId: label'
runPolicyIteAve(mdp,"Net reward","Duration")
plot(mdp, hyperarcColor = "policy")  # highlight optimal policy
plot(mdp, hyperarcShow = "policy")  # show only optimal policy


#### An infinite-horizon hierarchical replacement problem ####
library(magrittr)
mdp<-loadMDP("cow_")
hgf <- getHypergraph(mdp)
# modify labels
dat <- hgf$nodes %>% 
   dplyr::mutate(label = dplyr::case_when(
      label == "Low yield" ~ "L",
      label == "Avg yield" ~ "A",
      label == "High yield" ~ "H",
      label == "Dummy" ~ "D",
      label == "Bad genetic level" ~ "Bad",
      label == "Avg genetic level" ~ "Avg",
      label == "Good genetic level" ~ "Good",
      TRUE ~ "Error"
   ))
# assign nodes to grid ids
dat$gId[1:3]<-85:87
dat$gId[43:45]<-1:3
getGId<-function(process,stage,state) {
   if (process==0) start=18
   if (process==1) start=22
   if (process==2) start=26
   return(start + 14 * stage + state)
}
idx<-43
for (process in 0:2)
   for (stage in 0:4)
      for (state in 0:2) {
         if (stage==0 & state>0) break
         idx<-idx-1
         #cat(idx,process,stage,state,getGId(process,stage,state),"\n")
         dat$gId[idx]<-getGId(process,stage,state)
      }
hgf$nodes <- dat
# modify labels
dat <- hgf$hyperarcs %>% 
   dplyr::mutate(label = dplyr::case_when(
      label == "Replace" ~ "R",
      label == "Keep" ~ "K",
      label == "Dummy" ~ "D",
      TRUE ~ "Error"
   ),
   col = dplyr::case_when(
      label == "R" ~ "deepskyblue3",
      label == "K" ~ "darkorange1",
      label == "D" ~ "black",
      TRUE ~ "Error"
   ),
   lwd = 0.5,
   label = ""
   ) 
hgf$hyperarcs <- dat
# plot hypergraph
oldpar <- par(mai = c(0, 0, 0, 0))
plotHypergraph(gridDim = c(14, 7), hgf, cex = 0.8, radx = 0.02, rady = 0.03)
par(oldpar)

## Reset working dir
setwd(wd)

Run the code above in your browser using DataLab