Commit ad88e83c authored by Giovanne Marcelo's avatar Giovanne Marcelo
Browse files

Merge branch 'algorithm' into 'master'

Algorithm



See merge request !3
parents 739753bc c8828594
Pipeline #6433 failed with stage
in 4 minutes and 50 seconds
......@@ -23,7 +23,6 @@ CFLAGS = -std=c99 -g -O3 \
-Wold-style-definition \
-Woverlength-strings \
-Wpacked \
-Wpadded \
-Wpointer-arith \
-Wredundant-decls \
-Wshadow \
......@@ -34,12 +33,15 @@ CFLAGS = -std=c99 -g -O3 \
-Wundef \
-Wunsafe-loop-optimizations \
-Wvolatile-register-var \
-Wwrite-strings
LFLAGS = -lpq -ljansson -lgsl -lgslcblas -lm
-Wwrite-strings
CFLAGS += `pkg-config --cflags glib-2.0`
LFLAGS = -lpq -ljansson -lgsl -lgslcblas -lm
LFLAGS += `pkg-config --libs glib-2.0`
INCL = -I./include
SRC = ./src
DEPS = ./include/db_interface.h ./include/log.h ./include/main.h ./include/data_prepare.h ./include/matrix.h
OBJ = db_interface.o log.o matrix.o data_prepare.o main.o
DEPS = ./include/db_interface.h ./include/log.h ./include/main.h ./include/data_prepare.h ./include/matrix.h \
./include/algorithm.h
OBJ = db_interface.o log.o matrix.o data_prepare.o algorithm.o main.o
APP=recommender
all: $(APP)
......
#ifndef _ALGORITHM_H
#define _ALGORITHM_H
#include "matrix.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <glib.h>
#define double_args double, double, double, double*, double*
#define omega GSList**
typedef struct {
matrix residual;
matrix W;
matrix H;
omega omegai;
omega omegaj;
double lamb;
long unsigned int k;
long unsigned int T;
}algorithm_args;
void ccd(algorithm_args *args);
void ccd_pp(matrix residual, matrix W, matrix H, double lamb, int k);
void perform(algorithm_args *args);
void update_row_residual(matrix residual, GSList * omegaj,
double s, matrix W, matrix H, long unsigned int j,
long unsigned int t);
void update_col_residual(matrix residual, GSList * omegai,
double z, matrix W, matrix H, long unsigned int i,
long unsigned int t);
void z(double r, double w, double h, double *numerator, double *denominator);
void s(double r, double w, double h, double *numerator, double *denominator);
double find_minimun(matrix residual, matrix W, matrix H, double lamb,
GSList * om ,long unsigned int t,
long unsigned int i, void (*f)(double_args));
#endif
......@@ -3,5 +3,7 @@
#include "db_interface.h"
#include "data_prepare.h"
#include "log.h"
#include <glib.h>
#endif
......@@ -10,5 +10,7 @@ typedef gsl_matrix* matrix;
matrix matrix_alloc(long unsigned int rows, long unsigned int cols);
void print_matrix(matrix m);
void matrix_copy(matrix dest, matrix src);
void matrix_set(matrix m, long unsigned int row, long unsigned int col, double x);
double matrix_get(matrix m, long unsigned int row, long unsigned int col);
#endif
/*
* Implementation of a recommender algorithm with ccd++ factorization
* http://www.cs.utexas.edu/~cjhsieh/icdm-pmf.pdf
* This source code is licensed under the GNU General Public License,
* Version 3. See the file LICENSE for more details.
*
*/
#include "algorithm.h"
/* CCD
* Input: Initial R = A, W = 0, H, λ, and k
* for iter = 1, 2, . . . , T do
* for i = 1, 2, . . . , m do
* for t = 1, 2, . . . , k do
* Obtain z∗ using find_minimun(z)
* Update R using update_col_residual()
* wit using matrix_set()
* end for
* end for
*
* for j = 1, 2, . . . , n do
* for t = 1, 2, . . . , k do
* Obtain s∗ using find_minimun(s).
* Update R using update_row_residual()
* hjt using matrix_set()
* end for
* end for
* end for
*/
void ccd(algorithm_args *args) {
double rating;
for(long unsigned int iter = 0; iter < args->T ; iter++) {
for(long unsigned int i = 0; i < args->residual->size1 ; i++ ) {
for(long unsigned int t = 0; t < args->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \
args->omegai[i], t, i, z);
update_col_residual(args->residual, args->omegai[i], rating, \
args->W, args->H, i, t ) ;
matrix_set(args->W, i, t, rating);
}
}
for(long unsigned int j = 0; j < args->residual->size2; j++) {
for(long unsigned int t = 0; t < args->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \
args->omegaj[j], t, j, s);
update_row_residual(args->residual, args->omegaj[j], rating, \
args->W, args->H, j, t);
matrix_set(args->H, j, t, rating);
}
}
}
}
/*
* CCD++
* Input: Initial R = A, W = 0, H, λ, and k
* for iter = 1, 2, . . . do
* for t = 1, 2, . . . , k do
* Get (u, v) using T CCD iterations for (16).
* Update R and (w¯t, h¯t) using (17) and (18).
* end for
* end for
*/
/* void ccd_pp(matrix residual, matrix W, matrix H, double lamb, int k) {
}
*/
void perform(algorithm_args *args) {
ccd(args);
}
void update_col_residual(matrix residual, GSList * omegai,
double z, matrix W, matrix H, long unsigned int i,
long unsigned int t) {
for(guint iter = 0; iter < g_slist_length(omegai); iter++) {
long unsigned int *elem = (long unsigned int*) g_slist_nth_data(omegai, iter);
double Rij = matrix_get(residual, i, *elem);
double Wit = matrix_get(W, i, t);
double Hjt = matrix_get(H, *elem, t);
double result = Rij - (z - Wit) * Hjt;
matrix_set(residual, i, *elem, result );
}
}
void update_row_residual(matrix residual, GSList * omegaj,
double s, matrix W, matrix H, long unsigned int j,
long unsigned int t) {
for(guint iter = 0; iter < g_slist_length(omegaj); iter++){
long unsigned int* elem = (long unsigned int*) g_slist_nth_data(omegaj, iter);
double Rij = matrix_get(residual, *elem, j);
double Wit = matrix_get(W, *elem, t);
double Hjt = matrix_get(H, j, t);
double result = Rij - (s - Hjt) * Wit;
matrix_set(residual, *elem, j, result );
}
}
void z(double r, double w, double h, double *numerator, double *denominator ) {
*numerator += (r + w * h) * h;
*denominator += h * h;
}
void s(double r, double w, double h, double *numerator, double *denominator) {
*numerator += (r + h * w) * w;
*denominator += w * w;
}
double find_minimun(matrix residual, matrix W, matrix H, double lamb,
GSList * om ,long unsigned int t,
long unsigned int i,void (*f)(double_args)) {
double Rij, Wit, Hjt, numerator, denominator;
numerator = denominator = .0;
for(guint iter = 0; iter < g_slist_length(om); iter++) {
long unsigned int * elem = (long unsigned int*) g_slist_nth_data(om, iter);
Rij = matrix_get(residual,i, *elem);
Wit = matrix_get(W, i, t);
Hjt = matrix_get(H, *elem, t);
(*f)(Rij, Wit, Hjt,&numerator, &denominator);
}
denominator += lamb;
return ( numerator / denominator );
}
......@@ -55,7 +55,7 @@ PGconn* db_connect(void) {
return conn;
}
DBresult* db_select( const char *columns, const char *table, const char *where) {
DBresult* db_select( const char *columns, const char *table, const char *where){
PGconn *conn = db_connect();
char query[QUERY_LEN];
......
......@@ -3,6 +3,8 @@
int main(int argc, char **argv) {
info("Program started!");
matrix m = cartesian_product(10,10);
print_matrix(m);
......
......@@ -12,6 +12,14 @@ matrix matrix_alloc(long unsigned int rows, long unsigned int cols) {
return gsl_matrix_alloc(rows, cols);
}
void matrix_set(matrix m, long unsigned int row, long unsigned int col, double x) {
gsl_matrix_set(m, row, col, x);
}
double matrix_get(matrix m, long unsigned int row, long unsigned int col) {
return gsl_matrix_get(m, row, col);
}
void print_matrix(matrix m) {
for (size_t i=0; i<m->size1;++i){
for (size_t j=0; j<m->size2; ++j)
......@@ -19,3 +27,6 @@ void print_matrix(matrix m) {
puts(" ");
}
}
void matrix_copy(matrix dest, matrix src) {
gsl_matrix_memcpy (dest,src);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment