# TODO: Add comment
# 
# Author: diego
###############################################################################


rm(list=ls())
library("snow")
t1<-Sys.time()



rates<-c(0.5,0.5,0.7)
initialState<-c(0,0,0,0,0)
AllEvents<-c(1,1,1,1,2,2,2,3,1,2)
AllLocations<-c(1,2,3,4,1,4,2,1,5,4)
AllTimes<-1:10
AllConfigurations<-rbind(
		c(0,0,0,0,0),
		c(1,0,0,0,0),
		c(1,1,0,0,0),
		c(1,1,1,0,0),
		c(1,1,1,1,0),
		c(2,1,1,1,0),
		c(2,1,1,2,0),
		c(2,2,1,2,0),
		c(0,2,1,2,0),
		c(0,2,1,2,1),
		c(0,2,1,0,1)
		)

nevents<-length(AllEvents)

clusterSize<-2
cl<-makeCluster(clusterSize,type="SOCK")

arguments<-vector("list",nevents)

completedTimes<-c(0,AllTimes)
for(i in 1:nevents){
	element<-c(AllConfigurations[i,],
				rates,
				completedTimes[i],
				AllEvents[i],
				AllLocations[i],
				AllTimes[i])
	arguments[[i]]<-element
}

source("GetLogLikelihood.R")
parallelOutputs<-clusterApplyLB(cl,arguments,GetLogLikelihood)  # This give me error
#parallelOutputs<-clusterApply(cl,arguments,GetLogLikelihood) # This Work OK

print(sum(unlist(parallelOutputs)))

stopCluster(cl)
t2<-Sys.time()
print(t2-t1)
