## M likelihood profile
MPROF = TRUE
source("src/sesam.R")

objs <- list()
opts <- list()
mults <- seq(0.5,2,by=0.05)


for(mm in 1:length(mults)){
    cat("mm = ",mm,"\n")
    tmpdata = data
    tmpdata$auxM<-mults[mm] * tmpdata$auxM
    
    objs[[mm]] <- MakeADFun(tmpdata,param, random=c("logF","logN","logR"), DLL="sesam", inner.control=list(maxit=1000),map=mymap)

    opts[[mm]] <- nlminb(objs[[mm]]$par, objs[[mm]]$fn, objs[[mm]]$gr, control=list(trace=1,eval.max=1200,iter.max=900),lower=lower,upper=upper) 
    
}

lls = sapply(opts,function(x) (ifelse(opt$convergence==0,x$objective,NA)))

cat("Best multiplier: ",mults[which.min(lls)],"\n")

save(lls,mults,file="run/mprof.RData")

source("src/plotscript.R")
