R/solveCARNIVALSingle.R

Defines functions solveCARNIVALSingle

## Solving CARNIVAL problem for one single experimental condition
##
## Enio Gjerga, 2020

solveCARNIVALSingle <- function(data = data, pknList = pknList,
                                inputs = inputs, alphaWeight = alphaWeight,
                                betaWeight = betaWeight, scores = scores,
                                mipGAP = mipGAP, poolrelGAP = poolrelGAP,
                                limitPop = limitPop, poolCap = poolCap,
                                poolIntensity = poolIntensity,
                                poolReplace = poolReplace,
                                timelimit = timelimit,
                                threads = threads,
                                measWeights = measWeights, repIndex = repIndex,
                                condition = condition, solver = solver, 
                                solverPath = solverPath, variables = variables,
                                measObj = measObj, inputObj = inputObj, 
                                dir_name = dir_name){
  
  variables <- writeLPFile(data = data, pknList = pknList,
                           inputs = inputs, alphaWeight = alphaWeight,
                           betaWeight = betaWeight, scores = scores,
                           mipGAP = mipGAP, poolrelGAP = poolrelGAP,
                           limitPop = limitPop, poolCap = poolCap,
                           poolIntensity = poolIntensity,
                           threads = threads,
                           poolReplace = poolReplace, timelimit = timelimit,
                           measWeights = measWeights, repIndex = repIndex,
                           condition = condition)
  
  ## Solve ILP problem with cplex, remove temp files, 
  ## and return to the main directory
  message("Solving LP problem...")
  
  if(solver=="cplex"){
    
    if (Sys.info()[1]=="Windows") {
      file.copy(from = solverPath,to = getwd())
      system(paste0("cplex.exe -f cplexCommand_", 
                    condition,"_",repIndex,".txt"))
      file.remove("cplex.exe")
    } else {
      system(paste0(solverPath, " -f cplexCommand_", 
                    condition,"_",repIndex,".txt"))
    }
    
    ## Write result files in the results folder
    message("Saving results...")
    resList <- list()
    if (file.exists(paste0("results_cplex_",condition,"_",repIndex,".txt"))) {
      for(i in seq_len(length(variables))){
        res <- exportResult(cplexSolutionFileName = paste0("results_cplex_",
                                                           condition,"_",
                                                           repIndex,".txt"),
                            variables = variables, 
                            pknList = pknList, 
                            conditionIDX = i,
                            inputs=inputObj,
                            measurements=measObj)
        resList[[length(resList)+1]] <- res
      }
      if (!is.null(res)) {
        if(!is.null(dir_name)){
          if(dir.exists(dir_name)){
            WriteDOTfig(res=res,
                        dir_name=dir_name,
                        inputs=inputObj,
                        measurements=measObj,
                        UP2GS=FALSE)
          } else {
            warning("Specified directory does not exist. DOT figure not saved.")
          }
        }
      } else {
        message("No result to be written")
        return(NULL)
      }
    }
    
    cleanupCARNIVAL(condition = condition, repIndex = repIndex)
    
    ## Remove global variable 
    objs <- ls(pos = ".GlobalEnv")
    rm(list = objs[grep("pknList", objs)], pos = ".GlobalEnv")
    
    message(" ")
    message("--- End of the CARNIVAL pipeline ---")
    message(" ")
    
    result = resList[[1]]
    
    return(result)
    
  } else {
    
    if(solver=="cbc"){
      
      resFile = paste0("results_cbc_", 1, "_", 1, ".txt")
      
      cbc_command <- paste0(solverPath, " testFile_", 1, "_", 
                            1, ".lp -seconds ", timelimit,
                            " -ratio ", poolrelGAP, 
                            " solve printi csv solu ", resFile)
      
      system(cbc_command)
      
      res <- exportResult(cplexSolutionFileName = resFile, 
                          variables = variables, 
                          conditionIDX = 1,
                          pknList = pknList, 
                          inputs=inputObj, 
                          measurements=measObj, 
                          solver = "cbc")
      
      if (!is.null(res)) {
        if(!is.null(dir_name)){
          if(dir.exists(dir_name)){
            WriteDOTfig(res=res,
                        dir_name=dir_name,
                        inputs=inputObj,
                        measurements=measObj,
                        UP2GS=FALSE)
          } else {
            warning("Specified directory does not exist. DOT figure not saved.")
          }
        }
      } else {
        message("No result to be written")
        return(NULL)
      }
      
      ## cleanupCARNIVAL(condition = condition, repIndex = repIndex)
      
      return(res)
      
    } else {
      
      lpForm <- prepareLPMatrixSingle(variables = variables, measObj = measObj)
      
      lpSolution <- lp(direction = "min", objective.in = lpForm$obj, 
                       const.mat = lpForm$con, const.dir = lpForm$dir, 
                       const.rhs = lpForm$rhs, int.vec = lpForm$ints, 
                       binary.vec = lpForm$bins)$solution
      
      res <- exportResult(cplexSolutionFileName = NULL, variables = variables,
                          pknList = pknList, inputs = inputObj,
                          measurements = measObj, solver = solver,
                          lpSolution = lpSolution, mt = lpForm$mt,
                          conditionIDX = 1)
      
      if (!is.null(res)) {
        if(!is.null(dir_name)){
          if(dir.exists(dir_name)){
                  WriteDOTfig(res=res,
                              dir_name=dir_name,
                              inputs=inputObj,
                              measurements=measObj,
                              UP2GS=FALSE)
          } else {
            warning("Specified directory does not exist. DOT figure not saved.")
          }
        }
      } else {
        message("No result to be written")
        return(NULL)
      }
      
      return(res)
      
    }
    
  }
  
}

Try the CARNIVAL package in your browser

Any scripts or data that you put into this service are public.

CARNIVAL documentation built on Nov. 8, 2020, 5:24 p.m.