pagerank presque fonctionnel

git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@207849 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
lfainsin 2020-12-09 17:43:46 +00:00
parent 8c095b3b86
commit 561e1ac164
3 changed files with 125 additions and 47 deletions

View file

@ -15,13 +15,13 @@ package body Google_Naive is
return vec;
end "*";
function "*"(left: T_Element; right: T_Google) return T_Google is
function "*"(left: Float; right: T_Google) return T_Google is
mat: T_Google;
begin
initialize(mat);
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := left*right(i,j);
mat(i,j) := T_Element(left)*right(i,j);
end loop;
end loop;
return mat;
@ -39,6 +39,18 @@ package body Google_Naive is
return mat;
end "/";
function "+"(left, right: T_Google) return T_Google is
mat: T_Google;
begin
initialize(mat);
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := left(i,j) + right(i,j);
end loop;
end loop;
return mat;
end "+";
procedure initialize(mat: in out T_Google) is
begin
for i in 0..N-1 loop
@ -72,4 +84,46 @@ package body Google_Naive is
mat(i,j) := elm;
end insert;
procedure create_H(mat: in out T_Google ; file: in Ada.Text_IO.File_Type) is
row, col: Integer;
begin
while not end_of_File(file) loop
get(file, row);
get(file, col);
insert(mat, row, col, 1.0);
end loop;
end create_H;
procedure create_S(mat: in out T_Google) is
sum: T_Element;
begin
for i in 0..N-1 loop
sum := 0.0;
for j in 0..N-1 loop
sum := sum + mat(i,j);
end loop;
if sum /= 0.0 then
for j in 0..N-1 loop
mat(i,j) := 1.0/sum;
end loop;
else
for j in 0..N-1 loop
mat(i,j) := 1.0 / T_Element(N);
end loop;
end if;
end loop;
end create_S;
procedure create_G(mat: in out T_Google ; alpha: in Float) is
begin
mat := ( alpha * mat ) + ( Float((1.0-alpha)/Float(N)) * ones );
end create_G;
procedure put(vec: in T_Vecteur) is
begin
for i in 0..N-1 loop
put(Float(vec(i))); new_line;
end loop;
end put;
end Google_Naive;

View file

@ -1,3 +1,7 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
generic
type T_Element is digits <>;
@ -5,13 +9,14 @@ generic
package Google_Naive is
type T_Google is limited private;
type T_Vecteur is limited private;
type T_Google is private;
type T_Vecteur is private;
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur;
function "*"(left: T_Element; right: T_Google) return T_Google;
function "*"(left: Float; right: T_Google) return T_Google;
function "/"(left: T_Google; right: T_Element) return T_Google with
Pre => right /= 0.0;
Pre => right /= 0.0;
function "+"(left, right: T_Google) return T_Google;
procedure initialize(mat: in out T_Google);
procedure initialize(vec: in out T_Vecteur);
@ -20,6 +25,11 @@ package Google_Naive is
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element);
procedure create_H(mat: in out T_Google ; file: in Ada.Text_IO.File_Type);
procedure create_S(mat: in out T_Google);
procedure create_G(mat: in out T_Google ; alpha: in Float);
procedure put(vec: in T_Vecteur);
private
type T_Google is array (0..N-1, 0..N-1) of T_Element;

View file

@ -1,8 +1,10 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_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
@ -10,31 +12,48 @@ procedure pageRank is
Type T_Double is digits 6;
ERROR_args: Exception;
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;
i: Natural := 1;
begin
put(Argument_Count, 1); new_line;
for i in 1 .. Argument_Count loop
Put("Argument");
Put(i, 2);
Put(": ");
Put_line(Argument(i));
end loop;
-- on vérifie d'abord que le nombre d'arguments est cohérent
if not(0 < Argument_Count and Argument_Count <= 6) then -- verif si double inégalité possible
raise ERROR_args;
else
loop
-- put(Argument(i));
if Argument(i) = "-P" then
put_line("naif");
naif := True;
i := i + 1;
elsif Argument(i) = "-A" then
put_line("alpha");
alpha := Float'Value(Argument(i+1));
i := i + 2;
elsif Argument(i) = "-I" then
put_line("iteration");
ite_max := Natural'Value(Argument(i+1)); -- verif si les conversions sont bonnes
i := i + 2;
elsif Argument(i)(Argument(i)'Last-4 .. Argument(i)'Last) = ".net" then -- verif les indexs
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-5));
elsif Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then -- verif les indexs
put_line("filename");
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-4));
i := i + 1;
else
put_line("erreur");
raise ERROR_args;
end if;
exit when i > Argument_Count;
@ -47,51 +66,44 @@ procedure pageRank is
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
end get_args;
procedure algorithm(N: in Positive ; file: in Ada.Text_IO.File_Type ; alpha: in T_Double ; ite_max: in Natural) is
procedure algorithm_naif(N: in Positive;
file: in Ada.Text_IO.File_Type;
alpha: in Float;
ite_max: in Natural) is
package Google is
new Google_Naive(T_Element => T_Double, N => N);
use Google;
pi, pi_last: T_Vecteur;
pi: T_Vecteur;
G: T_Google;
sum: T_Double;
row, col: Positive;
begin
-- 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
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;
put_line("initialized pi");
initialize(pi);
-- on crée G
G := alpha * G + (1-alpha)/N * ones;
put_line("start algo naif");
create_H(G, file);
put_line("created H");
create_S(G);
put_line("created S");
create_G(G, alpha);
put_line("created G");
-- on applique l'algorithme itératif
for i in 1..ite_max loop
pi := pi * G;
end loop;
put(pi);
put_line("end algo naif");
end algorithm;
end algorithm_naif;
filename: Unbounded_String;
ite_max: Natural := 150;
@ -104,13 +116,15 @@ procedure pageRank is
begin
get_args(filename, alpha, ite_max, naif);
get_args(filename, ite_max, alpha, naif);
put_line("args OK");
open(file, In_File, To_String(filename & ".net"));
put_line("opened file");
open(file, In_File, filename & ".net"); -- verif si la concaténation fonctionne
get(file, N); -- on récupère le nombre de pages
put_line("got N");
algorithm(N, file, alpha, ite_max);
-- write_to_files(filename, ...); -- TODO
algorithm_naif(N, file, alpha, ite_max);
end pageRank;