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-09 17:43:46 +00:00
|
|
|
|
2020-12-07 22:03:27 +00:00
|
|
|
with Google_Naive;
|
2020-12-19 15:08:54 +00:00
|
|
|
with Vector;
|
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-05 15:20:01 +00:00
|
|
|
ERROR_args: Exception;
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on définit le type T_Double, et on utilise le module générique Float_IO pour pouvoir l'afficher
|
2020-12-19 15:08:54 +00:00
|
|
|
Type T_Double is digits 11;
|
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-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-09 17:43:46 +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-12 13:05:39 +00:00
|
|
|
if not(0 < Argument_Count and Argument_Count <= 6) 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
|
|
|
|
if Argument(i) = "-P" then
|
|
|
|
naif := True;
|
|
|
|
i := i + 1;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed naif");
|
2020-12-05 15:20:01 +00:00
|
|
|
elsif Argument(i) = "-A" then
|
2020-12-16 20:33:42 +00:00
|
|
|
alpha := T_Double'Value(Argument(i+1));
|
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-05 15:20:01 +00:00
|
|
|
elsif Argument(i) = "-I" then
|
2020-12-12 13:05:39 +00:00
|
|
|
ite_max := Natural'Value(Argument(i+1));
|
2020-12-05 15:20:01 +00:00
|
|
|
i := i + 2;
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("parsed ite_max");
|
|
|
|
elsif 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-05 15:20:01 +00:00
|
|
|
else
|
2020-12-12 13:05:39 +00:00
|
|
|
put_line("unexpected passing case");
|
2020-12-05 15:20:01 +00:00
|
|
|
raise ERROR_args;
|
|
|
|
end if;
|
|
|
|
exit when i > Argument_Count;
|
|
|
|
end loop;
|
|
|
|
end if;
|
2020-12-12 13:05:39 +00:00
|
|
|
|
|
|
|
new_line;
|
|
|
|
put("alpha = "); put(alpha, 1); new_line;
|
|
|
|
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-16 20:33:42 +00:00
|
|
|
when others => raise ERROR_args;
|
2020-12-05 18:18:53 +00:00
|
|
|
end get_args;
|
2020-12-05 15:20:01 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
procedure choix_type_algo(N: Natural;
|
|
|
|
filename: in Unbounded_String;
|
|
|
|
file: in out Ada.Text_IO.File_Type;
|
|
|
|
alpha: T_Double;
|
|
|
|
ite_max: Natural;
|
|
|
|
naif: Boolean) is
|
2020-12-19 15:08:54 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on utilise le module générique
|
2020-12-19 15:08:54 +00:00
|
|
|
package Vector_T_Double is
|
|
|
|
new Vector(T_Element => T_Double, N => N);
|
|
|
|
use Vector_T_Double;
|
2020-12-09 14:43:16 +00:00
|
|
|
|
2020-12-16 20:43:17 +00:00
|
|
|
procedure algorithm_naif(file: in out Ada.Text_IO.File_Type;
|
2020-12-16 20:33:42 +00:00
|
|
|
alpha: in T_Double;
|
2020-12-16 16:41:52 +00:00
|
|
|
ite_max: in Natural;
|
|
|
|
pi: out T_Vecteur) is
|
2020-12-19 15:08:54 +00:00
|
|
|
|
|
|
|
-- on utilise le module générique
|
|
|
|
package Google is
|
|
|
|
new Google_Naive(T_Element => T_Double, N => N, Vector_T_Element => Vector_T_Double);
|
|
|
|
use Google;
|
2020-12-16 16:41:52 +00:00
|
|
|
G: T_Google;
|
2020-12-19 15:08:54 +00:00
|
|
|
|
2020-12-16 16:41:52 +00:00
|
|
|
begin
|
|
|
|
|
|
|
|
initialize(pi);
|
|
|
|
put_line("initialized pi");
|
|
|
|
--put(pi); new_line;
|
|
|
|
|
|
|
|
initialize(G);
|
|
|
|
put_line("initialized G");
|
2020-12-16 20:33:42 +00:00
|
|
|
--put(G); new_line;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
create_H(G, file);
|
|
|
|
put_line("created H");
|
|
|
|
close(file);
|
2020-12-16 20:33:42 +00:00
|
|
|
--put(G); new_line;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
create_S(G);
|
|
|
|
put_line("created S");
|
2020-12-16 20:33:42 +00:00
|
|
|
--put(G); new_line;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
create_G(G, alpha);
|
|
|
|
put_line("created G");
|
2020-12-16 20:33:42 +00:00
|
|
|
--put(G); new_line;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
-- on applique l'algorithme itératif
|
|
|
|
put("ite: ");
|
|
|
|
for i in 1..ite_max loop
|
|
|
|
pi := pi * G;
|
|
|
|
put(i, 1); put(" ");
|
|
|
|
end loop; new_line;
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
--new_line;
|
|
|
|
--put_line("final pi:");
|
2020-12-19 15:08:54 +00:00
|
|
|
--put(pi);
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
end algorithm_naif;
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- procedure algorithm_creux(); -- TODO
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-19 15:08:54 +00:00
|
|
|
procedure write_to_files(filename: in Unbounded_String; pi_sorted: in T_Vecteur; pi_index: in T_Vecteur_Index) is
|
2020-12-16 16:41:52 +00:00
|
|
|
file: Ada.Text_IO.File_Type;
|
|
|
|
begin
|
|
|
|
create(file, Out_File, To_String(filename & ".p"));
|
2020-12-19 15:08:54 +00:00
|
|
|
put(file, N, 1);
|
|
|
|
put(file, alpha, 2);
|
|
|
|
put(file, ite_max, 2);
|
2020-12-16 16:41:52 +00:00
|
|
|
new_line(file);
|
|
|
|
put(file, pi_sorted);
|
|
|
|
close(file);
|
|
|
|
|
|
|
|
create(file, Out_File, To_String(filename & ".ord"));
|
|
|
|
put(file, pi_index);
|
|
|
|
close(file);
|
2020-12-16 20:33:42 +00:00
|
|
|
end write_to_files;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-19 15:08:54 +00:00
|
|
|
pi: T_Vecteur;
|
|
|
|
pi_index: T_Vecteur_Index;
|
2020-12-09 14:43:16 +00:00
|
|
|
|
2020-12-05 18:18:53 +00:00
|
|
|
begin
|
2020-12-09 14:43:16 +00:00
|
|
|
|
2020-12-16 16:41:52 +00:00
|
|
|
if naif then
|
2020-12-16 20:43:17 +00:00
|
|
|
algorithm_naif(file, alpha, ite_max, pi);
|
2020-12-16 16:41:52 +00:00
|
|
|
else
|
|
|
|
--algorithm_creux(N, file, alpha, ite_max, pi);
|
2020-12-16 20:33:42 +00:00
|
|
|
put_line("TODO");
|
2020-12-16 16:41:52 +00:00
|
|
|
end if;
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on trie les poids par ordre décroissant, on tri en même temps les indices des pages
|
2020-12-19 15:08:54 +00:00
|
|
|
initialize(pi_index);
|
|
|
|
sort_with_index_desc(pi, pi_index);
|
|
|
|
|
|
|
|
new_line;
|
|
|
|
put_line("sorted pi:");
|
|
|
|
put(pi); new_line;
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on écrit les resultats dans des fichiers
|
2020-12-19 15:08:54 +00:00
|
|
|
write_to_files(filename, pi, pi_index);
|
2020-12-16 16:41:52 +00:00
|
|
|
|
|
|
|
end choix_type_algo;
|
2020-12-09 14:43:16 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- initialisation 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-16 20:33:42 +00:00
|
|
|
-- initialisation des variables pour créer les matices/vecteurs
|
2020-12-09 14:43:16 +00:00
|
|
|
N: Positive;
|
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);
|
|
|
|
put_line("args OK");
|
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-12 13:05:39 +00:00
|
|
|
put_line("file OK");
|
2020-12-05 18:18:53 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on récupère le nombre de pages
|
|
|
|
get(file, N);
|
2020-12-12 13:05:39 +00:00
|
|
|
put("N = "); put(N, 1); new_line;
|
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
-- on peut maintenant choisir le type de matrice que l'on souhaite
|
2020-12-16 16:41:52 +00:00
|
|
|
choix_type_algo(N, filename, file, alpha, ite_max, naif);
|
|
|
|
|
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 =>
|
|
|
|
put_line("Erreur lors de la saisi de la commande");
|
|
|
|
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
|
2020-12-16 16:41:52 +00:00
|
|
|
|
2020-12-16 20:33:42 +00:00
|
|
|
end pageRank;
|