SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
Macros | Functions | Variables
bcsol.c File Reference

This file contains all the routines needed for BCLS optimization. More...

#include <slim.h>

Go to the source code of this file.

Macros

#define CS_MAX(a, b)   (((a) > (b)) ? (a) : (b))
 Compute the maximum of two.
 
#define CS_MIN(a, b)   (((a) < (b)) ? (a) : (b))
 Compute the minimum of two.
 
#define CS_FLIP(i)   (-(i)-2)
 Flip.
 
#define CS_UNFLIP(i)   (((i) < 0) ? CS_FLIP(i) : (i))
 Unflip.
 
#define CS_MARKED(w, j)   (w [j] < 0)
 Check if marked.
 
#define CS_MARK(w, j)   { w [j] = CS_FLIP (w [j]) ; }
 Mark.
 
#define CS_CSC(A)   (A && (A->nz == -1))
 CSC.
 
#define CS_TRIPLET(A)   (A && (A->nz >= 0))
 Triplet.
 

Functions

int call_back (BCLS *ls, void *UsrWrk)
 call_back function, periodically called by BCLS to test if the user wants to exit. This is from BCLS.
 
int call_back_it (BCLS *ls, void *UsrWrk)
 call_back function, immediately terminate BCLS iterations based on how many iterations it runs
 
int pretty_printer (void *io_file, char *msg)
 Pretty_printer, this is the print-routine that will be used by BCLS for its output. This is from BCLS.
 
void * cs_free (void *p)
 Wrapper for free.
 
void * cs_malloc (int n, size_t size)
 Wrapper for malloc.
 
void * cs_calloc (int n, size_t size)
 Wrapper for calloc.
 
cscs_spfree (cs *A)
 Free a sparse matrix.
 
cscs_spalloc (int m, int n, int nzmax, int values, int triplet)
 Allocate a sparse matrix (triplet form or compressed-column form)
 
void dload (const int n, const double alpha, double x[])
 Load a constant into a vector.
 
int Aprod (int mode, int m, int n, int nix, int ix[], double x[], double y[], void *UsrWrk)
 Aprod, matrix-vector products. This is from BCLS.
 
void bcsol (ctrl_t *ctrl, gk_csr_t *AA, double *bb, double *x, worksp *Wrk, double *bl, double *bu, double beta, double *c)
 BCLS learning. This is from BCLS.
 

Variables

int bcls_niters = 0
 Number of iterations.
 

Detailed Description

This file contains all the routines needed for BCLS optimization.

Definition in file bcsol.c.

Function Documentation

int Aprod ( int  mode,
int  m,
int  n,
int  nix,
int  ix[],
double  x[],
double  y[],
void *  UsrWrk 
)

Aprod, matrix-vector products. This is from BCLS.

If mode == BCLS_PROD_A (0), compute y <- A *x, with x untouched; and if mode == BCLS_PROD_At (1), compute x <- A'*y, with y untouched.

Definition at line 163 of file bcsol.c.

References worksp::A, worksp::acol, cs_sparse::i, cs_sparse::p, and cs_sparse::x.

Referenced by bcsol().

{
int i, j, k, l;
double aij;
double xj, sum;
worksp * Wrk = (worksp *)UsrWrk;
cs *A = (cs *)Wrk->A;
int * Ai = A->i;
int * Ap = A->p;
float * Ax = A->x;
int * acol = Wrk->acol;
if (mode == BCLS_PROD_A) {
gk_dset(m, 0.0, y);
for (l = 0; l < nix; l++) {
j = ix[l];
/* skip the inactive column */
if (!acol[j]) continue;
xj = x[j];
if (xj == 0.0)
; // Relax.
else
for (k = Ap[j]; k < Ap[j+1]; k++) {
aij = Ax[k];
/* this is to handle float-valued A matrix */
i = Ai[k];
y[i] += aij * xj;
}
}
}
else if (mode == BCLS_PROD_At) {
for (l = 0; l < nix; l++) {
j = ix[l];
sum = 0;
/* skip the inactive column */
if (!acol[j]){
x[j] = sum;
continue;
}
for (k = Ap[j]; k < Ap[j+1]; k++) {
aij = Ax[k];
/* this is to handle float-valued A matrix */
i = Ai[k];
sum += aij * y[i];
}
x[j] = sum;
}
}
return 0;
}
void bcsol ( ctrl_t ctrl,
gk_csr_t *  AA,
double *  bb,
double *  x,
worksp Wrk,
double *  bl,
double *  bu,
double  beta,
double *  c 
)

BCLS learning. This is from BCLS.

This is to solve the problem

\[ \begin{array}{ll} \displaystyle\mathop{\hbox{minimize}}_x & \frac12\|Ax-a_i\|_2^2 + \frac{1}{2}\beta\|x\|_2^2 + \lambda \|x\|_1 \\ \hbox{subject to} & 0 \le x \\ & x_i = 0 \\ \end{array} \]

Definition at line 247 of file bcsol.c.

References Aprod(), bcls_niters, call_back(), call_back_it(), ctrl_t::dbglvl, ctrl_t::max_bcls_niters, ctrl_t::optTol, and pretty_printer().

Referenced by slim_fs_learn(), and slim_learn().

{
/* Problem dimensions. */
int m = AA->nrows;
int n = AA->ncols;
/* init a bcls problem */
BCLS *ls = bcls_create_prob( m, n );
bcls_set_problem_data(ls, m, n, Aprod, Wrk, beta, x, bb, c, bl, bu);
/* set up tolerance */
ls->optTol = ctrl->optTol;
/* whatever */
bcls_set_print_hook( ls, stdout, pretty_printer );
ls->proj_search = proj_search;
ls->newton_step = newton_step;
/* if (ctrl->test_bcls) */
if (ctrl->max_bcls_niters > 0)
ls->CallBack = call_back_it;
else
ls->CallBack = call_back;
/* call the solver */
int err = bcls_solve_prob( ls );
/* solution */
if (ctrl->dbglvl > 1){
int nnzx = 0;
printf("\n Solution\n --------\n");
printf("%4s %18s %1s %18s %1s %18s %18s\n",
"Var","Lower","","Value","","Upper","Gradient");
for (int j = 0; j < n; j++) {
if (x[j] > 1e-10){
nnzx ++;
char * blActiv = "";
char * buActiv = "";
if (x[j] - bl[j] < ls->epsx) blActiv = "=";
if (bu[j] - x[j] < ls->epsx) buActiv = "=";
printf("%4d %18.11e %1s %18.11e %1s %18.11e %18.11e\n",
j+1, bl[j], blActiv, x[j], buActiv, bu[j], (ls->g)[j]);
}
}
printf("%d nnz solution values\n", nnzx);
}
/* free the problem */
err = bcls_free_prob( ls );
}