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