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;
|
2021-01-22 14:26:52 +00:00
|
|
|
with Google;
|
|
|
|
with Quicksort;
|
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;
|
2021-01-13 14:13:57 +00:00
|
|
|
ERROR_iteration: 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
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
-- définition du type T_Reel
|
2021-01-22 15:04:09 +00:00
|
|
|
Type T_Reel is digits 6;
|
2021-01-22 14:26:52 +00:00
|
|
|
-- on utilise le module générique Float_IO pour pouvoir afficher T_Reel directement
|
|
|
|
package Text_T_Reel is
|
|
|
|
new Ada.Text_IO.Float_IO(Num => T_Reel);
|
|
|
|
use Text_T_Reel;
|
2020-12-09 17:43:46 +00:00
|
|
|
|
2021-01-13 14:13:57 +00:00
|
|
|
-- permet de faire un retour chariot pour écrire plusieurs fois sur la même ligne, pas possible avec un put
|
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;
|
2021-01-22 14:26:52 +00:00
|
|
|
alpha: in out T_Reel;
|
2020-12-16 20:33:42 +00:00
|
|
|
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
|
2021-01-22 14:26:52 +00:00
|
|
|
alpha := T_Reel'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
|
2021-01-13 14:13:57 +00:00
|
|
|
raise ERROR_iteration;
|
2020-12-22 13:26:25 +00:00
|
|
|
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
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
elsif Argument(i)'Length > 4 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;
|
2021-01-22 14:26:52 +00:00
|
|
|
put("alpha = "); put(alpha, 1); 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));
|
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
|
2021-01-13 14:13:57 +00:00
|
|
|
raise ERROR_iteration;
|
2020-12-23 14:40:04 +00:00
|
|
|
else
|
2021-01-12 20:44:29 +00:00
|
|
|
put_line("Unexpected constraint_error");
|
2020-12-23 14:40:04 +00:00
|
|
|
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;
|
2021-01-22 14:26:52 +00:00
|
|
|
alpha: T_Reel := 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);
|
2021-01-22 14:26:52 +00:00
|
|
|
put_line("parsed successfully arguments"); new_line;
|
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
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
-- on calcule la densité
|
|
|
|
put("densité = "); put(T_Reel(N_Links)/T_Reel(N)**2, 1); new_line;
|
2020-12-22 13:26:25 +00:00
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
new_line;
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
-- on renvient au début du fichier
|
|
|
|
reset(file, In_File);
|
|
|
|
-- au saute la première ligne pour s'apprêter à lire le réseau
|
|
|
|
skip_line(file);
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
declare
|
2021-01-22 14:26:52 +00:00
|
|
|
-- on peut maintenant créer nos vecteurs
|
2020-12-24 16:55:22 +00:00
|
|
|
package Vector_Entier is
|
2021-01-22 14:26:52 +00:00
|
|
|
new Vector.Entier(T_Entier => Natural, Capacite => N);
|
|
|
|
package Vector_Reel is
|
|
|
|
new Vector.Reel(T_Reel => T_Reel, Capacite => N);
|
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
use Vector_Entier;
|
2021-01-22 14:26:52 +00:00
|
|
|
use Vector_Reel;
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
pi: Vector_Reel.T_Vecteur;
|
|
|
|
pi_index: Vector_Entier.T_Vecteur;
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
-- on instancie aussi l'algorithme QuickSort
|
|
|
|
package Quicksort_Reel_Entier is
|
|
|
|
new Quicksort(Vector_Index => Vector_Entier,
|
|
|
|
Vector_Element => Vector_Reel);
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
use Quicksort_Reel_Entier;
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
begin
|
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
initialize(pi, 1.0/T_Reel(N));
|
|
|
|
put_line("initialized pi to 1/N");
|
2020-12-24 16:55:22 +00:00
|
|
|
-- 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
|
2021-01-22 14:26:52 +00:00
|
|
|
package Google_Naif is
|
|
|
|
new Google.Naif(N => N,
|
2020-12-24 16:55:22 +00:00
|
|
|
N_links => N_links,
|
2021-01-22 14:26:52 +00:00
|
|
|
Vector_Element => Vector_Reel);
|
|
|
|
use Google_Naif;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
|
|
|
-- définition de la matrice Google
|
|
|
|
G: T_Google;
|
|
|
|
begin
|
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
initialize(G, 0.0);
|
|
|
|
put_line("initialized G to zeros");
|
2020-12-24 16:55:22 +00:00
|
|
|
-- put(G); new_line;
|
|
|
|
|
|
|
|
new_line;
|
2021-01-22 14:26:52 +00:00
|
|
|
create_Google(G, alpha, file);
|
2020-12-24 16:55:22 +00:00
|
|
|
put_line("created G");
|
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
close(file);
|
|
|
|
put("closed "); put(To_String(filename & ".net")); new_line;
|
2020-12-24 16:55:22 +00:00
|
|
|
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));
|
2021-01-22 15:04:09 +00:00
|
|
|
end loop;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
|
|
|
-- new_line;
|
|
|
|
-- put_line("final pi:");
|
|
|
|
-- put(pi); new_line;
|
|
|
|
end;
|
2021-01-13 14:13:57 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
else -- not naif
|
|
|
|
declare
|
|
|
|
-- on instancie le module générique Creux de Google
|
2021-01-22 14:26:52 +00:00
|
|
|
package Google_Creux is
|
|
|
|
new Google.Creux(N => N,
|
2020-12-24 16:55:22 +00:00
|
|
|
N_links => N_links,
|
2021-01-22 14:26:52 +00:00
|
|
|
Vector_Element => Vector_Reel);
|
|
|
|
use Google_Creux;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
|
|
|
-- définition de la matrice Google
|
2021-01-22 14:26:52 +00:00
|
|
|
G: T_Google;
|
2020-12-24 16:55:22 +00:00
|
|
|
begin
|
|
|
|
|
2021-01-22 14:26:52 +00:00
|
|
|
create_Google(G, file);
|
|
|
|
put_line("created G");
|
|
|
|
|
|
|
|
-- new_line; new_line;
|
|
|
|
-- put(G);
|
|
|
|
-- new_line; new_line;
|
|
|
|
|
|
|
|
close(file);
|
|
|
|
put("closed "); put(To_String(filename & ".net")); new_line;
|
|
|
|
new_line;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
|
|
|
-- on applique l'algorithme itératif
|
|
|
|
for i in 1..ite_max loop
|
2021-01-22 14:26:52 +00:00
|
|
|
pi := calcul(pi, G, alpha);
|
2020-12-24 16:55:22 +00:00
|
|
|
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
|
2021-01-22 14:26:52 +00:00
|
|
|
sort(pi, pi_index);
|
2020-12-24 16:55:22 +00:00
|
|
|
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"));
|
2021-01-22 14:26:52 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
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.");
|
2021-01-22 14:26:52 +00:00
|
|
|
put_line("Usage: pagerank [-n] [-i max_iterations] [-a alpha] [-h] fichier_reseau.net");
|
2020-12-22 13:26:25 +00:00
|
|
|
|
|
|
|
when ERROR_alpha =>
|
|
|
|
new_line;
|
|
|
|
put_line("Erreur lors de la saisi de alpha.");
|
|
|
|
put_line("alpha ∈ [0, 1]");
|
|
|
|
|
2021-01-13 14:13:57 +00:00
|
|
|
when ERROR_iteration =>
|
2020-12-22 13:26:25 +00:00
|
|
|
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.");
|
2021-01-22 14:26:52 +00:00
|
|
|
put_line("Veuillez rentrer un fichier existant.");
|
2020-12-23 14:40:04 +00:00
|
|
|
|
|
|
|
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:");
|
2021-01-22 14:26:52 +00:00
|
|
|
put_line(" -n, --naif specifies which type of matrix to use");
|
2020-12-22 13:26:25 +00:00
|
|
|
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;
|