Commit 46467bb1 authored by Alexandre Peres Arias's avatar Alexandre Peres Arias
Browse files

add recomendacao por usuario


Signed-off-by: Alexandre Peres Arias's avatarAlexandre Peres Arias <apa13@inf.ufpr.br>
parent 5c86c374
Pipeline #7034 failed with stage
in 48 seconds
This diff is collapsed.
#!/bin/bash
echo "200 1900" > base1
while read l; do
for i in `seq 1 1900`; do
......@@ -10,6 +10,7 @@ while read l; do
line=`echo $l | cut -d":" -f2 `
for pos in $line; do
#echo "POS = $pos"
vet[$pos]=1
done
......@@ -22,14 +23,14 @@ for l in `seq 31 200`; do
VET[$i]=0
done
QTDE=$(( ( RANDOM % 15 ) + 1))
QTDE=$(( ( RANDOM % 18 ) + 1))
while [[ QTDE -gt 0 ]]; do
POS=$(( ( RANDOM % 1900 ) + 1))
VET[$POS]=1
((QTDE--))
done
echo ${vet[*]} | cut -d" " -f-1900 >> base1
echo ${VET[*]} | cut -d" " -f-1900 >> base1
done
......
No preview for this file type
#include "hamming.h"
//retorna somente o viznho mais parecido
//tentar mesclar mais
/*
extern inline int vizinhos(hamming_str *ham){
int maior=0, sum_i=0, sum_a=1;
for(int i=0; i<K; ++i){
for(int j=i+1;j<K;++j)
if(ham[j].ind == ham[i].ind)
++sum_i;
if(sum_a < sum_i){
sum_a=sum_i;
maior=i;
}
}
return ham[maior].ind;
}
*/
extern int *hamming(int **M, int *user ){
int viz_mais;
int aval=0, dist=0, indx, min;
int *buf;
hamming_str *tmp_hamm;
//qntos items avaliados para esse usuario
for(int j=0; j<COL; ++j) aval+=user[j];
for(int i=0; i<LIN; ++i){
buf = M[i];
dist=0;
for(int j=0; j<COL; ++j) //fazer unroll desse laco
dist += user[j] & buf[j];
tmp_hamm = hamm[i];
min=INT_MAX; indx=0;
for(int k=0; k<K; ++k){
///pega a menor distancia
if(min > tmp_hamm[k].dis){
min = tmp_hamm[k].dis;
indx = k;
}
}
if(dist > min){
tmp_hamm[indx].dis = dist;
tmp_hamm[indx].ind = i;
}
}
return(recomenda(user, tmp_hamm));
}
//criar func q pega os melhores vizinhos
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <pthread.h>
#include <unistd.h>
typedef struct hamm{
int ind;
float dis;
} hamming_str;
typedef struct n_it{
int sum;
int ind;
}n_itens;
int N_THREADS,K,DOM;
int LIN,COL,N;
int **dados, **recom;
hamming_str **hamm;
extern inline int* hamming(int **M, int *user);
//extern inline int **alloc_data(FILE *f, int h);
//extern inline void *calc_dist(void *in);
//extern inline int* recomenda(int *user, hamming_str *ham);
5 8
0 0 0 1 0 1 1 1
0 0 0 0 0 1 1 0
1 0 0 1 0 1 0 0
0 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
......@@ -49,9 +49,9 @@ inline int *hamming(int **M, int user_id ){
dist=0;
for(int j=0; j<COL; ++j){ //fazer unroll desse laco
dist += user[j] & buf[j];
//printf(" user_id %d j %d i %d dist %d\n", user_id, j,i, dist);
}
//printf(" user_id %d i %d dist %d\n", user_id, i, dist);
for(int k=0; k<K; ++k){
///pega a menor distancia
if(min > tmp_hamm[k].dis){
......@@ -65,10 +65,11 @@ inline int *hamming(int **M, int user_id ){
min = dist;
}
}
//printf("RECOMENDANDO USER %d \n",user_id);
//printf("\nRECOMENDANDO USER %d ",user_id);
//printf("HAM: ");
//for(int i=0; i<K;++i)
// printf(" usr %d, dis %d .",tmp_hamm[i].ind, tmp_hamm[i].dis);
//puts("");
return(recomenda(user, tmp_hamm));
}
......@@ -174,7 +175,7 @@ extern inline void *calc_dist(void *in){
int max = ind_th*DOM + DOM;
if(ind_th == (N_THREADS-1) ) max = LIN;
printf("***** THREAD %d domini = [%d,%d]\n",ind_th, ind_th*DOM,(max-1));
//printf("***** THREAD %d domini = [%d,%d]\n",ind_th, ind_th*DOM,(max-1));
//salva vetor de recomendacao de cada usuario
for(int i=(ind_th*DOM); i<max; ++i)
recom[i] = hamming(dados, i);
......@@ -185,13 +186,14 @@ extern inline void *calc_dist(void *in){
/* ##################################################### */
int main(int argc, char *argv[]){
if(argc != 5){puts("ERRO ENTRADA");exit(1);}
int U;
if(argc != 6){puts("ERRO ENTRADA");exit(1);}
FILE *f1 = fopen(argv[1],"r");
K = atoi(argv[2]); //num de vizinhos para comparação
N_THREADS = atoi(argv[3]);
N = atoi(argv[4]); //# de recomendacoes
U=atoi(argv[5]);
if( fscanf(f1,"%d",&LIN) == EOF ) {puts("ERRO DE LEITURA 1"); exit(1);}
if( fscanf(f1,"%d",&COL) == EOF ) {puts("ERRO DE LEITURA 2"); exit(1);}
......@@ -206,7 +208,6 @@ int main(int argc, char *argv[]){
for(int i=0; i<LIN; ++i)
for(int j=0; j<K; ++j) hamm[i][j].dis = 0;
puts(" AAAA");
printf("\n***** Dominio Per Thread = %d \n",DOM);
......@@ -232,13 +233,18 @@ int main(int argc, char *argv[]){
puts("**** RECOMENDACOES ****");
for(int i=0; i<LIN; ++i){
printf(" ---- USUARIO %d:", i);
if( U != -1){
printf(" ---- USUARIO %d:", U);
for(int j=0; j<N; ++j)
printf(" %d",recom[i][j]);
puts("");
printf(" %d", recom[U][j]+1);
}else{
for(int i=0; i<LIN; ++i){
printf(" ---- USUARIO %d:", i);
for(int j=0; j<N; ++j)
printf(" %d",recom[i][j]+1);
puts("");
}
}
puts("");
puts("***** TERMINOU PROCESSAMENTO!");
......
1: 8 3 16 240 1282 1755
2: 8 3 1854 1003 16 218 600
3: 600 1755 1756 3 8 1000 1008
4: 1003 715 218 1744 701 3 214 5
5: 16 1282 1756 715 1003 1008 8 3 600
6: 218 214 1754 715 8 3 16 600
7: 600 3 218 1854 1000 1003 1008 8
8: 16 214 600 1755 1854 715 1008
9: 8 3 1008 214 1856 715 1003 1000
10: 214 600 1755 1756 1854 715
11: 8 4 7 11 213 215 706
12: 1126 12 13 4 209 213 214 22 28
13: 709 706 228 11 12 13 7 1126 1011 628
14: 703 1011 211 209 28 22
15: 11 7 4 1016 1017 28 22
1: 8 3 16 240 1282 1755 218 1854 715
2: 8 3 1854 1003 16 218 600 1000 1008
3: 600 1755 1756 3 8 1000 1008 16 1282
4: 1003 715 218 1744 701 3 214 5 1282
5: 16 1282 1756 715 1003 1008 8 3 600 1856
6: 218 214 1754 715 8 3 16 600 1854
7: 600 3 218 1854 1000 1003 1008 8 1856
8: 16 214 600 1755 1854 715 1008 218
9: 8 3 1008 214 1856 715 1003 1000 16
10: 214 600 1755 1756 1854 715 1003 1000 1008
11: 8 4 7 11 213 215 706 1126 22 209
12: 1126 12 13 4 209 213 214 22 28 1016 610 11
13: 709 706 228 11 12 13 7 1126 1011 628 4 703
14: 703 1011 211 209 28 22 13 624 1017
15: 11 7 4 1016 1017 28 22 1011 706 1091
16: 4 28 214 703 610 1011 1017 1126
17: 1091 1011 1005 5 28 12 4
17: 1091 1011 1005 5 28 12 4 1126 1016
18: 600 28 1250 228 11 213 1017 1005 4 7
19: 22 209 214 224 703 228 1000 701 1016 1121 7 4 11
20: 8 5 7 11 13 28 213 215 228 1011
20: 8 5 7 11 13 28 213 215 228 1011 4 12 22
21: 5 9 23 210 219 224 1203 1313 1380 1785 701 704 1004 1095
22: 1121 1095 1004 5 9 10 4 204
22: 1121 1095 1004 5 9 10 4 204 210 1895 701 1002
23: 9 10 23 204 210 217 250 1313 1380 701
24: 1005 1116 1121 5 6 10 210 219 224 23
25: 6 10 205 217 224 1380 7 2 1002 11
25: 6 10 205 217 224 1380 7 2 1002 11 1002 702
26: 1121 1116 5 6 9 1203 1250 1313 1744 1895 23 704
27: 5 6 9 224 715 600 1744 1785 701 1002 1004
28: 219 217 210 610 1785 1744 2005 204 1121
......
#include "recomenda.h"
//dentre os vizinhos similares, seleciona os itens ainda nao avaliados pelo
//usuario que mais aparecem entre os K vizinhos
//futuramente deve ser feita uma análise mais interessante dos itens, talvez
//comarando similaridade entre itens
//
extern int *recomenda( int *user, hamming_str *ham){
int tot;
n_itens *nit; //num de interseccoes entre os likes, botar o min na prim posicao
nit = (n_itens*)malloc(sizeof(n_itens)*N); //N o num de recomendacoes
for(int j=0; j<COL; ++j){
if(user[j]){
tot=0;
for(int k=0; k<K; ++k) tot+=dados[ham[k].ind][j];
if(tot > nit[0].sum){
nit[0].sum = tot; nit[0].ind = j;
//bota min na primeira
ordena_min(nit);
}
}
}
return ordena(nit);
}
inline void ordena_min(n_itens *nit){
int min=nit[0].sum, id = 0;
for(int i=1; i<K;++i)
if(min > nit[i].sum){
min = nit[i].sum;
id=i;
}
//troca min
if(id){
int tmp = nit[id].ind;
nit[id].sum = nit[0].sum;
nit[id].ind = nit[0].ind;
nit[0].sum=min;
nit[0].ind = tmp;
}
}
inline int *ordena(n_itens *nit){
int maior_ind;
int *nit_n;
nit_n = (int*) malloc(sizeof(int)*N);
for(int n=0; n<N; ++n){
maior_ind = nit[0].ind;
for(int i=1; i<K; ++i)
if(nit[maior_ind].sum > nit[i].sum)
maior_ind = i;
nit_n[n] = nit[maior_ind].ind;
nit[maior_ind].sum = 0;
}
free(nit);
return nit_n;
}
extern inline int* recomenda(int *user, hamming_str *ham);
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