# install_git("https://www.gitlab.ci.inf.usi.ch/SQUIC/SQUIC_R.git")
library(SQUIC)

# install.packages("devtools",repos = "http://cran.us.r-project.org")
library(devtools)

# install.packages("Matrix",repos = "http://cran.us.r-project.org")
library(Matrix)

# install.packages("MLmetrics",repos = "http://cran.us.r-project.org")
library(MLmetrics)

# install.packages("glasso",repos = "http://cran.us.r-project.org")
library(glasso)

# install.packages("BigQuic",repos = "http://cran.us.r-project.org")
library(BigQuic)

# devtools::install_github("cescwang85/EQUAL")
library(EQUAL)

source("load_data.R")
source("compare.R")

# =========================== #
# Run the timings experiments #
# =========================== #
library(parallel)
Sys.setenv(OMP_NUM_THREADS =detectCores()/2)

time_trid_squic   <- replicate(8,-1)
time_trid_glasso  <- replicate(8,-1)
time_trid_equal   <- replicate(8,-1)
time_trid_bigquic <- replicate(8,-1)

time_clus_squic   <- replicate(8,-1)
time_clus_glasso  <- replicate(8,-1)
time_clus_equal   <- replicate(8,-1)
time_clus_bigquic <- replicate(8,-1)

p_set             <- replicate(8,-1)

tol      <- 1e-3
max_iter <- 100

#####################################
i <- 1
p <- 500
#####################################
p_set[i] = p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)


#####################################
i <- 2
p <- 1000
#####################################
p_set[i] <- p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)

#####################################
i <- 3
p <- 2000
#####################################
p_set[i] <- p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)

#####################################
i <- 4
p <- 5000
#####################################
p_set[i] <- p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)


#####################################
i <- 5
p <- 1e4
#####################################
p_set[i] = p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)

#####################################
i <- 6
p <- 5*1e4
#####################################
p_set[i] = p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)

#####################################
i <- 7
p <- 1e5
#####################################
p_set[i] <- p
#####################################
lambda <- 0.35
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.2
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)


i <- 8
p <- 1e6
#####################################
p_set[i] <- p
#####################################
lambda <- 0.38
type   <- "trid"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_trid_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_equal[i]   <- compare(alg = "equal",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_trid_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#####################################
lambda <- 0.23
type   <- "clus"
print(paste("loading data:",type,"p:",p))
data                 <- load_data(type=type,p=p,n=500,normalize = TRUE)$data
time_clus_squic[i]   <- compare(alg = "squic",   data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_glasso[i]  <- compare(alg = "glasso",  data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time
#time_clus_equal[i]   <- compare(alg = "equal",   data = data, lambda = 0.1, tol = tol, max_iter = max_iter)$time
#time_clus_bigquic[i] <- compare(alg = "bigquic", data = data, lambda = lambda, tol = tol, max_iter = max_iter)$time

print(paste("-----trid------"))
print(data.frame(p_set, time_trid_squic,time_trid_glasso,time_trid_bigquic,time_trid_equal), digits = 3)
print(paste("-----clus------"))
print(data.frame(p_set, time_clus_squic,time_clus_glasso,time_clus_bigquic,time_clus_equal), digits = 3)
