Watts-College / cpp-527-spr-2022

https://watts-college.github.io/cpp-527-spr-2022/
0 stars 1 forks source link

Lab 2: Issues using provided code from lab 1 solutions #15

Open dholford opened 2 years ago

dholford commented 2 years ago

Apologies for the 2nd post tonight. As I continued to work through the lab, I noticed that I seemed to be getting odd results from the code that was provided in lab 1 solutions for creating a game where the number of goats and cars is dynamic. When I run the simulation for 2 goats and 1 car I'm getting far different results from what I'd expect. Did anyone else have this issue?

build_doors <- function( n ){ return( 1:n ) }

create_game <- function( num.goats, num.cars )
{
    a.game <- sample( x=rep( c("goat","car"), c(num.goats,num.cars) ), 
                      size=(num.goats+num.cars), replace=F )
    return( a.game )
}

select_door <- function( game )
{
  doors <- build_doors( n=length(game) ) 
  a.pick <- sample( doors, size=1 )
  return( a.pick )
}

open_goat_door <- function( game, a.pick )
{
   doors <- build_doors( n=length(game) )
   doors.that.can.be.opened <- doors[ ! ( game == "car" | doors == a.pick ) ]
   opened.door <- sample( doors.that.can.be.opened, size=1 )
   return( opened.door ) # number between 1 and N
}

change_door <- function( stay=T, game, opened.door, a.pick )
{
   doors <- build_doors( length(game) )

   if( stay )
   {
     final.pick <- a.pick
   }
   if( ! stay )
   {
     available.doors <- doors[ doors != opened.door & doors != a.pick ]
     final.pick  <- sample( available.doors, size=1 ) 
   }

   return( final.pick )  # number between 1 and N
}

determine_winner <- function( final.pick, game )
{
   if( game[ final.pick ] == "car" )
   {
      return( "WIN" )
   }
   if( game[ final.pick ] == "goat" )
   {
      return( "LOSE" )
   }
}

Packaging into Simulation

play_game <- function(){
num.goats <- 2
num.cars <- 1
this.game <- create_game( num.goats, num.cars )
my.initial.pick <- select_door( game=this.game )
opened.door <- open_goat_door( this.game, my.initial.pick )

my.final.pick.stay <- change_door( stay=T, game=this.game, 
                                   opened.door=opened.door, a.pick=my.initial.pick )
my.final.pick.switch <- change_door( stay=F, game=this.game,
                                     opened.door=opened.door, a.pick=my.initial.pick )

game.outcome.stay <- determine_winner( final.pick=my.final.pick.stay, game=this.game )
game.outcome.switch <- determine_winner( final.pick=my.final.pick.switch, game=this.game )

strategy <- c("stay","switch")
  outcome <- c(game.outcome.stay,game.outcome.switch)
  game.results <- data.frame( strategy, outcome,
                              stringsAsFactors=F )
  return( game.results )

}

Adding the Loop and Building Tables

results.df <- NULL

for(i in 1:10000){
  game.outcome <- play_game()
  results.df <- rbind(results.df, game.outcome)
}

G2C1 <- table(results.df) %>% 
  prop.table(margin = 1) %>% 
  round(2)

G2C1
  outcome

strategy LOSE WIN stay 0.67 0.33 switch 0.59 0.41

dholford commented 2 years ago

I believe I've narrowed it down, but I can't figure out exactly what the issue is. Currently, in change_doors() something is happening that is allowing a "switch" to select the opened door. Because of this, I'm ending up with games where you lose if you stay or if you switch which is throwing of the win/lose percentage for switch.

My fear is I misread the lab 1 solutions, and this is simply the consequence of taking out the if-else structure that was in place when it was 2 goats and 1 car since this version of the game was intended for multiple cars and more than 2 goats. So now trying to run a game with 2 goats and 1 car doesn't quite work. But it seems like there should be a way to make it dynamic and allow for the original game circumstances, so I tried to play around with this but couldn't figure it out a way to set it up so that if the game length was 3 it would determine the final pick one way and if it was > 3 it would determine the final pick a different way.