2020-12-05 15:20:01 +00:00
|
|
|
with Ada.Text_IO; use Ada.Text_IO;
|
|
|
|
with Ada.Command_Line; use Ada.Command_Line;
|
|
|
|
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
2020-12-07 22:03:27 +00:00
|
|
|
with Google_Naive;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-03 18:56:35 +00:00
|
|
|
procedure pageRank is
|
2020-12-05 18:18:53 +00:00
|
|
|
|
|
|
|
Type T_Double is digits 6; -- mettre la précision en générique au lieu de T_Element ?
|
2020-12-03 18:56:35 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
ERROR_args: Exception;
|
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
procedure get_args(filename: in out Unbounded_String; -- remplaçable par un String ?
|
|
|
|
ite_max: in out Natural;
|
2020-12-05 15:20:01 +00:00
|
|
|
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
|
|
|
|
raise ERROR_args;
|
|
|
|
else
|
|
|
|
loop
|
|
|
|
-- put(Argument(i));
|
|
|
|
if Argument(i) = "-P" then
|
|
|
|
naif := True;
|
|
|
|
i := i + 1;
|
|
|
|
elsif Argument(i) = "-A" then
|
|
|
|
alpha := Float'Value(Argument(i+1));
|
|
|
|
i := i + 2;
|
|
|
|
elsif Argument(i) = "-I" then
|
2020-12-05 18:18:53 +00:00
|
|
|
ite_max := Natural'Value(Argument(i+1)); -- verif si les conversions sont bonnes
|
2020-12-05 15:20:01 +00:00
|
|
|
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));
|
|
|
|
i := i + 1;
|
|
|
|
else
|
|
|
|
raise ERROR_args;
|
|
|
|
end if;
|
|
|
|
exit when i > Argument_Count;
|
|
|
|
end loop;
|
|
|
|
end if;
|
|
|
|
exception
|
|
|
|
-- s'il y a un problème on arrête l'exectution 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");
|
2020-12-05 18:18:53 +00:00
|
|
|
end get_args;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
function row_sum(row: T_Row) return Natural is
|
|
|
|
s: Natural := 0;
|
|
|
|
begin
|
|
|
|
for r in row loop
|
|
|
|
s := s + r;
|
2020-12-05 15:20:01 +00:00
|
|
|
end loop;
|
2020-12-05 18:18:53 +00:00
|
|
|
return s;
|
|
|
|
end row_sum;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
|
|
|
filename: Unbounded_String;
|
2020-12-05 18:18:53 +00:00
|
|
|
ite_max: Natural := 150;
|
2020-12-05 15:20:01 +00:00
|
|
|
naif: Boolean := False;
|
|
|
|
alpha: Float := 0.85;
|
|
|
|
|
|
|
|
epsilon: Float := 0.01;
|
2020-12-05 18:18:53 +00:00
|
|
|
ite: Natural := 0;
|
|
|
|
|
|
|
|
file: Ada.Text_IO.File_Type;
|
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
N: Positive;
|
2020-12-05 18:25:09 +00:00
|
|
|
pi, pi_last: T_Google; -- même T_Google que G ? psk vecteur /= matrice
|
|
|
|
G: T_Google;
|
2020-12-05 18:18:53 +00:00
|
|
|
row, col: Natural;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-03 18:56:35 +00:00
|
|
|
begin
|
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
get_args(filename, alpha, ite_max, naif);
|
|
|
|
|
|
|
|
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);
|
2020-12-05 18:25:09 +00:00
|
|
|
insert(G, row, col, 1.0);
|
2020-12-05 18:18:53 +00:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- on crée S
|
|
|
|
-- horrible, voir si simplifiable
|
2020-12-05 18:25:09 +00:00
|
|
|
for row in G.matrix loop
|
2020-12-05 18:18:53 +00:00
|
|
|
p = row_sum(row);
|
|
|
|
for j in row loop
|
|
|
|
if p = 0 then
|
|
|
|
row(j) := 1/N;
|
|
|
|
else
|
2020-12-05 18:25:09 +00:00
|
|
|
if row(j) /= 0 then -- TODO: changer T_Element générique en PRECISION
|
2020-12-05 18:18:53 +00:00
|
|
|
row(j) := 1/p;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end loop;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- on crée G
|
2020-12-05 18:25:09 +00:00
|
|
|
G := G*alpha + ones(N, N)*(1-alpha)/N;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
-- on applique l'algorithme itératif
|
2020-12-07 22:03:27 +00:00
|
|
|
for i in 1..ite_max loop
|
2020-12-05 18:18:53 +00:00
|
|
|
pi := pi * G;
|
2020-12-05 15:20:01 +00:00
|
|
|
end loop;
|
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
-- write_to_files(filename, ...); -- TODO
|
2020-12-03 18:56:35 +00:00
|
|
|
|
|
|
|
end pageRank;
|