library('ggplot2')
library('reshape2')

source('fns.R')


mkScoreWorker <- function(stratTable,scorePlan,pA) {
  # copy into captured environment
  force(stratTable)
  force(scorePlan)
  force(pA)
  function(pB) {
    scorePlan(stratTable,pA,pB)
  }
}

cl <- parallel::makeCluster(4)

doit <- function(workList,worker) {
  if(is.null(cl)) {
    lapply(workList,worker)
  } else {
    parallel::parLapply(cl,workList,worker)
  }
}


pA <- 0.01

demos = list('100 trials, 1$ payoff (bounded prior)'=buildBayesPlan(100,pA,
                             alpha0=pA/2,beta0=(1-pA/2),pBbound=2*pA),
             '10000 trials, 1$ payoff (bounded prior)'=buildBayesPlan(10000,pA,
                             alpha0=pA/2,beta0=(1-pA/2),pBbound=2*pA),
             '10000 trials, 1$ payoff (uninformative prior)'=buildBayesPlan(10000,pA),
             '10000 trials, 1$ payoff (optimistic prior)'=buildBayesPlan(10000,pA,
                             alpha0=10,beta0=100))
for(dname in names(demos)) {
  print("********************")
  print(dname)
  stratTable <- demos[[dname]]
  maxSteps <- length(stratTable)
  soln <- data.frame(nBTrials=0:(maxSteps-1),decisionCount=stratTable)
  
  print(head(soln))
  
  soln$cutRate <- soln$decisionCount/pmax(1,soln$nBTrials)
  
  #print(soln)
  print(ggplot(soln,aes(x=nBTrials,y=cutRate)) + 
    geom_ribbon(aes(ymin=cutRate),ymax=pA,alpha=0.5,fill='green') +
    geom_ribbon(aes(ymax=cutRate,ymin=0),alpha=0.5,fill='red') +
    geom_line() + geom_point() +
    ggtitle(paste(dname,"\nsensing plan (in rates)")))

  print(ggplot(soln,aes(x=nBTrials,y=decisionCount)) +
          geom_ribbon(aes(ymin=decisionCount),ymax=maxSteps,alpha=0.5,fill='green') +
          geom_ribbon(aes(ymax=decisionCount,ymin=0),alpha=0.5,fill='red') +
          geom_line() + geom_point() + 
          ggtitle(paste(dname,"\nesensing plan (in counts)")))
  
  if(maxSteps>1000) {
    print(ggplot(soln,aes(x=nBTrials,y=cutRate)) + 
            geom_ribbon(aes(ymin=cutRate),ymax=pA,alpha=0.5,fill='green') +
            geom_ribbon(aes(ymax=cutRate,ymin=0),alpha=0.5,fill='red') +
            geom_line() + geom_point() + coord_cartesian(xlim = c(0,1000)) +
            ggtitle(paste(dname,"\n\nearly sensing plan (in rates)")))
    
    print(ggplot(soln,aes(x=nBTrials,y=decisionCount)) +
            geom_ribbon(aes(ymin=decisionCount),ymax=maxSteps,alpha=0.5,fill='green') +
            geom_ribbon(aes(ymax=decisionCount,ymin=0),alpha=0.5,fill='red') +
            geom_line() + geom_point() + 
            coord_cartesian(xlim = c(0,1000), ylim = c(0,10)) +
            ggtitle(paste(dname,"\nearly sensing plan (in counts)")))
  }
  comparisonFrame <- data.frame(pB=seq(from=0,to=2*pA,length.out=25))
  comparisonFrame$theoreticalValue <- pmax(comparisonFrame$pB,pA)*maxSteps
  res <- doit(comparisonFrame$pB,mkScoreWorker(stratTable,scorePlan,pA))
  comparisonFrame$stratValue <- as.numeric(res)
  comparisonFrame$efficiency <- comparisonFrame$stratValue/comparisonFrame$theoreticalValue
  print(ggplot(comparisonFrame,aes(x=pB,y=efficiency)) + 
    geom_hline(y=1.0,color='blue',linetype=2) +
    geom_line() + 
    ggtitle(paste(dname,"\nefficiency versus (unobtainable) ideal")))
  
  comparisonFrame$improvement <- comparisonFrame$stratValue/(pA*maxSteps)
  print(ggplot(comparisonFrame,aes(x=pB,y=improvement)) + 
    geom_hline(y=1.0,color='blue',linetype=2) +
    geom_line() + 
    ggtitle(paste(dname,"\nvalue relative to pA")))
  print("********************")
}
## [1] "********************"
## [1] "100 trials, 1$ payoff (bounded prior)"
##   nBTrials decisionCount
## 1        0           Inf
## 2        1             1
## 3        2             1
## 4        3             1
## 5        4             1
## 6        5             1