# 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)

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

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

library(parallel)
Sys.setenv(OMP_NUM_THREADS =detectCores()/2)

tol <- 1e-6
tol_inv_set <- c(1e-6,1e-5,1e-4,1e-3,1e-2,1e-1)
p <- 2000

## Trid 
data  <- load_data(type="trid", p=p, n=500, normalize=TRUE)$data
nnzpr <- c()
error <- c()
for(i in 1:length(tol_inv_set)){

    out_squic  <- SQUIC(Y = data,lambda = 0.2, max_iter = 3, tol = tol,tol_inv=tol_inv_set[i],verbose = 1,M = NULL,X0 = NULL,W0 = NULL)

    X <- out_squic$X
    W <- out_squic$W
    E <- abs(X %*% W -  Diagonal(p))

    nnzpr[i] <- length(W@x)/p;
    error[i] <- max(E@x)
}
print("-----trid------")
print(data.frame(tol_inv_set, nnzpr,error), digits = 3)


## clus 
data  <- load_data(type="clus", p=p, n=500, normalize=TRUE)$data
nnzpr <- c()
error <- c()
for(i in 1:length(tol_inv_set)){

    out_squic <- SQUIC(Y = data,lambda = 0.2, max_iter = 3, tol = tol,tol_inv=tol_inv_set[i],verbose = 1,M = NULL,X0 = NULL,W0 = NULL)
    
    X <- out_squic$X
    W <- out_squic$W
    E <- abs(X %*% W -  Diagonal(p))

    nnzpr[i] <- length(W@x)/p;
    error[i] <- max(E@x)
}
print("-----clus------")
print(data.frame(tol_inv_set, nnzpr,error), digits = 3)