#class <- "u-1000-10-1000.txt"; O <- race("wrapper-race.R", maxExp=5000,stat.test=c("friedman"), conf.level=0.95, first.test=5, interactive=TRUE, log.file="race.log", no.slaves=0)

race.init<-function(){
  ## define a working directory. Better use full path
  wd <- "/home/marco/Teaching/Fall2008/DM811/Project/Submissions/"
  
  instances<-scan(file=class,what=as.character(0),skip=0,quiet=TRUE)
  instances<-scan(file=class,what=as.character(0),skip=0,quiet=TRUE)
  instances<-scan(file=class,what=as.character(0),skip=0,quiet=TRUE)
  instances<-scan(file=class,what=as.character(0),skip=0,quiet=TRUE)
  
  n<-length(instances) ##number.of.instances

  # Set the random seed so that, if running under PVM, all the 
  # slaves use the same data partition for the cross-validation
  if (exists(".Random.seed"))
    save.seed<-.Random.seed
  set.seed(0)
  smpl<-sample(n)  
  seed <- sample(n)
  ## Restore the random seed
  if(exists("save.seed",inherits=FALSE))
    assign(".Random.seed",save.seed,envir=globalenv())
  else
    rm(".Random.seed",envir=globalenv())

  # Prepare a data.frame defining the candidates
  candidates<- as.data.frame(rbind(
                                   c(label="300787",path="300787/src",command="Driver -tt 30 -ch 2 -ls 3"),
                                   c(label="100884",path="100884/",command="dm811e/Forced"),
                                   c(label="060386",path="060386/src",command="Mss"),
                                   c(label="281182",path="281182/src",command="FirstImprovement"),
                                   c(label="181180",path="181180/src",command="mss"),
                                   c(label="061184",path="061184/src",command="mss"),
                                   c(label="250684",path="250684/src",command="MSS"),
                                   c(label="200775",path="200775/src",command="mss"),
                                   c(label="070982",path="070982/src",command="mss")
                                   ))

  # Return init list
  return(list(name=class,
              no.candidates=nrow(candidates),
              no.tasks=n, #or stages
              ## unreplicated: no.tasks = num. of instances
              no.subtasks=1, 
              ## unreplicated: no.subtasks = 1
              wd=wd,
              seed=seed,
              smpl=smpl,
              instances=instances,
              candidates=candidates))
}


race.info<-function(data)
  return(list(race.name="Model Selection: ",data$name,
              no.candidates=(data$no.candidates),
              no.tasks=data$no.tasks,
              extra=paste("This is a very simple example that shows",
                "how to use the 'race' package.",
                "By using a",
                paste(data$no.tasks,"-fold",sep=""),
                "cross-validation,",
                "we optimize the structure of a neural network.",
                "In this example, we optimize the number of nodes",
                "and the decay.  See the help of function 'nnet'",
                "in the homonymous package.",
                "We limit here the number of iteration of the",
                "training process for didactical reasons.")))


race.wrapper<-function(candidate,task,data){
  # We should check the parameters are of expected type,
  # are not out of range, etc... but this is just a quick example :)

  ### Evaluate the given candidate on the given task:

   namehost <- system("hostname",intern=TRUE,ignore.stderr=TRUE)

  instance <- data$instances[data$smpl[task]]

   zz <- file(paste("Results/",data$name,"-",namehost,".res",sep=""), "a")  # open an output file connection


time=100;
if (data$candidates[candidate,]$label %in% c("200775","181180")) {
        command <- paste("",
                    data$candidates[candidate,]$command,
                   " -i ",instance,
                   " -t ",time,
                   " -s ",data$smpl[task],
                   " -o ",paste(candidate,task,1,sep="-"),
                   " 2>/dev/null", sep="")
     }
    else {
   command <- paste("",
                   "java ",
                    data$candidates[candidate,]$command,
                   " -i ",instance,
                   " -t ",time,
                   " -s ",data$smpl[task],
                   " -o ",paste(candidate,task,1,sep="-"),
                   " 2>/dev/null", sep="")
 }


   d <- getwd()
   setwd(paste(data$wd,data$candidates[candidate,]$path,sep=""))


    if (.Platform$OS.type=="unx") {
    dev.null<-file("/dev/null",open="w");
    sink(dev.null)
  }

   #cat(command)
   system(command,intern=TRUE,ignore.stderr=TRUE)

   command2 <- paste("~/Teaching/Fall2008/DM811/Project/Programs/Checker/vmss ",
                     instance,
                     paste(candidate,task,1,sep="-"))
   s <- -1 * as.integer(system(command2,intern=TRUE,ignore.stderr=TRUE))
   #cat(command)
   setwd(d)
   


 if (.Platform$OS.type=="unx"){
    sink(NULL);close(dev.null)}

     pr <- paste(as.character(data$candidates[candidate,]$label),
             basename(data$instances[data$smpl[task]]),
             data$smpl[task],
                 s,"\n", sep=" ")
   cat(pr,file=zz)
   close(zz)

   return(s)

}


race.describe<-function(candidate,data)
  return(data$candidates[candidate,])

