# # DTMC.R - A set of R functions for simulating M/M/k queues # #Copyright (C) 2004 Fernando Henrique F. P. Rosa # Vagner Aparecido Pedro Junior # # #This program is free software; you can redistribute it and/or #modify it under the terms of version 2 of the GNU General Public License #as published by the Free Software Foundation. A copy of this license should #be included in the file COPYING. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program; if not, write to the Free Software #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # # Última data de edição: 20050915 # Descrição: esse conjunto de funções simula diversos processos de fila # M/M/k. Para descrição da motivação e algum desenvolvimento teórico veja: # http://www.feferraz.net/files/lista/mae312-trabalho.pdf estima.Pn <- function(n,sistema,inicial,final) { sucessos <- 0 L <- length(sistema) if (n > L) { stop('unable to estimate over more steps than sampled.') } tamanho.janela <- n N <- L-n for (i in 1:N) { a <- sistema[i] b <- sistema[i+n] if (a != inicial) { N <- N -1 } else if (b == final) { sucessos <- sucessos + 1 } } sucessos/N } estima.matriz.ineficiente <- function(space,n=1,na.action=na.omit) { na.action <- match.fun(na.action) space <- na.action(space) trans <- matrix(rep(0,length(unique(space))^2),ncol=length(unique(space))) colnames(trans) <- sort(unique(space)) row.names(trans) <- sort(unique(space)) for (i in seq(along=unique(space))) { for (j in seq(along=unique(space))) { trans[i,j] <- estima.Pn(n,space,sort(unique(space))[i],sort(unique(space))[j]) } } trans } simula <- function(trans,N) { transita <- function(char,trans) { sample(colnames(trans),1,prob=trans[char,]) } sim <- character(N) sim[1] <- sample(colnames(trans),1) for (i in 2:N) { sim[i] <- transita(sim[i-1],trans) } sim } estima.matriz <- function(space,na.action=na.omit) { na.action <- match.fun(na.action) space <- na.action(space) trans <- matrix(.C("estima_matriz",as.integer(factor(space)),as.integer(length(space)),as.integer(length(unique(space))),trans=double(length(unique(space))^2),DUP=FALSE)$trans,ncol=length(unique(space)),byrow=T) colnames(trans) <- sort(unique(space)) row.names(trans) <- sort(unique(space)) trans } le.espaco <- function(file) { scan(file,what=character(),n=-1,sep="\n") } cat.simulairc <- function(v,file="") { v <- gsub("^N(.*)N$","<\\1>",v) v <- gsub("CR","\n",v) v <- paste(v,collapse=' ') cat(v,file=file) } cat.simulatxt <- function(v,file="") { v <- gsub("CR","\n",v) v <- paste(v,collapse=' ') cat(v,file=file) }