# # aquecimento.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: 20040630 # 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 prison <- function(n) { # supomos q nos somos o 1 minha.morte <- numeric(n) for (i in 1:n) { morre <- sample(1:3,1) decisao.carc <- sample((1:3)[-morre],1) minha.morte[i] <- ifelse(morre==1,1,0) } mean(minha.morte) } bars <- function(n) { is.triangle <- function(l) { if ((l[1] + l[2] > l[3]) && (l[1] + l[3] > l[2]) && (l[3] + l[2] > l[1])) { 1 } else { 0 } } a <- runif(n) b <- runif(n) lados <- cbind(pmin(a,b),pmax(a,b)-pmin(a,b),1-pmax(a,b)) outcome <- apply(lados,1,is.triangle) mean(outcome) } jogo <- function(n,rounds,N=100) { game <- function(rounds,N=100) { ganho <- 0 for (i in 1:rounds) { carta.jogador <- sample(0:(N-1),1) if (runif(1) > 0.5) { carta.oponente <- carta.jogador + 1 } else { carta.oponente <- carta.jogador - 1 } if ( carta.oponente > carta.jogador){ ganho <- ganho - carta.oponente } else {ganho <- ganho+ carta.oponente } } ganho/rounds } ganho <- numeric(n) for(i in 1:n) { ganho[i] <- game(rounds,N) } ganho } graficos.jogo<- function(){ x <- jogo(1000,100) hist(x) mean(x) } onib <- function(N,chegada=100,intervalo=rexp,...) { intervalo <- match.fun(intervalo) espera <- numeric(N) for (i in 1:N) { tempo <- 0 n <- 0 while (tempo < chegada) { tempo <- tempo + intervalo(1,...) n <- n+1 } espera[i] <- tempo - chegada } espera } graficos.onibus <- function() { x <- onib(1000,intervalo=runif,min=0,max=3) y <- onib(1000,intervalo=rexp) hist(x,prob=TRUE,xlim=range(x,y)) par(mfrow=c(2,1)) hist(x,prob=TRUE,xlim=range(x,y)) hist(y,prob=TRUE,xlim=range(x,y)) }