pageRank updated, tjs buggé
git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@207815 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
parent
647d7d8730
commit
8c095b3b86
|
@ -1,40 +1,27 @@
|
||||||
package body Google_Naive is
|
package body Google_Naive is
|
||||||
|
|
||||||
function "*"(left, right: T_Google) return T_Google is
|
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur is
|
||||||
mat: T_Google;
|
vec: T_Vecteur;
|
||||||
Compteur: T_Element := 0.0;
|
c: T_Element;
|
||||||
begin
|
begin
|
||||||
initialize(mat);
|
initialize(vec);
|
||||||
for i in 0..left'Length(1) loop
|
for i in 0..N-1 loop
|
||||||
for j in 0..right'Length(2) loop
|
c := 0.0;
|
||||||
for k in 0..left'Length(2) loop
|
for j in 0..N-1 loop
|
||||||
Compteur := Compteur + left(i,k) * right(k,j);
|
c := c + left(i)*right(i,j);
|
||||||
end loop;
|
|
||||||
mat(i,j) := Compteur;
|
|
||||||
end loop;
|
end loop;
|
||||||
|
vec(i) := c;
|
||||||
end loop;
|
end loop;
|
||||||
return mat;
|
return vec;
|
||||||
end "*";
|
end "*";
|
||||||
|
|
||||||
function "+"(left, right: T_Google) return T_Google is
|
function "*"(left: T_Element; right: T_Google) return T_Google is
|
||||||
mat: T_Google;
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
initialize(mat);
|
initialize(mat);
|
||||||
for i in 0..left'Length(1) loop
|
for i in 0..N-1 loop
|
||||||
for j in 0..left'Length(2) loop
|
for j in 0..N-1 loop
|
||||||
mat(i,j) := left(i,j) + right(i,j);
|
mat(i,j) := left*right(i,j);
|
||||||
end loop;
|
|
||||||
end loop;
|
|
||||||
return mat;
|
|
||||||
end "+";
|
|
||||||
|
|
||||||
function "*"(left: T_Google; right: T_Element) return T_Google is
|
|
||||||
mat: T_Google;
|
|
||||||
begin
|
|
||||||
initialize(mat);
|
|
||||||
for i in 0..left'Length(1) loop
|
|
||||||
for j in 0..left'Length(2) loop
|
|
||||||
mat(i,j) := right*left(i,j);
|
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
return mat;
|
return mat;
|
||||||
|
@ -44,8 +31,8 @@ package body Google_Naive is
|
||||||
mat: T_Google;
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
initialize(mat);
|
initialize(mat);
|
||||||
for i in 0..left'Length(1) loop
|
for i in 0..N-1 loop
|
||||||
for j in 0..left'Length(2) loop
|
for j in 0..N-1 loop
|
||||||
mat(i,j) := left(i,j)/right;
|
mat(i,j) := left(i,j)/right;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
|
@ -54,19 +41,26 @@ package body Google_Naive is
|
||||||
|
|
||||||
procedure initialize(mat: in out T_Google) is
|
procedure initialize(mat: in out T_Google) is
|
||||||
begin
|
begin
|
||||||
for i in 1..mat'Length(1) loop
|
for i in 0..N-1 loop
|
||||||
for j in 1..mat'Length(2) loop
|
for j in 0..N-1 loop
|
||||||
mat(i,j) := 0.0;
|
mat(i,j) := 0.0;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
end initialize;
|
end initialize;
|
||||||
|
|
||||||
function ones(rows, cols: Positive) return T_Google is
|
procedure initialize(vec: in out T_Vecteur) is
|
||||||
|
begin
|
||||||
|
for i in 0..N-1 loop
|
||||||
|
vec(i) := 0.0;
|
||||||
|
end loop;
|
||||||
|
end initialize;
|
||||||
|
|
||||||
|
function ones return T_Google is
|
||||||
mat: T_Google;
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
initialize(mat);
|
initialize(mat);
|
||||||
for i in 1..mat'Length(1) loop
|
for i in 1..N-1 loop
|
||||||
for j in 1..mat'Length(2) loop
|
for j in 1..N-1 loop
|
||||||
mat(i,j) := 1.0;
|
mat(i,j) := 1.0;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
|
|
|
@ -1,36 +1,28 @@
|
||||||
generic
|
generic
|
||||||
|
|
||||||
type T_Element is digits <>;
|
type T_Element is digits <>;
|
||||||
nb_rows: Positive;
|
N: Positive;
|
||||||
nb_cols: Positive;
|
|
||||||
|
|
||||||
package Google_Naive is
|
package Google_Naive is
|
||||||
|
|
||||||
type T_Google is limited private;
|
type T_Google is limited private;
|
||||||
|
type T_Vecteur is limited private;
|
||||||
|
|
||||||
function "*"(left, right: T_Google) return T_Google with
|
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur;
|
||||||
Pre => left'Length(2) = right'Length(1),
|
function "*"(left: T_Element; right: T_Google) return T_Google;
|
||||||
Post => "*"'Result'Length(1) = left'Length(1)
|
function "/"(left: T_Google; right: T_Element) return T_Google with
|
||||||
and "*"'Result'Length(2) = right'Length(2);
|
|
||||||
|
|
||||||
function "+"(left, right: T_Google) return T_Google with
|
|
||||||
Pre => left'Length(1) = right'Length(1)
|
|
||||||
and left'Length(2) = right'Length(2);
|
|
||||||
|
|
||||||
function "*"(left: in T_Google; right: T_Element) return T_Google;
|
|
||||||
function "/"(left: in T_Google; right: T_Element) return T_Google with
|
|
||||||
Pre => right /= 0.0;
|
Pre => right /= 0.0;
|
||||||
|
|
||||||
procedure initialize(mat: in out T_Google);
|
procedure initialize(mat: in out T_Google);
|
||||||
|
procedure initialize(vec: in out T_Vecteur);
|
||||||
|
|
||||||
function ones(rows, cols: Positive) return T_Google;
|
function ones return T_Google;
|
||||||
|
|
||||||
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element);
|
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element);
|
||||||
|
|
||||||
--function transpose(mat: in T_Google) return T_Google;
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
type T_Google is array (0..nb_rows-1, 0..nb_cols-1) of T_Element;
|
type T_Google is array (0..N-1, 0..N-1) of T_Element;
|
||||||
|
type T_Vecteur is array (0..N-1) of T_Element;
|
||||||
|
|
||||||
end Google_Naive;
|
end Google_Naive;
|
||||||
|
|
109
src/pageRank.adb
109
src/pageRank.adb
|
@ -1,22 +1,23 @@
|
||||||
with Ada.Text_IO; use Ada.Text_IO;
|
with Ada.Text_IO; use Ada.Text_IO;
|
||||||
with Ada.Command_Line; use Ada.Command_Line;
|
with Ada.Command_Line; use Ada.Command_Line;
|
||||||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||||
|
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
|
||||||
|
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||||
with Google_Naive;
|
with Google_Naive;
|
||||||
|
|
||||||
procedure pageRank is
|
procedure pageRank is
|
||||||
|
|
||||||
Type T_Double is digits 6; -- mettre la précision en générique au lieu de T_Element ?
|
Type T_Double is digits 6;
|
||||||
|
|
||||||
ERROR_args: Exception;
|
ERROR_args: Exception;
|
||||||
|
|
||||||
procedure get_args(filename: in out Unbounded_String; -- remplaçable par un String ?
|
procedure get_args(filename: in out Unbounded_String;
|
||||||
ite_max: in out Natural;
|
ite_max: in out Natural;
|
||||||
alpha: in out Float;
|
alpha: in out Float;
|
||||||
naif: in out Boolean) is
|
naif: in out Boolean) is
|
||||||
i: Natural := 0;
|
i: Natural := 0;
|
||||||
begin
|
begin
|
||||||
-- on vérifie d'abord que le nombre d'arguments est cohérent
|
-- on vérifie d'abord que le nombre d'arguments est cohérent
|
||||||
if not(0 < Argument_Count <= 6) then -- verif si double inégalité possible
|
if not(0 < Argument_Count and Argument_Count <= 6) then -- verif si double inégalité possible
|
||||||
raise ERROR_args;
|
raise ERROR_args;
|
||||||
else
|
else
|
||||||
loop
|
loop
|
||||||
|
@ -46,30 +47,61 @@ procedure pageRank is
|
||||||
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
|
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
|
||||||
end get_args;
|
end get_args;
|
||||||
|
|
||||||
function row_sum(row: T_Row) return Natural is
|
procedure algorithm(N: in Positive ; file: in Ada.Text_IO.File_Type ; alpha: in T_Double ; ite_max: in Natural) is
|
||||||
s: Natural := 0;
|
|
||||||
|
package Google is
|
||||||
|
new Google_Naive(T_Element => T_Double, N => N);
|
||||||
|
use Google;
|
||||||
|
|
||||||
|
pi, pi_last: T_Vecteur;
|
||||||
|
G: T_Google;
|
||||||
|
sum: T_Double;
|
||||||
|
row, col: Positive;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
for r in row loop
|
-- on crée H
|
||||||
s := s + r;
|
while not end_of_File(file) loop
|
||||||
|
get(file, row);
|
||||||
|
get(file, col);
|
||||||
|
insert(G, row, col, 1.0);
|
||||||
end loop;
|
end loop;
|
||||||
return s;
|
|
||||||
end row_sum;
|
-- on crée S
|
||||||
|
for i in 0..N-1 loop
|
||||||
|
sum := 0.0;
|
||||||
|
for j in 0..N-1 loop
|
||||||
|
sum := sum + G(i,j);
|
||||||
|
end loop;
|
||||||
|
if sum /= 0.0 then
|
||||||
|
for j in 0..N-1 loop
|
||||||
|
G(i,j) := 1.0/sum;
|
||||||
|
end loop;
|
||||||
|
else
|
||||||
|
for j in 0..N-1 loop
|
||||||
|
G(i,j) := 1/N;
|
||||||
|
end loop;
|
||||||
|
end if;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
-- on crée G
|
||||||
|
G := alpha * G + (1-alpha)/N * ones;
|
||||||
|
|
||||||
|
-- on applique l'algorithme itératif
|
||||||
|
for i in 1..ite_max loop
|
||||||
|
pi := pi * G;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
end algorithm;
|
||||||
|
|
||||||
filename: Unbounded_String;
|
filename: Unbounded_String;
|
||||||
ite_max: Natural := 150;
|
ite_max: Natural := 150;
|
||||||
naif: Boolean := False;
|
naif: Boolean := False;
|
||||||
alpha: Float := 0.85;
|
alpha: Float := 0.85;
|
||||||
|
|
||||||
epsilon: Float := 0.01;
|
N: Positive;
|
||||||
ite: Natural := 0;
|
|
||||||
|
|
||||||
file: Ada.Text_IO.File_Type;
|
file: Ada.Text_IO.File_Type;
|
||||||
|
|
||||||
N: Positive;
|
|
||||||
pi, pi_last: T_Google; -- même T_Google que G ? psk vecteur /= matrice
|
|
||||||
G: T_Google;
|
|
||||||
row, col: Natural;
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
get_args(filename, alpha, ite_max, naif);
|
get_args(filename, alpha, ite_max, naif);
|
||||||
|
@ -77,48 +109,7 @@ begin
|
||||||
open(file, In_File, filename & ".net"); -- verif si la concaténation fonctionne
|
open(file, In_File, filename & ".net"); -- verif si la concaténation fonctionne
|
||||||
get(file, N); -- on récupère le nombre de pages
|
get(file, N); -- on récupère le nombre de pages
|
||||||
|
|
||||||
-- on peut maintenant utiliser le module générique Google_Naive
|
algorithm(N, file, alpha, ite_max);
|
||||||
-- je sais pas si on peut utiliser deux fois le module, à voir
|
|
||||||
package Matrice is
|
|
||||||
new Google_Naive(T_Element => T_Double,
|
|
||||||
nb_rows => N,
|
|
||||||
nb_cols => N);
|
|
||||||
use Matrice;
|
|
||||||
package Vecteur is
|
|
||||||
new Google_Naive(T_Element => T_Double,
|
|
||||||
nb_rows => 1,
|
|
||||||
nb_cols => N);
|
|
||||||
use Vecteur;
|
|
||||||
|
|
||||||
-- on crée H
|
|
||||||
while not end_of_File(file) loop
|
|
||||||
get(file, row);
|
|
||||||
get(file, col);
|
|
||||||
insert(G, row, col, 1.0);
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
-- on crée S
|
|
||||||
-- horrible, voir si simplifiable
|
|
||||||
for row in G.matrix loop
|
|
||||||
p = row_sum(row);
|
|
||||||
for j in row loop
|
|
||||||
if p = 0 then
|
|
||||||
row(j) := 1/N;
|
|
||||||
else
|
|
||||||
if row(j) /= 0 then -- TODO: changer T_Element générique en PRECISION
|
|
||||||
row(j) := 1/p;
|
|
||||||
end if;
|
|
||||||
end if;
|
|
||||||
end loop;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
-- on crée G
|
|
||||||
G := G*alpha + ones(N, N)*(1-alpha)/N;
|
|
||||||
|
|
||||||
-- on applique l'algorithme itératif
|
|
||||||
for i in 1..ite_max loop
|
|
||||||
pi := pi * G;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
-- write_to_files(filename, ...); -- TODO
|
-- write_to_files(filename, ...); -- TODO
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue