# ----------------- CREATE DATA ----------
# Simulate 200 observations of a 10-state Markov chain,
# and compute fuzzy stationary probability of state 1
library(markovchain) # for simulating from a known crisp Markov chain
# Transition matrix taken from Fig. 1 of Amigoni et al. (see references)
mcPatrol <- new("markovchain", states = robotStates, byrow = TRUE,
transitionMatrix = transRobot, name = "Patrolling")
set.seed(666)
simulatedData <- rmarkovchain(n = 200, object = mcPatrol, t0 =
sample(robotStates, 1))
mcfit = markovchainFit(simulatedData) # Fit with markovchain package
vsteady = steadyStates(mcfit$estimate) # 1 x n matrix of stat. probs
# ---------------------------------------
# Simplest case: compute only alpha-cuts for alpha=0.001 and alpha=0.999
# Set itermax to 30 (too few) just for a fast example (not good results)
linear = fuzzyStationaryProb(simulatedData,list(verbose=TRUE, states="01",
regression="piecewise"), step=1, itermax = 30)
summary(linear)
linear$fuzzyStatProb[["01"]]
plot(linear$fuzzyStatProb[["01"]])
points(linear$acuts[["01"]])
## Not run:
# # A more accurate approximation, with steps of 0.1 (takes much longer!)
# # Run the previous code to create mcPatrol, vsteady and simlatedData
# quadratic = fuzzyStationaryProb(data = simulatedData,list(verbose=TRUE,
# ncores = 2, regression="quadratic"), step=0.1)
# m <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,11),nrow = 4,ncol = 3,byrow = TRUE)
# layout(mat = m,heights = c(0.25,0.25,0.25,0.25))
# for (i in robotStates){
# par(mar = c(4,4,2,1))
# plot(quadratic$fuzzyStatProb[[i]],col="red",main=paste("State ",i),
# cex.lab = 1.1,lwd=2);
# points(quadratic$acuts[[i]]);
# abline(v = vsteady[1,i], lty = "dashed");
# }
# plot(1, type = "n", axes=FALSE, xlab="", ylab="")
# plot_colors <- c("red")
# legend(x = "top",inset = 0, legend = c("Quadratic"), col=plot_colors,
# bty = "n", lwd=2, cex=1, horiz = FALSE)
#
# # Now departing from user-specified fuzzy transition probabilities
# library(FuzzyNumbers)
# EU = TrapezoidalFuzzyNumber(0,0,0.02,0.07); # Extremely unlikely
# VLC = TrapezoidalFuzzyNumber(0.04,0.1,0.18,0.23); # Very low chance
# SC = TrapezoidalFuzzyNumber(0.17,0.22,0.36,0.42); # Small chance
# IM = TrapezoidalFuzzyNumber(0.32,0.41,0.58,0.65); # It may
# MC = TrapezoidalFuzzyNumber(0.58,0.63,0.8,0.86); # Meaningful chance
# ML = TrapezoidalFuzzyNumber(0.72,0.78,0.92,0.97); # Most likely
# EL = TrapezoidalFuzzyNumber(0.93,0.98,1,1); # Extremely likely
# allnumbers = c(EU,VLC,SC,IM,MC,ML,EL);
# names(allnumbers) = c("EU","VLC","SC","IM","MC","ML","EL");
# rownames(linguisticTransitions) = robotStates; # see the package data
# colnames(linguisticTransitions) = robotStates;
#
# # Simplest case: compute only alpha-cuts for alpha=0.001 and alpha=0.999
# # linguisticTransitions is a matrix of strings defined in the package data
# linear = fuzzyStationaryProb(linguisticTransitions,list(verbose=TRUE,
# regression="linear", ncores = 4, fuzzynumbers = allnumbers),step=0.2)
# summary(linear)
# ## End(Not run)
Run the code above in your browser using DataLab