% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/writer.R
\name{hmpMDPWriter}
\alias{hmpMDPWriter}
\title{Function for writing an HMDP model to a hmp file (XML). The function define
sub-functions which can be used to define an HMDP model stored in a hmp file.}
\usage{
hmpMDPWriter(
  file = "r.hmp",
  rate = 0.1,
  rateBase = 1,
  precision = 1e-05,
  desc = "HMP file created using hmpMDPWriter in R",
  getLog = TRUE
)
}
\arguments{
\item{file}{The name of the file storing the model (e.g. \code{r.hmp}).}

\item{rate}{The interest rate (used if consider discounting).}

\item{rateBase}{The time where the \code{rate} is taken over, e.g. if the \code{rate} is 0.1 and \code{rateBase} is 365 days
then we have an interest rate of 10 percent over the year.}

\item{precision}{The precision used when checking if probabilities sum to one.}

\item{desc}{Description of the model.}

\item{getLog}{Output log text.}
}
\value{
A list of functions.
}
\description{
HMP files are in XML format and human readable using e.g. a text editor.
HMP files are not suitable for storing large HMDP models since text files are very
verbose. Moreover, approximation of the weights and probabilities may occur since
the parser writing the hmp file may no output all digits. If you consider large
models then use the binary file format instead.
}
\details{
The returned writer exposes these functions:
\itemize{
\item \code{setWeights(labels, duration)}: sets the labels of the weights used in the
actions. \code{labels} is a vector of label names. \code{duration} identifies which
label corresponds to duration or time. For example, if the first entry in
\code{labels} is time, then \code{duration = 1}. Call this before building the model.
\item \code{setTransWeights(labels)}: sets the labels of transition-level weights.
\item \code{process()}: starts a (sub)process.
\item \code{endProcess()}: ends a (sub)process.
\item \code{stage(label = NULL)}: starts a stage.
\item \code{endStage()}: ends a stage.
\item \code{state(label = NULL)}: starts a state and returns the state index \code{sIdx}.
\item \code{endState()}: ends a state.
\item \code{action(label = NULL, weights, prob, statesNext = NULL, transWeights = NULL)}: starts an
action. \code{weights} must be a vector of action weights, and \code{prob} must
contain triples \verb{(scope, idx, pr)}. \code{scope} can take three values:
\itemize{
\item \code{0}: a transition to the next stage in the father process.
\item \code{1}: a transition to the next stage in the current process.
\item \code{2}: a transition to a child process, at stage zero in the child process.
}

The \code{idx} value denotes the index of the state at the stage considered. For
example, if \code{scope = 1} and \code{idx = 2}, the transition is to state number 3
at the next stage in the current process, counting from zero. \code{scope = 3}
is not supported in the \code{hmp} file format. \code{statesNext} is the number of
states in the next stage of the process and is only needed when there is a
transition to the father.
\item \code{endAction()}: ends an action.
\item \code{closeWriter()}: closes the writer. Call this when the model description is
finished.
}
}
\note{
Note all indexes are starting from zero (C/C++ style).
}
\examples{
## Use temp dir
wd <- setwd(tempdir())

## Create a small HMDP with two levels
w<-hmpMDPWriter()
w$setWeights(c("Duration","Net reward","Items"), duration=1)
w$process()
  w$stage()
   w$state(label="M0")
     w$action(label="A0",weights=c(0,0,0),prob=c(2,0,1))
      w$process()
        w$stage()
         w$state(label="D")
           w$action(label="A0",weights=c(0,0,1),prob=c(1,0,0.5,1,1,0.5))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(0,0,0),prob=c(1,0,1))
           w$endAction()
           w$action(label="A1",weights=c(1,2,1),prob=c(1,0,0.5,1,1,0.5))
           w$endAction()
         w$endState()
         w$state(label="C1")
           w$action(label="A0",weights=c(0,0,0),prob=c(1,0,1))
           w$endAction()
           w$action(label="A1",weights=c(1,2,1),prob=c(1,0,0.5,1,1,0.5))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
         w$endState()
         w$state(label="C1")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
         w$endState()
        w$endStage()
      w$endProcess()
     w$endAction()
     w$action(label="A1",weights=c(0,0,0),prob=c(2,0,1))
      w$process()
        w$stage()
         w$state(label="D")
           w$action(label="A0",weights=c(0,0,1),prob=c(1,0,1))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(0,0,0),prob=c(1,0,1))
           w$endAction()
           w$action(label="A1",weights=c(1,2,1),prob=c(1,0,0.5,1,1,0.5))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
         w$endState()
         w$state(label="C1")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
           w$action(label="A1",weights=c(0,10,5),prob=c(0,0,0.5,0,1,0.5), statesNext=0)
           w$endAction()
         w$endState()
        w$endStage()
      w$endProcess()
     w$endAction()
   w$endState()
   w$state(label="M1")
     w$action(label="A0",weights=c(0,0,0),prob=c(2,0,1))
      w$process()
        w$stage()
         w$state(label="D")
           w$action(label="A0",weights=c(0,0,1),prob=c(1,0,0.5,1,1,0.5))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(0,0,0),prob=c(1,0,1))
           w$endAction()
         w$endState()
         w$state(label="C1")
           w$action(label="A0",weights=c(0,0,0),prob=c(1,0,1))
           w$endAction()
         w$endState()
        w$endStage()
        w$stage()
         w$state(label="C0")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
         w$endState()
         w$state(label="C1")
           w$action(label="A0",weights=c(1,4,0),prob=c(0,0,1), statesNext=0)
           w$endAction()
         w$endState()
        w$endStage()
      w$endProcess()
     w$endAction()
   w$endState()
  w$endStage()
w$endProcess()
w$closeWriter()

## Have a look at the hmp file
cat(readr::read_file("r.hmp"))

## Reset working dir
setwd(wd)
}
