## ----set-options, [encoded data, 9980 characters]
options(width = 10000)

## ----setup, include = [encoded data, 9976 characters]
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = '#>'
)
knitr::opts_chunk$set(fig.height = 5)
knitr::opts_chunk$set(fig.width = 8)
knitr::opts_chunk$set(fig.align = 'center')

## ---- include = [encoded data, 9982 characters]
library(UBayFS)
data(bcw)

## ---- include = [encoded data, 9982 characters]
block_list = list()
block_list[[1]] = 1:10
block_list[[2]] = 11:20
block_list[[3]] = 21:30

block_list

## ---- include = [encoded data, 9982 characters]
block_constraints = buildConstraints(constraint_types = c('max_size'), 
                                     constraint_vars = list(1), 
                                     num_elements = length(block_list), 
                                     rho = 1, 
                                     block_list = block_list)

block_constraints

## ---- include = [encoded data, 9982 characters]
block_matrix = matrix(0, nrow = 3, ncol = ncol(bcw$data))
block_matrix[1, 1:10] = 1
block_matrix[2, 11:20] = 1
block_matrix[3, 21:30] = 1

block_matrix

## ---- include = [encoded data, 9982 characters]
block_constraints = buildConstraints(constraint_types = c('max_size', 'must_link'), 
                                     constraint_vars = list(1, c(1,2)), 
                                     num_elements = nrow(block_matrix), 
                                     rho = 1, 
                                     block_matrix = block_matrix)

block_constraints

## ---- include = [encoded data, 9982 characters]
build_block_weights = function(blocks, weights){
	return(weights[blocks])
}
prior_weights = build_block_weights(blocks = rep(c(1, 2, 3), each = 10), weights = c(0.5, 1, 2))

## ---- include = [encoded data, 9982 characters]
set.seed(1) # for reproducibility

# define model without block constraints (only feature-wise max-size constraint)
model = build.UBaymodel(data = bcw$data,
                        target = bcw$labels,
                        M = 100, 
                        tt_split = 0.75,
                        nr_features = 10,
                        method = 'mRMR',
                        prior_model ='dirichlet',
                        weights = prior_weights,
                        lambda = 1,
                        constraints = buildConstraints(constraint_types = c('max_size'), 
                                                       constraint_vars = list(3), 
                                                       num_elements = ncol(bcw$data), 
                                                       rho = 1),
                        optim_method = 'GA',
                        popsize = 100,
                        maxiter = 100,
                        shiny = FALSE
                        )

# add block constraints
model <- setConstraints(model, constraints = block_constraints)
summary(model)

## ---- include = [encoded data, 9982 characters]
model = UBayFS::train(model)
summary(model)
plot(model)

## ---- include = [encoded data, 9982 characters]
evaluateMultiple(state = model$output$feature_set[1,], model = model)

