# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

# Any results you write to the current directory are saved as output.

rmsls <- function(predicted, actual) {
        sqrt(mean((log(predicted+1)-log(actual+1))^2))
}

# Reading dataset with faster readr library

train <- read_csv('../input/train.csv', col_names=TRUE)

test <- read_csv('../input/test.csv', col_names=TRUE)


gc()
prod.name <- 'Producto_ID'

flag_test = T

if (flag_test) {
  products <- unique(test[, prod.name])
  test.n <- 1:nrow(test)
  target.product <- test[, prod.name]
  t <- train
} else {  
  products <- unique(train[, prod.name])
  test.n <- which(train[, 'Semana'] == 9)
  target.product <- train[test.n, prod.name]
  target.test.actual <- train[test.n, 'Demanda_uni_equil']
  t <- train[train[, 'Semana'] < 9, ]
}
target.test <- rep(0, length(test.n))
target.done <- rep(0, length(test.n))
library(doMC)
# Change to how many cores you have but for 8 cores needs 40gb memory, well there are better scripts...
registerDoMC(cores=1)


chunks.begin <- seq(1,length(products), 100)
chunks.end <- c(chunks.begin[2:length(chunks.begin)]-1, length(products))
for (j in 1:length(chunks.begin)) {
  x <- unlist(foreach(i = chunks.begin[j]:chunks.end[j]) %dopar% median(t[t[, prod.name] == products[i], 'Demanda_uni_equil']))
  x[is.na(x)] <- 6
  for (k in 1:length(x)) {
    target.test[target.product == products[chunks.begin[j]+k-1]] <- x[k]
    target.done[target.product == products[chunks.begin[j]+k-1]] <- 1
  }
  gc()
  if (flag_test) {
    writeLines(toString(chunks.end[j]))
  } else {
    writeLines(paste(toString(chunks.end[j]), toString(rmsell(target.test[target.done==1], target.test.actual[target.done==1]))))
  }
}

submission <- read.csv("../input/sample_submission.csv",header = TRUE, sep = ",")
submission[, 2] <- target.test
write.csv(submission, "submission_1.csv", row.names=F, quote=F)

