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:
lfainsin 2020-12-09 14:43:16 +00:00
parent 647d7d8730
commit 8c095b3b86
3 changed files with 87 additions and 110 deletions

View file

@ -1,40 +1,27 @@
package body Google_Naive is
function "*"(left, right: T_Google) return T_Google is
mat: T_Google;
Compteur: T_Element := 0.0;
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur is
vec: T_Vecteur;
c: T_Element;
begin
initialize(mat);
for i in 0..left'Length(1) loop
for j in 0..right'Length(2) loop
for k in 0..left'Length(2) loop
Compteur := Compteur + left(i,k) * right(k,j);
end loop;
mat(i,j) := Compteur;
initialize(vec);
for i in 0..N-1 loop
c := 0.0;
for j in 0..N-1 loop
c := c + left(i)*right(i,j);
end loop;
vec(i) := c;
end loop;
return mat;
return vec;
end "*";
function "+"(left, right: T_Google) return T_Google is
function "*"(left: T_Element; right: T_Google) 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) := left(i,j) + 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);
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := left*right(i,j);
end loop;
end loop;
return mat;
@ -44,8 +31,8 @@ package body Google_Naive is
mat: T_Google;
begin
initialize(mat);
for i in 0..left'Length(1) loop
for j in 0..left'Length(2) loop
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := left(i,j)/right;
end loop;
end loop;
@ -54,19 +41,26 @@ package body Google_Naive is
procedure initialize(mat: in out T_Google) is
begin
for i in 1..mat'Length(1) loop
for j in 1..mat'Length(2) loop
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := 0.0;
end loop;
end loop;
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;
begin
initialize(mat);
for i in 1..mat'Length(1) loop
for j in 1..mat'Length(2) loop
for i in 1..N-1 loop
for j in 1..N-1 loop
mat(i,j) := 1.0;
end loop;
end loop;

View file

@ -1,36 +1,28 @@
generic
type T_Element is digits <>;
nb_rows: Positive;
nb_cols: Positive;
N: Positive;
package Google_Naive is
type T_Google is limited private;
type T_Vecteur is limited private;
function "*"(left, right: T_Google) return T_Google with
Pre => left'Length(2) = right'Length(1),
Post => "*"'Result'Length(1) = left'Length(1)
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
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur;
function "*"(left: T_Element; right: T_Google) return T_Google;
function "/"(left: T_Google; right: T_Element) return T_Google with
Pre => right /= 0.0;
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);
--function transpose(mat: in T_Google) return T_Google;
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;

View file

@ -1,22 +1,23 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
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;
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;
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;
alpha: in out Float;
naif: in out Boolean) is
i: Natural := 0;
begin
-- 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;
else
loop
@ -46,30 +47,61 @@ procedure pageRank is
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
end get_args;
function row_sum(row: T_Row) return Natural is
s: Natural := 0;
procedure algorithm(N: in Positive ; file: in Ada.Text_IO.File_Type ; alpha: in T_Double ; ite_max: in Natural) is
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
for r in row loop
s := s + r;
-- 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;
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;
ite_max: Natural := 150;
naif: Boolean := False;
alpha: Float := 0.85;
epsilon: Float := 0.01;
ite: Natural := 0;
N: Positive;
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
get_args(filename, alpha, ite_max, naif);
@ -77,48 +109,7 @@ begin
open(file, In_File, filename & ".net"); -- verif si la concaténation fonctionne
get(file, N); -- on récupère le nombre de pages
-- on peut maintenant utiliser le module générique Google_Naive
-- 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;
algorithm(N, file, alpha, ite_max);
-- write_to_files(filename, ...); -- TODO