2020-12-09 17:43:46 +00:00
|
|
|
with Ada.Text_IO; use Ada.Text_IO;
|
|
|
|
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
2020-12-16 20:33:42 +00:00
|
|
|
with Ada.Command_Line; use Ada.Command_Line;
|
2020-12-05 15:20:01 +00:00
|
|
|
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
2020-12-22 13:26:25 +00:00
|
|
|
with Ada.Text_IO.Text_Streams;
|
2020-12-09 17:43:46 +00:00
|
|
|
|
2020-12-19 15:08:54 +00:00
|
|
|
with Vector;
|
2020-12-19 15:55:41 +00:00
|
|
|
with Google_Naive;
|
|
|
|
with Google_Creux;
|
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
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
-- défition d'exception pour gérer le parsing des arguments.
|
2020-12-05 15:20:01 +00:00
|
|
|
ERROR_args: Exception;
|
2020-12-22 13:26:25 +00:00
|
|
|
ERROR_alpha: Exception;
|
|
|
|
ERROR_ite: Exception;
|
2020-12-23 14:40:04 +00:00
|
|
|
ERROR_filename: Exception;
|
|
|
|
INFO_tips: Exception;
|
2020-12-22 13:26:25 +00:00
|
|
|
INFO_help: Exception;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-19 15:55:41 +00:00
|
|
|
-- définition du type T_Double
|
2020-12-22 13:26:25 +00:00
|
|
|
Type T_Double is digits 18;
|
2020-12-19 15:55:41 +00:00
|
|
|
-- on utilise le module générique Float_IO pour pouvoir afficher T_Double directement
|
2020-12-16 20:33:42 +00:00
|
|
|
package Text_T_Double is
|
|
|
|
new Ada.Text_IO.Float_IO(Num => T_Double);
|
|
|
|
use Text_T_Double;
|
2020-12-09 17:43:46 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
stdout: constant Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output;
|
|
|
|
|
2020-12-19 15:55:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
|
|
|
|
-- procédure pour récupérer les arguments de la ligne de commande
|
2020-12-09 14:43:16 +00:00
|
|
|
procedure get_args(filename: in out Unbounded_String;
|
2020-12-16 20:33:42 +00:00
|
|
|
ite_max: in out Natural;
|
|
|
|
alpha: in out T_Double;
|
|
|
|
naif: in out Boolean) is
|
2020-12-09 17:43:46 +00:00
|
|
|
i: Natural := 1;
|
2020-12-05 15:20:01 +00:00
|
|
|
begin
|
2020-12-16 20:33:42 +00:00
|
|
|
|
|
|
|
-- on affiche tous les arguments
|
2020-12-12 13:05:39 +00:00
|
|
|
put("Argument_Count = "); put(Argument_Count, 1); new_line;
|
2020-12-19 15:55:41 +00:00
|
|
|
for i in 1..Argument_Count loop
|
2020-12-12 13:05:39 +00:00
|
|
|
Put("Argument(");
|
|
|
|
Put(i, 0);
|
|
|
|
Put(") = ");
|
2020-12-09 17:43:46 +00:00
|
|
|
Put_line(Argument(i));
|
|
|
|
end loop;
|
2020-12-12 13:05:39 +00:00
|
|
|
|
|
|
|
new_line;
|
2020-12-09 17:43:46 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
-- on vérifie d'abord que le nombre d'arguments est cohérent
|
2020-12-22 13:26:25 +00:00
|
|
|
if not(0 < Argument_Count and Argument_Count <= 7) then
|
2020-12-05 15:20:01 +00:00
|
|
|
raise ERROR_args;
|
2020-12-12 13:05:39 +00:00
|
|
|
else -- sinon on parse les arguments
|
2020-12-05 15:20:01 +00:00
|
|
|
loop
|
2020-12-22 13:26:25 +00:00
|
|
|
if Argument(i) = "-h" or Argument(i) = "--help" then
|
|
|
|
put_line("parsed help");
|
|
|
|
raise INFO_help;
|
|
|
|
|
|
|
|
elsif Argument(i) = "-n" or Argument(i) = "--naif" then
|
2020-12-05 15:20:01 +00:00
|
|
|
naif := True;
|
|
|
|
i := i + 1;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed naif");
|
2020-12-19 15:55:41 +00:00
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
elsif Argument(i) = "-a" or Argument(i) = "--alpha" then
|
2020-12-16 20:33:42 +00:00
|
|
|
alpha := T_Double'Value(Argument(i+1));
|
2020-12-22 13:26:25 +00:00
|
|
|
if alpha < 0.0 or alpha > 1.0 then
|
|
|
|
raise ERROR_alpha;
|
|
|
|
end if;
|
2020-12-05 15:20:01 +00:00
|
|
|
i := i + 2;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed alpha");
|
2020-12-19 15:55:41 +00:00
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
elsif Argument(i) = "-i" or Argument(i) = "--ite-max" then
|
2020-12-12 13:05:39 +00:00
|
|
|
ite_max := Natural'Value(Argument(i+1));
|
2020-12-22 13:26:25 +00:00
|
|
|
if ite_max > 150 then
|
|
|
|
raise ERROR_ite;
|
|
|
|
end if;
|
2020-12-05 15:20:01 +00:00
|
|
|
i := i + 2;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed ite_max");
|
2020-12-19 15:55:41 +00:00
|
|
|
|
2020-12-23 14:40:04 +00:00
|
|
|
elsif Argument(i)'Length > 3 and then Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then
|
2020-12-09 17:43:46 +00:00
|
|
|
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-4));
|
2020-12-05 15:20:01 +00:00
|
|
|
i := i + 1;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed filename");
|
2020-12-19 15:55:41 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
else
|
2020-12-22 13:26:25 +00:00
|
|
|
new_line;
|
2020-12-23 14:40:04 +00:00
|
|
|
put("Argument: '");
|
2020-12-22 13:26:25 +00:00
|
|
|
put(Argument(i));
|
|
|
|
put("' non reconnu");
|
|
|
|
new_line;
|
2020-12-23 14:40:04 +00:00
|
|
|
raise INFO_tips;
|
2020-12-19 15:55:41 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
end if;
|
|
|
|
exit when i > Argument_Count;
|
2020-12-23 14:40:04 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
end loop;
|
2020-12-23 14:40:04 +00:00
|
|
|
|
|
|
|
if Length(filename) = 0 then
|
|
|
|
raise ERROR_filename;
|
|
|
|
end if;
|
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
end if;
|
2020-12-23 14:40:04 +00:00
|
|
|
|
2020-12-12 13:05:39 +00:00
|
|
|
new_line;
|
2020-12-22 13:26:25 +00:00
|
|
|
put("alpha = "); put(alpha, Fore=>1, Aft=>10); new_line;
|
2020-12-12 13:05:39 +00:00
|
|
|
put("naif = "); put(Boolean'Pos(naif), 1); new_line;
|
2020-12-16 20:33:42 +00:00
|
|
|
put("ite_max = "); put(ite_max, 1); new_line;
|
2020-12-12 13:05:39 +00:00
|
|
|
put("filename = "); put_line(To_String(filename));
|
|
|
|
new_line;
|
2020-12-16 20:33:42 +00:00
|
|
|
|
2020-12-05 15:20:01 +00:00
|
|
|
exception
|
2020-12-22 13:26:25 +00:00
|
|
|
|
|
|
|
when CONSTRAINT_ERROR =>
|
|
|
|
if Argument(i) = "-a" or Argument(i) = "--alpha" then
|
|
|
|
raise ERROR_alpha;
|
|
|
|
elsif Argument(i) = "-i" or Argument(i) = "--ite-max" then
|
|
|
|
raise ERROR_ite;
|
2020-12-23 14:40:04 +00:00
|
|
|
else
|
|
|
|
put_line("Unexpected contrain_error");
|
|
|
|
raise ERROR_args;
|
2020-12-22 13:26:25 +00:00
|
|
|
end if;
|
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
end get_args;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-19 15:55:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
|
2020-12-19 15:55:41 +00:00
|
|
|
-- définition des arguments
|
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;
|
2020-12-16 20:33:42 +00:00
|
|
|
alpha: T_Double := 0.85;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-19 15:55:41 +00:00
|
|
|
-- définition des variables pour créer les matices/vecteurs
|
2020-12-09 14:43:16 +00:00
|
|
|
N: Positive;
|
2020-12-22 13:26:25 +00:00
|
|
|
N_links: Integer := -1;
|
2020-12-05 18:18:53 +00:00
|
|
|
file: Ada.Text_IO.File_Type;
|
|
|
|
|
2020-12-03 18:56:35 +00:00
|
|
|
begin
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on récupère les arguments de la ligne de commande
|
2020-12-09 17:43:46 +00:00
|
|
|
get_args(filename, ite_max, alpha, naif);
|
2020-12-24 16:55:22 +00:00
|
|
|
put_line("parsed successfully arguments");
|
2020-12-05 18:18:53 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on ouvre le fichier .net
|
2020-12-09 17:43:46 +00:00
|
|
|
open(file, In_File, To_String(filename & ".net"));
|
2020-12-24 16:55:22 +00:00
|
|
|
put("opened "); put(To_String(filename & ".net")); new_line;
|
2020-12-05 18:18:53 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
-- on récupère le nombre de pages (N)
|
|
|
|
get(file, N);
|
|
|
|
put("N = "); put(N, 1); new_line;
|
|
|
|
|
|
|
|
-- on récupère le nombre de liens (N_Links)
|
2020-12-22 13:26:25 +00:00
|
|
|
while not End_Of_File(file) loop
|
2020-12-24 16:55:22 +00:00
|
|
|
skip_line(file);
|
2020-12-22 13:26:25 +00:00
|
|
|
N_links := N_links + 1;
|
|
|
|
end loop;
|
|
|
|
put("N_links = "); put(N_links, 1); new_line;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
|
|
|
reset(file, In_File);
|
|
|
|
skip_line(file);
|
2020-12-22 13:26:25 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
-- on peut maintenant créer nos vecteurs
|
|
|
|
declare
|
|
|
|
package Vector_Entier is
|
|
|
|
new Vector.Entier(Capacite => N);
|
|
|
|
package Vector_Double is
|
|
|
|
new Vector.Digit(T_Digit => T_Double,
|
|
|
|
Capacite => N,
|
|
|
|
Vector_Entier => Vector_Entier);
|
|
|
|
package Vector_Link is
|
|
|
|
new Vector.Link(Capacite => N_links);
|
|
|
|
|
|
|
|
use Vector_Double;
|
|
|
|
use Vector_Entier;
|
|
|
|
use Vector_Link;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
network: Vector_Link.T_Vecteur;
|
|
|
|
row, col: Natural;
|
|
|
|
dupe: Natural;
|
|
|
|
|
|
|
|
pi: Vector_Double.T_Vecteur;
|
|
|
|
pi_index: Vector_Entier.T_Vecteur;
|
|
|
|
begin
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
-- on charge le réseau en mémoire
|
|
|
|
for i in 0..N_links-1 loop
|
|
|
|
get(file, row);
|
|
|
|
get(file, col);
|
|
|
|
network(i) := T_Link'(row, col);
|
|
|
|
end loop;
|
|
|
|
close(file);
|
|
|
|
put_line("loaded in memory the network");
|
|
|
|
|
|
|
|
-- on trie le réseau, si besoin
|
|
|
|
if not naif then -- and then not(is_sorted(network)) then
|
|
|
|
quicksort(network);
|
|
|
|
put_line("sorted the network");
|
|
|
|
end if;
|
|
|
|
|
|
|
|
-- on compte le nombre de doublons
|
|
|
|
-- count_dupe(network, dupe);
|
|
|
|
-- TODO
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
|
|
|
|
initialize(pi, 1.0/T_Double(N));
|
|
|
|
put_line("initialized pi");
|
|
|
|
-- put(pi); new_line;
|
|
|
|
|
|
|
|
identity(pi_index);
|
|
|
|
put_line("initialized pi_index to identity");
|
|
|
|
-- put(pi_index); new_line;
|
|
|
|
|
|
|
|
if naif then
|
|
|
|
declare
|
|
|
|
-- on instancie le module générique Naif de Google
|
|
|
|
package Google is
|
|
|
|
new Google_Naive(T_Element => T_Double,
|
|
|
|
N => N,
|
|
|
|
N_links => N_links,
|
|
|
|
Vector_Natural => Vector_Entier,
|
|
|
|
Vector_Element => Vector_Double,
|
|
|
|
Vector_Link => Vector_Link);
|
|
|
|
use Google;
|
|
|
|
|
|
|
|
-- définition de la matrice Google
|
|
|
|
G: T_Google;
|
|
|
|
begin
|
|
|
|
|
|
|
|
initialize(G);
|
|
|
|
put_line("initialized G");
|
|
|
|
-- put(G); new_line;
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
|
|
|
|
create_H(G, network);
|
|
|
|
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;
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
|
|
|
|
-- on applique l'algorithme itératif
|
|
|
|
for i in 1..ite_max loop
|
|
|
|
pi := pi * G;
|
|
|
|
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
|
|
|
|
ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max));
|
|
|
|
end loop; new_line;
|
|
|
|
|
|
|
|
-- new_line;
|
|
|
|
-- put_line("final pi:");
|
|
|
|
-- put(pi); new_line;
|
|
|
|
end;
|
|
|
|
else -- not naif
|
|
|
|
declare
|
|
|
|
-- on instancie le module générique Creux de Google
|
|
|
|
package Google is
|
|
|
|
new Google_Creux(T_Element => T_Double,
|
|
|
|
N => N,
|
|
|
|
N_links => N_links,
|
|
|
|
Vector_Natural => Vector_Entier,
|
|
|
|
Vector_Element => Vector_Double,
|
|
|
|
Vector_Link => Vector_Link);
|
|
|
|
use Google;
|
|
|
|
|
|
|
|
-- définition de la matrice Google
|
|
|
|
H: T_Google;
|
|
|
|
begin
|
|
|
|
|
|
|
|
create_H(H, network);
|
|
|
|
put_line("created H");
|
|
|
|
-- put(H); new_line; new_line;
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
|
|
|
|
-- on applique l'algorithme itératif
|
|
|
|
for i in 1..ite_max loop
|
|
|
|
pi := calcul(pi, H, alpha);
|
|
|
|
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
|
|
|
|
ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max));
|
|
|
|
end loop; new_line;
|
|
|
|
|
|
|
|
-- new_line;
|
|
|
|
-- put_line("final pi:");
|
|
|
|
-- put(pi);
|
|
|
|
end;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
|
|
|
|
-- on trie pi avec ses indices
|
|
|
|
quicksort(pi, pi_index);
|
|
|
|
put_line("sorted pi and pi_index");
|
|
|
|
flip(pi);
|
|
|
|
put_line("reversed pi");
|
|
|
|
flip(pi_index);
|
|
|
|
put_line("reversed pi_index");
|
|
|
|
|
|
|
|
new_line;
|
2020-12-22 13:26:25 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
-- on écrit les résultats dans les fichiers
|
|
|
|
create(file, Out_File, To_String(filename & "_GH05.p"));
|
|
|
|
put(file, N, 1); put(file, ' ');
|
|
|
|
put(file, alpha, Fore=>1, Aft=>10); put(file, ' ');
|
|
|
|
put(file, ite_max, 1); new_line(file);
|
|
|
|
put(file, pi);
|
|
|
|
close(file);
|
|
|
|
put_line("wrote pi to " & To_String(filename & "_GH05.p"));
|
|
|
|
|
|
|
|
create(file, Out_File, To_String(filename & "_GH05.ord"));
|
|
|
|
put(file, pi_index);
|
|
|
|
close(file);
|
|
|
|
put_line("wrote pi_index to " & To_String(filename & "_GH05.ord"));
|
|
|
|
end;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
exception
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- si il y a une erreur lors du parsing des arguments, on rappelle l'usage.
|
|
|
|
when ERROR_args =>
|
2020-12-22 13:26:25 +00:00
|
|
|
new_line;
|
|
|
|
put_line("Erreur lors de la saisi de la commande.");
|
|
|
|
put_line("Usage: pagerank [-P] [-i max_iterations] [-a alpha] [-h] fichier_reseau.net");
|
|
|
|
|
|
|
|
when ERROR_alpha =>
|
|
|
|
new_line;
|
|
|
|
put_line("Erreur lors de la saisi de alpha.");
|
|
|
|
put_line("alpha ∈ [0, 1]");
|
|
|
|
|
|
|
|
when ERROR_ite =>
|
|
|
|
new_line;
|
|
|
|
put_line("Erreur lors de la saisi de ite_max.");
|
|
|
|
put_line("ite_max ∈ ⟦0, 150⟧");
|
|
|
|
|
2020-12-23 14:40:04 +00:00
|
|
|
when ERROR_filename =>
|
|
|
|
new_line;
|
|
|
|
put_line("Erreur lors de la saisi du fichier réseau.");
|
|
|
|
put_line("Veuillez rentrer un nom valide.");
|
|
|
|
|
|
|
|
when INFO_tips =>
|
|
|
|
put_line("Essayez 'pagerank --help' pour plus d'informations.");
|
|
|
|
|
2020-12-22 13:26:25 +00:00
|
|
|
when INFO_help =>
|
|
|
|
new_line;
|
|
|
|
put_line("Usage: pagerank [OPTIONS] network.net");
|
|
|
|
put_line("Calcule le pagerank d'un réseau à partir de son réseau.net");
|
|
|
|
new_line;
|
|
|
|
put_line("Options:");
|
|
|
|
put_line(" -P, --naif specifies which type of matrix to use");
|
|
|
|
put_line(" -a, --alpha specifies the alpha constant (alpha ∈ [0, 1])");
|
|
|
|
put_line(" -i, --ite-max specifies the maximum number of iterations (ite_max ∈ ⟦0, 150⟧)");
|
|
|
|
put_line(" -h, --help display this help message and exit");
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
end pageRank;
|