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

203 lines
5.8 KiB
Ada
Raw Normal View History

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
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 := 1;
begin
put("Argument_Count = "); put(Argument_Count, 1); new_line;
for i in 1 .. Argument_Count loop
Put("Argument(");
Put(i, 0);
Put(") = ");
Put_line(Argument(i));
end loop;
new_line;
-- on vérifie d'abord que le nombre d'arguments est cohérent
if not(0 < Argument_Count and Argument_Count <= 6) then
raise ERROR_args;
else -- sinon on parse les arguments
loop
if Argument(i) = "-P" then
naif := True;
i := i + 1;
put_line("parsed naif");
elsif Argument(i) = "-A" then
alpha := Float'Value(Argument(i+1));
i := i + 2;
put_line("parsed alpha");
elsif Argument(i) = "-I" then
ite_max := Natural'Value(Argument(i+1));
i := i + 2;
put_line("parsed ite_max");
elsif Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-4));
i := i + 1;
put_line("parsed filename");
else
put_line("unexpected passing case");
raise ERROR_args;
end if;
exit when i > Argument_Count;
end loop;
end if;
new_line;
put("alpha = "); put(alpha, 1); new_line;
put("naif = "); put(Boolean'Pos(naif), 1); new_line;
put("ite_max = "); put(Float(ite_max), 1); new_line;
put("filename = "); put_line(To_String(filename));
new_line;
exception
-- s'il y a un problème on arrête l'execution 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 choix_type_algo(N: Natural; filename: in Unbounded_String; file: in out Ada.Text_IO.File_Type; alpha: Float; ite_max: Natural; naif: Boolean) is
package Google is
new Google_Naive(T_Element => T_Double, N => N);
use Google;
procedure algorithm_naif(N: in Positive;
file: in out Ada.Text_IO.File_Type;
alpha: in Float;
ite_max: in Natural;
pi: out T_Vecteur) is
G: T_Google;
begin
initialize(pi);
put_line("initialized pi");
--put(pi); new_line;
initialize(G);
put_line("initialized G");
put(G); new_line;
create_H(G, file);
put_line("created H");
put(G); new_line;
close(file);
create_S(G);
put_line("created S");
put(G); new_line;
create_G(G, alpha);
put_line("created G");
put(G); new_line;
-- 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;
new_line;
put_line("final pi:");
put(pi); new_line;
end algorithm_naif;
-- procedure algorithm_creux();
procedure write_to_file(filename: in Unbounded_String; pi_sorted: in T_Vecteur; pi_index: in T_Vecteur) is
file: Ada.Text_IO.File_Type;
begin
create(file, Out_File, To_String(filename & ".p"));
put(file, N);
put(file, alpha);
put(file, ite_max);
new_line(file);
put(file, pi_sorted);
close(file);
create(file, Out_File, To_String(filename & ".ord"));
put(file, pi_index);
close(file);
end write_to_file;
pi, pi_sorted, pi_index: T_Vecteur;
begin
if naif then
algorithm_naif(N, file, alpha, ite_max, pi);
else
--algorithm_creux(N, file, alpha, ite_max, pi);
null;
end if;
sort_with_index_desc(pi, pi_sorted, pi_index);
write_to_file(filename, pi_sorted, pi_index);
end choix_type_algo;
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, ite_max, alpha, naif);
put_line("args OK");
open(file, In_File, To_String(filename & ".net"));
put_line("file OK");
get(file, N); -- on récupère le nombre de pages
put("N = "); put(N, 1); new_line;
choix_type_algo(N, filename, file, alpha, ite_max, naif);
end pageRank;
--def triRapide(L): # Quicksort
-- if len(L) <= 1:
-- return L
-- pvt = L[0]
-- L1, L2 = [], []
-- for i in range(1, len(L)):
-- if L[i] < pvt:
-- L1.append(L[i])
-- else:
-- L2.append(L[i])
-- return triRapide(L1) + [pvt] + triRapide(L2)