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

116 lines
3.6 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 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;
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;
begin
-- 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
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;
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
-- 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;
-- 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;
N: Positive;
file: Ada.Text_IO.File_Type;
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
algorithm(N, file, alpha, ite_max);
-- write_to_files(filename, ...); -- TODO
end pageRank;