
# 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.
## libs
library(data.table)
library(Metrics)
library(FNN)
library(parallel)
library(dplyr)

## read data
require(bit64)
#train_Full <- fread("../input/train.csv")
#test_Full  <- fread("../input/test.csv")
result_final <- NULL
Date <- as.POSIXct('2014-01-01T01:01', tz = "")

weights = c(500, 1000, 4, 3, 2, 10, 10 , .45)


train_Full <- fread("../input/train.csv")
test_Full  <- fread("../input/test.csv") 

train_Full$DateTime <- Date+train_Full$time
test_Full$DateTime <- Date+test_Full$time




KNN <- function(train , test)
{
k=10
#print(head(train[as.numeric(t(knn_indexes)),]$place_id))
knn_model <- get.knnx( train[  ,.(x= x*weights[1]
                            ,y= y*weights[2]
                        #    ,hour =as.numeric(format(DateTime, "%H"))*weights[3]
                        #    ,year =(as.numeric(format(DateTime, "%Y"))-2013)*weights[6]
                        #    ,Month =as.numeric(format(DateTime, "%m"))**weights[5]
                        #    ,day =as.numeric(format(DateTime, "%d"))*weights[8]
                        #    ,weekday =as.numeric(factor(weekdays(DateTime)))*weights[4]
                        #    ,accuracy =as.numeric(log10(accuracy))*weights[7]
                            )],
test[  ,.(x= x*weights[1]
                            ,y= y*weights[2]
                        #    ,hour =as.numeric(format(DateTime, "%H"))*weights[3]
                        #    ,year =(as.numeric(format(DateTime, "%Y"))-2013)*weights[6]
                        #    ,Month =as.numeric(format(DateTime, "%m"))**weights[5]
                        #    ,day =as.numeric(format(DateTime, "%d"))*weights[8]
                        #    ,weekday =as.numeric(factor(weekdays(DateTime)))*weights[4]
                        #    ,accuracy =as.numeric(log10(accuracy))*weights[7]
                        )], k=10)
print(str(knn_model))

pred_list_num <- as.character(train[as.numeric(knn_model$nn.index),]$place_id)
dim(pred_list_num) <- c(nrow(test), 10)
final_pred_list <-
t(data.frame(place_id= apply(pred_list_num ,1,function(x){
    x1<-summarize(group_by(data.frame(x) , x) ,value=n())
    x2<-arrange(x1 ,x, desc(value))[1:3,1]
    summarize(group_by(x2) ,place_id=paste(x,collapse=" "))
  })))
  
#  print(head(final_pred_list))
data.frame(row_id=test$row_id, place_id=final_pred_list)
}


#for(x_out in seq( 10000 ,50000  , 10000))
#{
#  for(y_out in seq( 10000 ,100000  , 10000))
#  {
#train<-train_Full[train_Full$x <= (x_out+.1)*.1 & train_Full$x > (x_out-10000)*.1 & train_Full$y <= (y_out+.5)*.1 & train_Full$y > (y_out-10000)*.1,]
#test<-test_Full[test_Full$x <= (x_out)*.1 & test_Full$x > (x_out-10000)*.1 & test_Full$y <= (y_out)*.1 & test_Full$y > (y_out-10000)*.1,]
       

for(x_out in seq( 1 ,10  , 1))
{
print(x_out)
#write_csv(data.frame(result) , paste0("KNN.csv"))
result_final<-rbind(result_final , KNN(train_Full[train_Full$x <= (x_out) & train_Full$x > (x_out-1),], test_Full[test_Full$x <= (x_out) & test_Full$x > (x_out-1),]) )
#print(result_final)
#}
}
  
  


write_csv(data.frame(result_final) , paste0("KNN.csv"))