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

test_accuracy <- function(X_star,data,lambda_set) {

  p <- dim(data)[1]
  n <- dim(data)[2]

  out_squic   <- c();
  out_glasso  <- c();
  out_bigquic <- c();
  out_equal   <- c();	

  for (i in 1:length(lambda_set)) {
    lambda  <- lambda_set[i];

    print(sprintf("Benchmark for lambda=%f started",lambda));
    
    out <- compare(alg="squic", data=data, lambda=lambda, X_star=X_star, M = NULL);
    out_squic[i] <- out$f1;
    print(paste(type,":p=",p,":SQUIC:","nnzpr=",nnzero(out$X)/p))
    
    out <- compare(alg="glasso", data=data, lambda=lambda, X_star=X_star, M = NULL);
    out_glasso[i]  <-  out$f1;	
    print(paste(type,":p=",p,":glasso:","nnzpr=",nnzero(out$X)/p))
    
    out <- compare(alg="bigquic", data=data, lambda=lambda , X_star=X_star, M = NULL);
    out_bigquic[i] <- out$f1;	
    print(paste(type,":p=",p,":BigQUIC:","nnzpr=",nnzero(out$X)/p))

    out <- compare(alg="equal", data=data, lambda=lambda, X_star=X_star);
    out_equal[i] <- out$f1;
    print(paste(type,":p=",p,":EQUAL:","nnzpr=",nnzero(out$X)/p))
    
  }
  
  output <- list(
    "f1_squic"   = out_squic, 
    "f1_glasso"  = out_glasso,	
    "f1_bigquic" = out_bigquic,	
    "f1_equal"   = out_equal 		
  )
  
  return(output);
}

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

p <- 1000;
lambda_set <- seq(.05, .95,.1);

# Generate data
type    <- "trid";
out     <- load_data(type=type, p=p, n=500, normalize=TRUE);
X_star  <- out$X_star;
data    <- out$data;
result  <- test_accuracy(X_star=X_star,data=data,lambda_set=lambda_set)
print("-----trid------")
print(data.frame(lambda_set, result$f1_squic, result$f1_glasso, result$f1_bigquic, result$f1_equal), digits = 3)

# Generate data
type    <- "clus";
out     <- load_data(type=type, p=p, n=500, normalize=TRUE);
X_star  <- out$X_star;
data    <- out$data;
result  <- test_accuracy(X_star=X_star,data=data,lambda_set=lambda_set) 
print("-----clus------")
print(data.frame(lambda_set, result$f1_squic, result$f1_glasso, result$f1_bigquic, result$f1_equal), digits = 3)