pageRank naif fonctionne, ajout de prints de debug

git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@208511 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
lfainsin 2020-12-12 13:05:39 +00:00
parent 561e1ac164
commit f089f32c83
3 changed files with 89 additions and 46 deletions

View file

@ -8,7 +8,7 @@ package body Google_Naive is
for i in 0..N-1 loop
c := 0.0;
for j in 0..N-1 loop
c := c + left(i)*right(i,j);
c := c + left(j)*right(j,i);
end loop;
vec(i) := c;
end loop;
@ -27,13 +27,13 @@ package body Google_Naive is
return mat;
end "*";
function "/"(left: T_Google; right: T_Element) return T_Google is
function "/"(left: T_Google; right: Float) 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;
mat(i,j) := left(i,j)/T_Element(right);
end loop;
end loop;
return mat;
@ -63,7 +63,7 @@ package body Google_Naive is
procedure initialize(vec: in out T_Vecteur) is
begin
for i in 0..N-1 loop
vec(i) := 0.0;
vec(i) := 1.0/T_Element(N);
end loop;
end initialize;
@ -71,8 +71,8 @@ package body Google_Naive is
mat: T_Google;
begin
initialize(mat);
for i in 1..N-1 loop
for j in 1..N-1 loop
for i in 0..N-1 loop
for j in 0..N-1 loop
mat(i,j) := 1.0;
end loop;
end loop;
@ -98,25 +98,27 @@ package body Google_Naive 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;
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
if mat(i,j) /= 0.0 then
mat(i,j) := 1.0/sum;
end if;
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 );
mat := alpha*mat + (1.0-alpha)/Float(N)*ones;
end create_G;
procedure put(vec: in T_Vecteur) is
@ -126,4 +128,14 @@ package body Google_Naive is
end loop;
end put;
procedure put(mat: in T_Google) is
begin
for i in 0..N-1 loop
for j in 0..N-1 loop
put(Float(mat(i,j)));
end loop;
new_line;
end loop;
end put;
end Google_Naive;

View file

@ -12,10 +12,9 @@ package Google_Naive is
type T_Google is private;
type T_Vecteur is private;
function "*"(left : T_Vecteur ; right : T_Google) return T_Vecteur;
function "*"(left: T_Vecteur ; right: T_Google) return T_Vecteur;
function "*"(left: Float; right: T_Google) return T_Google;
function "/"(left: T_Google; right: T_Element) return T_Google with
Pre => right /= 0.0;
function "/"(left: T_Google; right: Float) return T_Google with Pre => right /= 0.0;
function "+"(left, right: T_Google) return T_Google;
procedure initialize(mat: in out T_Google);
@ -30,6 +29,7 @@ package Google_Naive is
procedure create_G(mat: in out T_Google ; alpha: in Float);
procedure put(vec: in T_Vecteur);
procedure put(mat: in T_Google);
private
type T_Google is array (0..N-1, 0..N-1) of T_Element;

View file

@ -1,6 +1,6 @@
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;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
@ -23,44 +23,56 @@ procedure pageRank is
i: Natural := 1;
begin
put(Argument_Count, 1); new_line;
put("Argument_Count = "); put(Argument_Count, 1); new_line;
for i in 1 .. Argument_Count loop
Put("Argument");
Put(i, 2);
Put(": ");
Put("Argument(");
Put(i, 0);
Put(") = ");
Put_line(Argument(i));
end loop;
new_line;
-- 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
if not(0 < Argument_Count and Argument_Count <= 6) then
raise ERROR_args;
else
else -- sinon on parse les arguments
loop
if Argument(i) = "-P" then
put_line("naif");
naif := True;
i := i + 1;
put_line("parsed naif");
elsif Argument(i) = "-A" then
put_line("alpha");
alpha := Float'Value(Argument(i+1));
i := i + 2;
put_line("parsed alpha");
elsif Argument(i) = "-I" then
put_line("iteration");
ite_max := Natural'Value(Argument(i+1)); -- verif si les conversions sont bonnes
ite_max := Natural'Value(Argument(i+1));
i := i + 2;
elsif Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then -- verif les indexs
put_line("filename");
put_line("parsed ite_max");
elsif Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-4));
i := i + 1;
put_line("parsed filename");
else
put_line("erreur");
put_line("unexpected passing case");
raise ERROR_args;
end if;
exit when i > Argument_Count;
end loop;
end if;
new_line;
put("alpha = "); put(alpha, 1); new_line;
put("naif = "); put(Boolean'Pos(naif), 1); new_line;
put("ite_max = "); put(Float(ite_max), 1); new_line;
put("filename = "); put_line(To_String(filename));
new_line;
exception
-- s'il y a un problème on arrête l'exectution et on affiche l'usage.
-- s'il y a un problème on arrête l'execution et on affiche l'usage.
when others =>
put_line("Erreur lors de la saisi de la commande");
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
@ -84,24 +96,36 @@ procedure pageRank is
begin
put_line("initialized pi");
initialize(pi);
put_line("initialized pi");
--put(pi); new_line;
put_line("start algo naif");
initialize(G);
put_line("initialized G");
--put(G); new_line;
create_H(G, file);
put_line("created H");
--put(G); new_line;
create_S(G);
put_line("created S");
--put(G); new_line;
create_G(G, alpha);
put_line("created G");
--put(G); new_line;
-- on applique l'algorithme itératif
put("ite: ");
for i in 1..ite_max loop
pi := pi * G;
end loop;
put(i, 1); put(" ");
end loop; new_line;
put(pi);
put_line("end algo naif");
--new_line;
--put_line("final pi:");
--put(pi); new_line;
end algorithm_naif;
@ -111,7 +135,6 @@ procedure pageRank is
alpha: Float := 0.85;
N: Positive;
file: Ada.Text_IO.File_Type;
begin
@ -120,11 +143,19 @@ begin
put_line("args OK");
open(file, In_File, To_String(filename & ".net"));
put_line("opened file");
put_line("file OK");
get(file, N); -- on récupère le nombre de pages
put_line("got N");
put("N = "); put(N, 1); new_line;
new_line;
algorithm_naif(N, file, alpha, ite_max);
new_line;
--sort_with_index(pi, pi_sorted, pi_index);
--write-to_file(filename);
end pageRank;