### To launch:
###
###  O <- race("wrapper-race-ex1.R", maxExp=640,stat.test=c("friedman"),
###           conf.level=0.95, first.test=40, interactive=TRUE,
###           log.file="race.log", no.slaves=0)
###
###  save(O,file="results-race.dataR")
###


############################################################
race.init<-function(){
  class <- "500"
  wd <- path.expand("./")
### remove old result file
  unlink(paste(wd,class,".res",sep=""))

### Define Instances
  ##instances <- c("pr264.tsp","gil262.tsp")
  ##instances<-scan(file=classes[Tclass],what=as.character(0),skip=0,quiet=TRUE)
  instclass <- paste("instances",class,sep="/")
  instances <- paste(instclass,dir(instclass),sep="/")
  
  n<-length(instances) ##number.of.instances
  
  ## slaves use the same data partition for the cross-validation
  if (exists(".Random.seed"))
    save.seed<-.Random.seed
  set.seed(0)
  
  ## Restore the random seed
  if(exists("save",inherits=FALSE))
    assign(".Random.seed",save,envir=globalenv())
  else
    rm(".Random.seed",envir=globalenv())

### Prepare a data.frame defining the candidates
  ##candidates <- expand.grid(p=programs,c=c("I","NN"),l=c("BB","FB","BFR","FFR","BFRDLB","FFRDLB"))
  ##candidates<- read.table(paste(wd,"calls.txt",sep=""),header=FALSE,sep=" ",strip.white=TRUE,comment.char="#",as.is=TRUE)
  ##candidates <- read.table("calls.txt",header=TRUE,sep="\t",strip.white=TRUE,comment.char="#")
  candidates<- as.data.frame(rbind(
                                   c(label="040885",exe="Assignment4/040885/gcp"),
                                   c(label="060590",exe="Assignment4/060590/gcp"),
                                   c(label="230190",exe="Assignment4/230190/gcp"),
                                   c(label="280881",exe="Assignment4/280881/gcp"),
                                   c(label="290786",exe="Assignment4/290786/gcp")
                                   ))
  ## Return init list
  return(list(name = class,
              wd=wd,
              candidates=candidates,
              no.candidates=dim(candidates)[1],
              
              instances=instances,
              no.instances=n,

              no.tasks=n, #or stages
              ##if unreplicated: no.tasks = num. of instances
              no.subtasks=1,
              ##if replicated no.subtasks = number of instances available
              smpl=sample(n)
              ##if unreplicated: smpl=sample(num. of instances)
              ))

}

######################################################################
race.info<-function(data)
  return(list(race.name=paste("GCP Heuristic -- ",
                data$name),
              no.candidates=(data$no.candidates),
              no.tasks=(data$no.tasks),
              no.instances=(data$no.instances),
              no.subtasks=data$no.subtasks
              ))


######################################################################
race.describe<-function(candidate,data){
  return(data$candidates[candidate,])
  ##  return(paste(as.character(tmp$V1),as.character(tmp$V2),sep=" "))
}


######################################################################
###Race Wrapper for UnReplicated Designs 
race.wrapper<-function(candidate,task,data){
  ##instance.loc <- paste("../instances/",data$instances[task],sep="")
  command <- paste("nice -19 ",
                   data$wd,
                   data$candidates[candidate,2],
                   " -i ",data$wd,data$instances[task],
                   " -s ",data$smpl[task]," ",
                   " -t 5 ",
                   "-o /dev/null 2>/dev/null",
                   sep="")
  print(command)
  s <- as.double(system(command,intern=TRUE,ignore.stderr=TRUE))
  print(s)
  out <- s[1]
  ##cat(out)    
  return(out)
}


