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

migrate#2

parent c5b79c8a
Pipeline #6435 passed with stage
in 4 minutes and 53 seconds
CC = g++ CC = g++
CFLAGS = -g -O3 \ CFLAGS = -g -O3 -std=c++11\
-pipe \ -pipe \
-ggdb3 -Wstrict-overflow=5 -fstack-protector-all \ -ggdb3 -Wstrict-overflow=5 -fstack-protector-all \
-W -Wall -Wextra \ -W -Wall -Wextra \
...@@ -22,7 +22,6 @@ CFLAGS = -g -O3 \ ...@@ -22,7 +22,6 @@ CFLAGS = -g -O3 \
-Wpadded \ -Wpadded \
-Wpointer-arith \ -Wpointer-arith \
-Wredundant-decls \ -Wredundant-decls \
-Wshadow \
-Wsign-compare \ -Wsign-compare \
-Wstack-protector \ -Wstack-protector \
-Wstrict-aliasing=2 \ -Wstrict-aliasing=2 \
...@@ -33,8 +32,9 @@ CFLAGS = -g -O3 \ ...@@ -33,8 +32,9 @@ CFLAGS = -g -O3 \
LFLAGS = -lpq -ljansson -lgsl -lgslcblas -lm LFLAGS = -lpq -ljansson -lgsl -lgslcblas -lm
INCL = -I./include INCL = -I./include
SRC = ./src SRC = ./src
DEPS = ./include/db_interface.h ./include/log.h ./include/main.h ./include/data_prepare.h ./include/matrix.h DEPS = ./include/db_interface.h ./include/log.h ./include/main.h \
OBJ = db_interface.o log.o matrix.o data_prepare.o main.o ./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 APP=recommender
all: $(APP) all: $(APP)
...@@ -43,8 +43,9 @@ all: $(APP) ...@@ -43,8 +43,9 @@ all: $(APP)
$(APP): $(OBJ) $(APP): $(OBJ)
@$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) @$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)
%.o: $(SRC)/%.c $(DEPS) %.o: $(SRC)/%.cpp $(DEPS)
@$(CC) $(CFLAGS) -c -o $@ $< $(INCL) @$(CC) $(CFLAGS) -c -o $@ $< $(INCL)
clean: clean:
rm -f $(OBJ) $(APP) core *~ ./include/*~ *.o .*.swp ./include/*.swp ./src/*.swp ./include/.*.swp ./src/.*.swp rm -f $(OBJ) $(APP) core *~ ./include/*~ *.o .*.swp ./include/*.swp \
./src/*.swp ./include/.*.swp ./src/.*.swp
...@@ -2,41 +2,53 @@ ...@@ -2,41 +2,53 @@
#define _ALGORITHM_H #define _ALGORITHM_H
#include "matrix.h" #include "matrix.h"
#include <assert.h> #include "log.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h>
#include <string.h> #include <string.h>
#include <glib.h> #include <list>
#include <vector>
using namespace std;
#define double_args double, double, double, double*, double* #define double_args double, double, double, double*, double*
#define omega GSList**
typedef struct {
class Algorithm {
public:
void perform();
Algorithm(matrix residual, matrix W, matrix H,
vector<list<long unsigned int> > omegai,
vector<list<long unsigned int> > omegaj, double lamb,
long unsigned int k, long unsigned int T);
private:
matrix residual; matrix residual;
matrix W; matrix W;
matrix H; matrix H;
omega omegai; vector<list<long unsigned int> > omegai;
omega omegaj; vector<list<long unsigned int> > omegaj;
double lamb; double lamb;
long unsigned int k; long unsigned int k;
long unsigned int T; long unsigned int T;
}algorithm_args;
void ccd();
void ccd_pp();
void ccd(algorithm_args *args); void update_row_residual(list<long unsigned int> omegaj,
void ccd_pp(matrix residual, matrix W, matrix H, double lamb, int k); double s,long unsigned int j,
void perform(algorithm_args *args); long unsigned int t);
void update_row_residual(matrix residual, GSList * omegaj, void update_col_residual(list<long unsigned int> omegai,
double s, matrix W, matrix H, long unsigned int j, double z, long unsigned int i,
long unsigned int t); long unsigned int t);
void update_col_residual(matrix residual, GSList * omegai, double find_minimun(list<long unsigned int> om ,long unsigned int t,
double z, matrix W, matrix H, long unsigned int i, long unsigned int i, void (*f)(double_args));
long unsigned int t);
};
void z(double r, double w, double h, double *numerator, double *denominator); void z(double r, double w, double h, double *numerator, double *denominator);
void s(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 #endif
...@@ -4,5 +4,8 @@ ...@@ -4,5 +4,8 @@
#include "db_interface.h" #include "db_interface.h"
#include "data_prepare.h" #include "data_prepare.h"
#include "log.h" #include "log.h"
#include "algorithm.h"
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#endif #endif
...@@ -29,28 +29,24 @@ ...@@ -29,28 +29,24 @@
* end for * end for
*/ */
void ccd(algorithm_args *args) { void Algorithm::ccd() {
double rating; double rating;
for(long unsigned int iter = 0; iter < args->T ; iter++) { for(long unsigned int iter = 0; iter < this->T ; iter++) {
for(long unsigned int i = 0; i < args->residual->size1 ; i++ ) { for(long unsigned int i = 0; i < this->residual->size1 ; i++ ) {
for(long unsigned int t = 0; t < args->k; t++) { for(long unsigned int t = 0; t < this->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \ rating = find_minimun(this->omegai[i], t, i, z);
args->omegai[i], t, i, z); update_col_residual(this->omegai[i], rating, i, t ) ;
update_col_residual(args->residual, args->omegai[i], rating, \ matrix_set(this->W, i, t, 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 j = 0; j < this->residual->size2; j++) {
for(long unsigned int t = 0; t < args->k; t++) { for(long unsigned int t = 0; t < this->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \ rating = find_minimun(this->omegaj[j], t, j, s);
args->omegaj[j], t, j, s); update_row_residual(this->omegaj[j], rating, j, t);
update_row_residual(args->residual, args->omegaj[j], rating, \ matrix_set(this->H, j, t, rating);
args->W, args->H, j, t);
matrix_set(args->H, j, t, rating);
} }
} }
} }
...@@ -72,35 +68,50 @@ void ccd(algorithm_args *args) { ...@@ -72,35 +68,50 @@ void ccd(algorithm_args *args) {
} }
*/ */
void perform(algorithm_args *args) { Algorithm::Algorithm(matrix residual, matrix W, matrix H,
ccd(args); vector<list<long unsigned int> > omegai,
vector<list<long unsigned int> > omegaj, double lamb,
long unsigned int k, long unsigned int T) {
this->residual = residual;
this->W = W;
this->H = H;
this->omegai = omegai;
this->omegaj = omegaj;
this->lamb = lamb;
this->k = k;
this->T = T;
} }
void update_col_residual(matrix residual, GSList * omegai, void Algorithm::perform() {
double z, matrix W, matrix H, long unsigned int i,
long unsigned int t) { info("Algorithm Args is builded!");
this->ccd();
info("CCD Performed!");
}
for(guint iter = 0; iter < g_slist_length(omegai); iter++) { void Algorithm::update_col_residual(list<long unsigned int> omegai, double z,
long unsigned int *elem = (long unsigned int*) g_slist_nth_data(omegai, iter); long unsigned int i, long unsigned int t) {
double Rij = matrix_get(residual, i, *elem);
double Wit = matrix_get(W, i, t); for (list<long unsigned int>::iterator it = omegai.begin(); it != omegai.end(); it++) {
double Hjt = matrix_get(H, *elem, t); double Rij = matrix_get(this->residual, i, *it);
double Wit = matrix_get(this->W, i, t);
double Hjt = matrix_get(this->H, *it, t);
double result = Rij - (z - Wit) * Hjt; double result = Rij - (z - Wit) * Hjt;
matrix_set(residual, i, *elem, result ); matrix_set(this->residual, i, *it, result );
} }
} }
void update_row_residual(matrix residual, GSList * omegaj, void Algorithm::update_row_residual(list<long unsigned int> omegaj, double s,
double s, matrix W, matrix H, long unsigned int j, long unsigned int j, long unsigned int t) {
long unsigned int t) {
for(guint iter = 0; iter < g_slist_length(omegaj); iter++){ for (list<long unsigned int>::iterator it = omegaj.begin(); it != omegaj.end(); it++){
long unsigned int* elem = (long unsigned int*) g_slist_nth_data(omegaj, iter); double Rij = matrix_get(this->residual, *it, j);
double Rij = matrix_get(residual, *elem, j); double Wit = matrix_get(this->W, *it, t);
double Wit = matrix_get(W, *elem, t); double Hjt = matrix_get(this->H, j, t);
double Hjt = matrix_get(H, j, t);
double result = Rij - (s - Hjt) * Wit; double result = Rij - (s - Hjt) * Wit;
matrix_set(residual, *elem, j, result ); matrix_set(this->residual, *it, j, result );
} }
} }
...@@ -114,21 +125,21 @@ void s(double r, double w, double h, double *numerator, double *denominator) { ...@@ -114,21 +125,21 @@ void s(double r, double w, double h, double *numerator, double *denominator) {
*denominator += w * w; *denominator += w * w;
} }
double find_minimun(matrix residual, matrix W, matrix H, double lamb, double Algorithm::find_minimun(list<long unsigned int> om ,long unsigned int t,
GSList * om ,long unsigned int t,
long unsigned int i,void (*f)(double_args)) { long unsigned int i,void (*f)(double_args)) {
double Rij, Wit, Hjt, numerator, denominator; double Rij, Wit, Hjt, numerator, denominator;
numerator = denominator = .0; numerator = denominator = .0;
for(guint iter = 0; iter < g_slist_length(om); iter++) { for (list<long unsigned int>::iterator it = om.begin(); it != om.end(); it++){
long unsigned int * elem = (long unsigned int*) g_slist_nth_data(om, iter); Rij = matrix_get(this->residual,i, *it);
Rij = matrix_get(residual,i, *elem); Wit = matrix_get(this->W, i, t);
Wit = matrix_get(W, i, t); Hjt = matrix_get(this->H, *it, t);
Hjt = matrix_get(H, *elem, t);
(*f)(Rij, Wit, Hjt,&numerator, &denominator); (*f)(Rij, Wit, Hjt,&numerator, &denominator);
} }
denominator += lamb; denominator += lamb;
return ( numerator / denominator ); return ( numerator / denominator );
} }
...@@ -4,9 +4,5 @@ ...@@ -4,9 +4,5 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
info("Program started!"); info("Program started!");
matrix m = cartesian_product(10,10);
print_matrix(m);
return 0; return 0;
} }
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