projet-programmation-impera.../src/pageRank.adb

125 lines
3.9 KiB
Ada
Raw Normal View History

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 Google_Naive;
procedure pageRank is
Type T_Double is digits 6; -- mettre la précision en générique au lieu de T_Element ?
ERROR_args: Exception;
procedure get_args(filename: in out Unbounded_String; -- remplaçable par un 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
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
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));
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");
end get_args;
function row_sum(row: T_Row) return Natural is
s: Natural := 0;
begin
for r in row loop
s := s + r;
end loop;
return s;
end row_sum;
filename: Unbounded_String;
ite_max: Natural := 150;
naif: Boolean := False;
alpha: Float := 0.85;
epsilon: Float := 0.01;
ite: Natural := 0;
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);
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;
-- write_to_files(filename, ...); -- TODO
end pageRank;