
# 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*0.4, 1000*0.4, (1000/24)*0.055, (1000/7)*0.05, (1000/12)*0.04, (1000/2013)*0.005, 1*0.025 , (1000/30)*0.025)


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

test_Full  <-test_Full[ ,.( 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((accuracy))*weights[7]
,row_id)]

train_Full  <-train_Full[ ,.( 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((accuracy))*weights[7]
                            ,row_id
                            ,place_id)]




for(x_out in seq( 2000 ,2000  , 500)*0.4)
{
  for(y_out in seq( 500 ,3000  , 500)*0.4)
  {
train<-train_Full[train_Full$x <= (x_out+.1) & train_Full$x > (x_out-500*0.4) & train_Full$y <= (y_out+.2) & train_Full$y > (y_out-500*0.4),]
test<-test_Full[test_Full$x <= (x_out) & test_Full$x > (x_out-500*0.4) & test_Full$y <= (y_out) & test_Full$y > (y_out-500*0.4),]
       


k=10
#print(head(train[as.numeric(t(knn_indexes)),]$place_id))
knn_model <- get.knnx( train[  ,.(x , y , accuracy , hour , year,Month , day,weekday)],
test[  ,.(x , y , accuracy , hour , year,Month , day,weekday)], k=10)
#print(str(knn_model))

pred_list_num <- train[as.numeric(knn_model$nn.index),]$place_id
dim(pred_list_num) <- c(nrow(test), 10)
final_pred_list <-
data.frame(place_id=matrix( unlist( 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=" "))
  }), recursive = FALSE) ))
  
#  print(head(final_pred_list))
result <- data.frame(row_id=test$row_id, place_id=unlist(final_pred_list))
print(x_out)
#write_csv(data.frame(result) , paste0("KNN.csv"))
result_final<-rbind(result_final , result )
#print(result_final)
}
}
  
  

print("result_final")
write_csv(data.frame(result_final) , paste0("KNN2000_0.csv"))