Guest User

Untitled

a guest
Nov 30th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 20.62 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #' internal function used by ds_user_parallel
  5. #' assign capacities to workload for one region and determines transferable workload
  6. ds_user <- function(
  7.   workload, capacity, min_workertype, min_worktype, region, month_map, NbMonth, categ_map, worktype_map, NbWT, Nbtot, NbCat, dimsToIndex,
  8.   no_work, max_offshore
  9. ) {      
  10.   if(length(region)!=1) stop("region must be character of length 1.")
  11.  
  12.   ### set up lp
  13.   # the first Nbtot are the decision variables we are interested in (effort per month/work_type/categ/workplace/work_source). These do not
  14.   # go into the objective (zero coefficients). They are, however, the main result.
  15.   # the next NbCat variables are the "unmet workload" that is the work for which there is no capacity. Ideally, these would all be zero.
  16.   # the next NbCat variables are "max effort over the months for a given category". This is used to evenly distribute the effort.
  17.   # the next NbWT variables are "min capacity required" or "max effort over the months for a given worker type.
  18.   lprec <- make.lp(0, ncol=Nbtot+NbCat+NbCat+NbWT+NbCat)
  19.   set.objfn(lprec, obj=c(rep(0, Nbtot), rep(1000, NbCat), rep(1, NbCat), c(100, 80, 60), rep(500, NbCat)))
  20.  
  21.   ### constraints 1: no work in certain months for some types of work
  22.   for (categ in names(no_work))
  23.     for (m in intersect(names(month_map), no_work[[categ]])) {
  24.       idx <- dimsToIndex(month=m, categ=categ)
  25.       add.constraint(lprec, xt=rep(1, length(idx)), type="=", rhs=0, indices=idx)
  26.     }
  27.  
  28.   ### constraints 2: workload must be met
  29.   for (categ in names(categ_map)) {
  30.     add.constraint(
  31.       lprec, xt=rep(1, NbMonth*NbWT+2), "=", rhs=workload[region, categ],
  32.       indices=c(
  33.         dimsToIndex(categ=categ),
  34.         Nbtot+categ_map[categ], # unmet untransferable effort
  35.         Nbtot+NbCat+NbCat+NbWT+categ_map[categ] # unmet transferable effort
  36.       )
  37.     )
  38.   }
  39.  
  40.   ### constraints 3: effort <= capacity
  41.   for (m in names(month_map)) {
  42.     idx <- which(capacity[,"mon"]==m)
  43.     add.constraint(
  44.       lprec, xt=rep(1, NbCat), type="<=", rhs=capacity[idx, "dcutil"],
  45.       indices=dimsToIndex(month=m, worker_type="data_collector")
  46.     )
  47.     add.constraint(
  48.       lprec, xt=rep(1, NbCat), type="<=", rhs=capacity[idx, "regutil"],
  49.       indices=dimsToIndex(month=m, worker_type="regular")
  50.     )
  51.     add.constraint(
  52.       lprec, xt=rep(1, NbCat), type="<=", rhs=capacity[idx, "nonregutil"],
  53.       indices=dimsToIndex(month=m, worker_type="nonregular")
  54.     )
  55.   }
  56.  
  57.   ### constraints 4: minimum worker type utilization
  58.   ## for each month, region and category, we have two constraints:
  59.   ## 1) effort_regular >= perc * (effort_dc + effort_nonreg + effort_regular)
  60.   ## 2) effort_dc <= perc * (effort_dc + effort_nonreg + effort_regular)
  61.   ## where perc are input constants given in the min_workertype data.frame
  62.   ## By simple algebra, this normalizes to
  63.   ## 1') -perc * effort_dc - perc * effort_nonreg + (1-perc) * effort_regular >= 0
  64.   ## 2') (1-perc) * effort_dc - perc * effort_nonreg - perc * effort_regular <= 0
  65.   ## the second constraint only applies if there is actually data_collector capacity in that region
  66.   for (m in names(month_map))
  67.     for (categ in names(categ_map)) {
  68.       cap_idx <- which(capacity[,"mon"]==m)
  69.       if(capacity[cap_idx, "regutil"]>0){
  70.         idx1 <- dimsToIndex(month=m, categ=categ, worker_type=c("data_collector", "nonregular"))
  71.         idx2 <- dimsToIndex(month=m, categ=categ, worker_type="regular")
  72.         perc <- min_workertype[categ, "reg_min"]
  73.         add.constraint(lprec, xt=c(rep(-perc, 2), (1-perc)), type=">=", rhs=0, indices=c(idx1, idx2))
  74.       }
  75.       if(capacity[cap_idx, "dcutil"]>0) {
  76.         idx1 <- dimsToIndex(month=m, categ=categ, worker_type=c("regular", "nonregular"))
  77.         idx2 <- dimsToIndex(month=m, categ=categ, worker_type="data_collector")
  78.         perc <- min_workertype[categ, "dc_min"]
  79.         add.constraint(lprec, xt=c(rep(-perc, 2), (1-perc)), type="<=", rhs=0, indices=c(idx1, idx2))
  80.       }
  81.     }
  82.  
  83.   ### constraints 5: maximum work that can be done offshore
  84.   ## only a certain percentage can be done offshore
  85.   for (m in names(month_map))
  86.     for (categ in names(categ_map))
  87.       add.constraint(lprec, xt=1/max_offshore[categ], "<=", rhs=workload[region, categ], indices=Nbtot+NbCat+NbCat+NbWT+categ_map[categ])
  88.  
  89.   ### constraints 6: minimum work for some worktypes
  90.   for (categ in names(min_worktype)) {
  91.     rhs <- workload[region, categ] * min_worktype[categ]
  92.     for (m in names(month_map)) add.constraint(lprec, xt=rep(1, NbWT), ">=", rhs=rhs, indices=dimsToIndex(month=m, categ=categ))
  93.   }
  94.  
  95.   ### helper variable: maximum effort over all months for each category
  96.   for (m in names(month_map))
  97.     for (categ in names(categ_map))
  98.       add.constraint(
  99.         lprec, xt=c(rep(1, NbWT),-1), "<=", rhs=0,
  100.         indices=c(dimsToIndex(month=m, categ=categ), Nbtot+NbCat+categ_map[categ])
  101.       )
  102.  
  103.   ### helper variable: maximum effort over all months for each worker type
  104.   for (m in names(month_map))
  105.     for (wt in names(worktype_map))
  106.       add.constraint(
  107.         lprec, xt=c(rep(1, NbCat), -1), "<=", rhs=0,
  108.         indices=c(dimsToIndex(month=m, worker_type=wt), Nbtot+NbCat+NbCat+worktype_map[wt])
  109.       )
  110.  
  111.   # browser()
  112.   ### solve
  113.   status <- solve(lprec)
  114.  
  115.   ### no solution? halt
  116.   if(status!=0) stop(paste("no solution found, error code ", status, "; region ", region))
  117.  
  118.   # unmet, non-transferable.
  119.   unmet <- matrix(get.variables(lprec)[seq(Nbtot+1, length.out=NbCat)], nrow=1, dimnames=list(region, names(categ_map)))
  120.  
  121.   # resulting effort
  122.   effort <- get.variables(lprec)[1:Nbtot]
  123.   ans <- expand.grid(
  124.     Month=names(month_map), Category=names(categ_map),
  125.     Region=region, Worker_Type=names(worktype_map),
  126.     stringsAsFactors=FALSE
  127.   )
  128.   lookup_one <- function(x) effort[dimsToIndex(month=x[["Month"]], categ=x[["Category"]], worker_type=x[["Worker_Type"]])]
  129.   ans[,"Effort"] <- apply(ans, 1, lookup_one)
  130.  
  131.   # transferable
  132.   transferable <- get.variables(lprec)[seq(Nbtot+NbCat+NbCat+NbWT+1, length.out=NbCat)]
  133.   idx <- which(transferable>.Machine$double.eps^0.5)
  134.   if(length(idx)>0) {
  135.     transferable_df <- data.frame(
  136.       region=region,
  137.       categ=names(categ_map)[idx],
  138.       transferable=transferable[idx],
  139.       stringsAsFactors=FALSE
  140.     )
  141.   } else transferable_df <- data.frame()
  142.  
  143.   # if( nrow(transferable_df)>0) browser()
  144.   return(list(effort=ans, unmet=unmet, transferable=transferable_df))
  145. }
  146.  
  147. #' internal function used by ds_user_parallel
  148. #' distributes transferable workload to regions with free capacities
  149. ds_user_transfer <- function(effort, capacity, transferable, min_workertype, month_map, NbMonth, categ_map, worktype_map, NbWT, NbCat, no_work) {
  150.   # calculate the capacity remaining after onshore work
  151.   eff <- aggregate(Effort ~ Month + Region + Worker_Type, data=effort, sum)
  152.   eff <- reshape(eff, timevar="Worker_Type", idvar=c("Month","Region"), direction="wide")
  153.   eff <- eff[, c("Month", "Region", "Effort.data_collector", "Effort.nonregular", "Effort.regular")]
  154.   colnames(eff) <- c("mon", "region", "dceff", "nonregeff", "regeff")
  155.   remaining_capa <- merge(eff, capacity)
  156.   remaining_capa[, "regremain"] <- pmax(0, remaining_capa[, "regutil"] - remaining_capa[, "regeff"])
  157.   remaining_capa[, "nonregremain"] <- pmax(0, remaining_capa[, "nonregutil"] - remaining_capa[, "nonregeff"])
  158.   remaining_capa[, "dcremain"] <- pmax(0, remaining_capa[, "dcutil"] - remaining_capa[, "dceff"])
  159.   remaining_capa <- remaining_capa[,c("mon", "region", "regremain", "nonregremain", "dcremain")]
  160.  
  161.   # from where to where can work be shifted?
  162.   from_regions <- unique(transferable[,"region"])
  163.   NbShifts <- nrow(transferable) # region / categ combinations
  164.   idx <- !(remaining_capa[,"region"] %in% from_regions) &
  165.     (remaining_capa[,"regremain"]+remaining_capa[,"nonregremain"]+remaining_capa[,"dcremain"])>0
  166.   to_regions <- unique(remaining_capa[idx, "region"])
  167.   NbRegTo <- length(to_regions)
  168.  
  169.   # is there at least one region where we can transfer work to?
  170.   if(length(to_regions)==0) {
  171.     unmet_df <- reshape(transferable, direction="wide", timevar="categ", idvar="region")
  172.     rownames(unmet_df) <- unmet_df[,"region"]
  173.     unmet_df[,"region"] <- NULL
  174.     unmet_df <- as.matrix(unmet_df)
  175.     unmet_df[is.na(unmet_df)] <- 0
  176.     colnames(unmet_df) <- substring(colnames(unmet_df), 14)
  177.     return(list(transfers=data.frame(), still_unmet=unmet_df))
  178.   }
  179.  
  180.   # set up lp
  181.   # first NbShifts*NbRegTo*NbWT*NbMonth entries: effort for the transfers
  182.   # next NbRegTo*NbWT entries: capacity needed after the shifts (this is to minimize)
  183.   # next NbShifts entries: unmet untransferable work (hopefully zero, this is penalized)
  184.   lprec <- make.lp(0, ncol=NbShifts*NbRegTo*NbWT*NbMonth+NbRegTo*NbWT+NbShifts)
  185.   set.objfn(lprec, obj=c(rep(0, NbShifts*NbRegTo*NbWT*NbMonth), rep(c(100, 80, 60), NbRegTo), rep(1000, NbShifts)))
  186.   cat("#Vars: ", NbShifts*NbRegTo*NbWT*NbMonth+NbRegTo*NbWT+NbShifts, "\n")
  187.  
  188.   ### constraints 1: no work in certain months for some types of work
  189.   for (categ in names(no_work))
  190.     for (m in intersect(names(month_map), no_work[[categ]])) {
  191.       idx_shifts <- which(transferable[,"categ"]==categ)
  192.       for (i in idx_shifts)
  193.         for (reg in seq.int(NbRegTo))
  194.           for (wt in names(worktype_map)) {
  195.             add.constraint(
  196.               lprec,
  197.               xt=1, type="=", rhs=0,
  198.               indices=(i-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbMonth + (worktype_map[wt]-1)*NbMonth + month_map[m]
  199.             )
  200.           }
  201.     }
  202.  
  203.   ### constraints 2: workload must be met
  204.   for (i in seq.int(nrow(transferable))) {
  205.     add.constraint(
  206.       lprec, xt=rep(1, NbRegTo*NbWT*NbMonth+1), "=", rhs=transferable[i, "transferable"] ,
  207.       indices=c(seq((i-1)*NbRegTo*NbWT*NbMonth+1, i*NbRegTo*NbWT*NbMonth), NbShifts*NbRegTo*NbWT*NbMonth+NbRegTo*NbWT+i)
  208.     )
  209.   }
  210.  
  211.   ### constraints 3: transfered effort <= remaining capacity
  212.   for (m in names(month_map))
  213.     for (reg in seq.int(NbRegTo)) {
  214.       idx <- which(remaining_capa[,"mon"]==m & remaining_capa[,"region"]==to_regions[reg])
  215.       add.constraint(
  216.         lprec, xt=rep(1, NbShifts), type="<=", rhs=capacity[idx, "dcutil"],
  217.         indices=(seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map["data_collector"]-1)*NbMonth+month_map[m]
  218.       )
  219.       add.constraint(
  220.         lprec, xt=rep(1, NbShifts), type="<=", rhs=capacity[idx, "regutil"],
  221.         indices=(seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map["regular"]-1)*NbMonth+month_map[m]
  222.       )
  223.       add.constraint(
  224.         lprec, xt=rep(1, NbShifts), type="<=", rhs=capacity[idx, "nonregutil"],
  225.         indices=(seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map["nonregular"]-1)*NbMonth+month_map[m]
  226.       )
  227.     }
  228.  
  229.   ### constraints 4: minimum worker type utilization
  230.   ## for each month, region and category, we have two constraints:
  231.   ## 1) effort_regular_from_offshore + effort_regular_onsite >= perc * (effort_total_from_offshore + effort_total_onsite)
  232.   ## 2) effort_dc_from_offshore + effort_dc_onsite <= perc * (effort_total_offshore + effort_total_onsite)
  233.   ## where perc are input constants depending on the category given in the min_workertype data.frame
  234.   ## By simple algebra, this normalizes to
  235.   ## 1') (1-perc) effort_regular_offshore >= perc effort_total_onsite - effort_regular_onsite
  236.   ## 2') (1-perc) effort_dc_offshore <= perc effort_total_onsite - effort_dc_onsite
  237.   ## the second constraint only applies if there is actually data_collector capacity in that region
  238.   for (reg in seq.int(NbRegTo))
  239.     for (m in names(month_map))
  240.       for (categ in names(categ_map)) {
  241.         # lookup onsite values
  242.         idx <- which(effort[,"Month"]==m & effort[, "Category"]==categ & effort[, "Region"]==to_regions[reg])
  243.         onsite <- setNames(effort[idx, "Effort"], effort[idx, "Worker_Type"])
  244.         perc <- min_workertype[categ, "reg_min"]
  245.         add.constraint(
  246.           lprec, xt=rep((1-perc), NbShifts), type=">=",
  247.           rhs=perc*sum(onsite)-onsite["regular"],
  248.           indices=(seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map["regular"]-1)*NbMonth+month_map[m]
  249.         )
  250.         perc <- min_workertype[categ, "dc_min"]
  251.         add.constraint(
  252.           lprec, xt=rep((1-perc), NbShifts), type="<=",
  253.           rhs=perc*sum(onsite)-onsite["data_collector"],
  254.           indices=(seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map["data_collector"]-1)*NbMonth+month_map[m]
  255.         )
  256.       }
  257.  
  258.   ### helper variable: maximum effort over all months for each worker type
  259.   for (reg in seq.int(NbRegTo))
  260.     for (m in names(month_map))
  261.       for (wt in names(worktype_map)) {
  262.         # lookup onsite values
  263.         idx <- which(effort[,"Month"]==m & effort[, "Region"]==to_regions[reg] & effort[, "Worker_Type"]==wt)
  264.         onsite <- sum(effort[idx, "Effort"]) # sum over categories
  265.         add.constraint(
  266.           lprec, xt=c(rep(1, NbShifts), -1), "<=", rhs=-onsite,
  267.           indices=c((seq.int(NbShifts)-1)*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT*NbMonth+(worktype_map[wt]-1)*NbMonth+month_map[m],
  268.                     NbShifts*NbRegTo*NbWT*NbMonth+(reg-1)*NbWT+worktype_map[wt])
  269.         )
  270.       }
  271.  
  272.   ### solve
  273.   status <- solve(lprec)
  274.  
  275.   ### no solution? halt
  276.   if(status!=0) stop(paste("no solution found, error code ", status))
  277.  
  278.   ### gather results
  279.   one_transfer <- function(i) {
  280.     vars <- get.variables(lprec)[seq((i-1)*NbRegTo*NbWT*NbMonth+1, i*NbRegTo*NbWT*NbMonth)]
  281.     idx1 <- which(vars > .Machine$double.eps^0.5)
  282.     if(length(idx1)>0) {
  283.       reg <- idx1 %/% (NbWT*NbMonth) + 1
  284.       idx <- idx1 - (reg - 1) * (NbWT*NbMonth)
  285.       wt <- idx %/% NbMonth + 1
  286.       m <- idx - (wt - 1) * NbMonth + 1
  287.       # browser()
  288.      
  289.       ret <- data.frame(
  290.         region_from=transferable[i, "region"],
  291.         categ=transferable[i,"categ"],
  292.         region_to=to_regions[reg],
  293.         worker_type=names(worktype_map)[wt],
  294.         mon=names(month_map)[m],
  295.         effort=vars[idx1],
  296.         stringsAsFactors=FALSE
  297.       )
  298.     } else ret <- data.frame()
  299.     return(ret)
  300.   }
  301.  
  302.   transf <- lapply(seq.int(nrow(transferable)), FUN=one_transfer)
  303.   transf <- do.call(rbind, transf)
  304.  
  305.   ### still unmet workload?
  306.   unmet <- get.variables(lprec)[seq(NbShifts*NbRegTo*NbWT*NbMonth+NbRegTo*NbWT, length.out=NbShifts)]
  307.   if(any(unmet > .Machine$double.eps^0.5)) {
  308.     unmet_df <- transferable
  309.     unmet_df[, "transferable"] <- NULL
  310.     unmet_df[, "unmet"] <- unmet
  311.     unmet_df <- reshape(transferable, direction="wide", timevar="categ", idvar="region")
  312.     rownames(unmet_df) <- unmet_df[,"region"]
  313.     unmet_df[,"region"] <- NULL
  314.     unmet_df <- as.matrix(unmet_df)
  315.     unmet_df[is.na(unmet_df)] <- 0
  316.     colnames(unmet_df) <- substring(colnames(unmet_df), 14)
  317.   } else unmet_df <- data.frame()
  318.  
  319.   return(list(transfers=transf, still_unmet=unmet_df))
  320. }
  321.  
  322. #' Solves a resource allocation problem
  323. #'
  324. #' @param workload workload data.frame (rows=regions, columns=worktype)
  325. #' @param capacity work capacity per month data.frame (columns: region, month, regutil, nonregutil, dcutil)
  326. #' @param min_workertype minimum requirements data.frame (columns=region, month, worktype, regmin, dcmin)
  327. #' @param min_worktype optional minimum requirement for work types.
  328. #' @param start_month start month: from here to Dec the workload needs to be processed (three letter month abbreviation). Default "Jan"
  329. #' @param no_work named list of worktype with the months (three letter month abbreviation) where no work on that worktype is possible
  330. #' @param max_offshore named vector with the percentages of work that can be done offshore
  331. #'
  332. #' @return a list with three elements named "effort" with the distributed workload
  333. #'        (data.frame, columns: region, month, worktype, worker type, effort),  
  334. #'        "unmet", a (hopefully all zero) matrix (columns: category, rows: regions) where capacity problems arise, and
  335. #'        (only if transfers==TRUE) "transfers" a data.frame with distributed offshore work
  336. #'        (data.frame, columns: region_from, categ, region_to, worker_type, mon, effort)
  337. ds_user_parallel <- function(
  338.   workload,
  339.   capacity,
  340.   min_workertype,
  341.   min_worktype=c(Subdivisions=1/12, HomeValue=0.05),
  342.   start_month="Jan",
  343.   no_work=list(
  344.     "Reval_Days"=c("Jan", "Feb", "Mar", "Apr", "May", "Jun"),
  345.     "Asset"=c("Sep", "Oct", "Nov", "Dec", "Jan"),
  346.     "Reval_Objections"=c("Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
  347.   ),
  348.   max_offshore=c(
  349.     Building_Consents=0.5, Subdivisions=0.9, S12_Sales=0.5, RM_Objections=0.1, Reval_Days=0.3, Reval_Objections=0.1,
  350.     Asset=0.1, HomeValue=0.2, Rural=0.2, Urgent_New_Imp=0.2
  351.   ),
  352.   transfers=TRUE
  353. ){
  354.   ### helper functions
  355.   month_map <- setNames(
  356.     rev(seq.int(12)),
  357.     c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
  358.   )
  359.   if(!start_month %in% names(month_map)) stop("invalid start_month, must be a three letter month abbreviation.")
  360.   NbMonth <- month_map[start_month]
  361.   month_map <- tail(month_map, NbMonth)
  362.   categ_map <- unique(rownames(min_workertype))
  363.   NbCat <- length(categ_map)
  364.   categ_map <- setNames(seq.int(NbCat), categ_map)
  365.  
  366.   ### sanity check 1: consistent use of work category names
  367.   if(length(intersect(colnames(workload), names(categ_map)))<max(c(length(colnames(workload)), length(categ_map))))
  368.     stop("work categories in workload and min_req argument do not match.")
  369.   if(length(setdiff(names(no_work), names(categ_map))) > 0)
  370.     stop("work categories in no_work and min_req argument do not match.")
  371.  
  372.   worktype_map <- c(regular=1, nonregular=2, data_collector=3)
  373.   NbWT <- length(worktype_map)
  374.   Nbtot <- (NbMonth * NbCat * NbWT)
  375.  
  376.   ### sanity check 2: need more capacity than workload
  377.   capacity <- capacity[capacity[,"mon"] %in% names(month_map),]
  378.   #if(sum(workload)>sum(capacity[, c("regutil", "nonregutil", "dcutil")])) stop("workload exceeds capacity")
  379.  
  380.   ### map subscripts of the decision vars to index and vice versa
  381.   dimsToIndex <- function(month=names(month_map), categ=names(categ_map), worker_type=names(worktype_map)) {
  382.     args <- expand.grid(
  383.       month=month_map[month]-1,
  384.       categ=categ_map[categ]-1,
  385.       worker_type=worktype_map[worker_type],
  386.       stringsAsFactors=FALSE
  387.     )
  388.     w <- c(NbCat*NbWT, NbWT, 1)
  389.     ret <- apply(args, 1, function(x) {ans <- x %*% w; dim(ans) <- NULL; return(ans)})
  390.     return(ret)
  391.   }
  392.   dimsToIndex <- cmpfun(dimsToIndex)
  393.  
  394.   subset_wl <- function(reg) matrix(workload[reg,], nrow=1, dimnames=list(reg, colnames(workload)))
  395.   splitted_cap <- split(capacity, capacity[,"region"])
  396.   my_combine <- function(x, y)
  397.     list(
  398.       effort=rbind(x[["effort"]], y[["effort"]]),
  399.       unmet=rbind(x[["unmet"]], y[["unmet"]]),
  400.       transferable=rbind(x[["transferable"]], y[["transferable"]])
  401.     )
  402.   arg <- list(
  403.     month_map=month_map, NbMonth=NbMonth, categ_map=categ_map, worktype_map=worktype_map, min_workertype=min_workertype, min_worktype=min_worktype,
  404.     NbWT=NbWT, Nbtot=Nbtot, NbCat=NbCat, dimsToIndex=dimsToIndex, no_work=no_work, max_offshore=max_offshore
  405.   )
  406.   ret <- foreach(reg=names(splitted_cap), .combine=my_combine) %do% {
  407.     arg[["workload"]] <- subset_wl(reg)
  408.     arg[["capacity"]] <- splitted_cap[[reg]]
  409.     arg[["region"]] <- reg
  410.     do.call(ds_user, arg)
  411.   }
  412.  
  413.   if(nrow(ret[["transferable"]])>0)
  414.     if(transfers) {
  415.       transf_ret <- ds_user_transfer(
  416.         effort=ret[["effort"]], capacity=capacity, transferable=ret[["transferable"]], min_workertype=min_workertype,
  417.         month_map=month_map, NbMonth=NbMonth, categ_map=categ_map, worktype_map, NbWT=NbWT, NbCat, no_work=no_work
  418.       )
  419.       still_unmet <- transf_ret[["still_unmet"]]
  420.       if(nrow(still_unmet)>0)
  421.         ret[["unmet"]][rownames(still_unmet), colnames(still_unmet)] <-
  422.         ret[["unmet"]][rownames(still_unmet), colnames(still_unmet)] + still_unmet
  423.       ret[["transferable"]] <- NULL
  424.       ret[["transfers"]] <- transf_ret[["transfers"]]
  425.     } else {
  426.       # no transfers wished -- add to unmet
  427.       unmet_df <- reshape(ret[["transferable"]], direction="wide", timevar="categ", idvar="region")
  428.       rownames(unmet_df) <- unmet_df[,"region"]
  429.       unmet_df[,"region"] <- NULL
  430.       unmet_df <- as.matrix(unmet_df)
  431.       unmet_df[is.na(unmet_df)] <- 0
  432.       colnames(unmet_df) <- substring(colnames(unmet_df), 14)
  433.       ret[["unmet"]][rownames(unmet_df), colnames(unmet_df)] <- ret[["unmet"]][rownames(unmet_df), colnames(unmet_df)] + unmet_df
  434.       ret[["transferable"]] <- NULL
  435.     }
  436.  
  437.   if(any(ret[["unmet"]]>.Machine$double.eps^0.5)) warning("there is unmet ", if(transfers) "untransferable", "workload")
  438.  
  439.   # browser()
  440.   return(ret)
  441. }
Advertisement
Add Comment
Please, Sign In to add comment