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

116 lines
3.4 KiB
Ada
Raw Normal View History

with SDA_Exceptions; use SDA_Exceptions;
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 googleNaive;
procedure pageRank is
ERROR_args: Exception;
procedure getArgs(filename: in out Unbounded_String;
iteMAX: 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
iteMAX := 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 getArgs;
procedure createG_naive(G: in out T_Google) is
File : Ada.Text_IO.File_Type;
N: Natural;
H, S: T_Google;
row, col: Natural;
function rowSum(row: T_Row) return Natural is
s: Natural := 0;
begin
for r in row loop
s := s + r;
end loop;
return s;
end rowSum;
begin
open(File, In_File, filename & ".net");
get(File, N); -- on récupère le nombre de pages
-- on créé H
while not end_of_File(File) loop
get(File, row);
get(File, col);
insert(H, row, col, 1.0);
end loop;
-- on créé S
-- TODO: simplifier tout ça avec l'opérateur "/" sur T_row
for row in H loop
p = rowSum(row);
for j in row loop
if p = 0 then
row(j) := 1/N;
else
if row(j) /= 0 then
row(j) := 1/p;
end if;
end if;
end loop;
end loop;
G := S*alpha + ones(N, N)*(1-alpha)/N;
end createG_naive;
filename: Unbounded_String;
iteMAX: Natural := 150;
naif: Boolean := False;
alpha: Float := 0.85;
epsilon: Float := 0.01;
pi: T_Google;
G: T_Google;
N: Positive;
begin
getArgs(filename, alpha, iteMAX, naif);
initialize(pi, 1, N);
initialize(G, N, N);
createG_naive(G);
for i in 1..N loop
pi := pi * G;
end loop;
-- write2file(filename, pi); à implémenter
end pageRank;