fix typos pour googleNaive.ad*, pageRank.adb tjs brouillon
git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@207227 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
parent
05d2d4dddf
commit
da91ba9da2
|
@ -1,108 +1,91 @@
|
||||||
package body googleNaive is
|
package body googleNaive is
|
||||||
|
|
||||||
function "*"(left, right: T_Google) return T_Google is
|
function "*"(left, right: T_Google) return T_Google is
|
||||||
M : T_Google;
|
mat: T_Google;
|
||||||
n, m, o: Positive;
|
n: Positive := left.dim(0);
|
||||||
Compteur : Integer;
|
m: Positive := right.dim(1);
|
||||||
|
l: Positive := left.dim(1);
|
||||||
|
Compteur: T_Element := 0;
|
||||||
begin
|
begin
|
||||||
n:=left.dimension(0);
|
initialize(mat);
|
||||||
m:=right.dimension(1);
|
|
||||||
o :=left.dimension(1);
|
|
||||||
initialize(M,n,m);
|
|
||||||
for i in 0..n loop
|
for i in 0..n loop
|
||||||
for j in 0..m loop
|
for j in 0..m loop
|
||||||
Compteur:=0;
|
for k in 0..l loop
|
||||||
for l in 0..o loop
|
Compteur := Compteur + left.matrix(i)(k)*right.matrix(k)(j);
|
||||||
Compteur:= Compteur + left.matrix(i,l)*right.matrix(l,j);
|
|
||||||
end loop;
|
end loop;
|
||||||
M.matrix(i,j):=Compteur;
|
mat.matrix(i)(j) := Compteur;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
return M;
|
return mat;
|
||||||
end "*";
|
end "*";
|
||||||
|
|
||||||
function "+"(left, right: T_Google) return T_Google is
|
function "+"(left, right: T_Google) return T_Google is
|
||||||
n, m : Positive;
|
n: Positive := left.dim(0);
|
||||||
M : T_Google;
|
m: Positive := left.dim(1);
|
||||||
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
n := left.dimension(0);
|
initialize(mat);
|
||||||
m := left.dimension(1);
|
|
||||||
initialize(M,n,m);
|
|
||||||
for i in 0..n loop
|
for i in 0..n loop
|
||||||
for j in 0..m loop
|
for j in 0..m loop
|
||||||
M(i,j):=left.matrix(i,j) + right.matrix(i,j);
|
mat(i)(j) := left.matrix(i)(j) + right.matrix(i)(j);
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
return M;
|
return mat;
|
||||||
end "+";
|
end "+";
|
||||||
|
|
||||||
function "*"(left: T_Google; right: Integer) return T_Google is
|
function "*"(left: T_Google; right: Integer) return T_Google is
|
||||||
n, m : Positive;
|
n: Positive := left.dim(0);
|
||||||
M : T_Google;
|
m: Positive := left.dim(1);
|
||||||
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
n := left.dimension(0);
|
initialize(mat);
|
||||||
m := left.dimension(1);
|
|
||||||
initialize(M,n,m);
|
|
||||||
for i in 0..n loop
|
for i in 0..n loop
|
||||||
for j in 0..m loop
|
for j in 0..m loop
|
||||||
M(i,j):=right*left.matrix(i,j);
|
mat(i)(j) := right*left.matrix(i)(j);
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
return M;
|
return mat;
|
||||||
end "*";
|
end "*";
|
||||||
|
|
||||||
function "/"(left: T_Google; right: Integer) return T_Google is
|
function "/"(left: T_Google; right: Integer) return T_Google is
|
||||||
n, m : Positive;
|
n: Positive := left.dim(0);
|
||||||
M : T_Google;
|
m: Positive := left.dim(1);
|
||||||
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
n := left.dimension(0);
|
initialize(mat);
|
||||||
m := left.dimension(1);
|
|
||||||
initialize(M,n,m);
|
|
||||||
for i in 0..n loop
|
for i in 0..n loop
|
||||||
for j in 0..m loop
|
for j in 0..m loop
|
||||||
M(i,j):=left.matrix(i,j)/right;
|
mat(i)(j) := left.matrix(i)(j)/right;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
return M;
|
return mat;
|
||||||
end "/";
|
end "/";
|
||||||
|
|
||||||
function zeros(rows, cols: in Positive) return T_Google is
|
procedure initialize(mat: out T_Google) is
|
||||||
M : T_Google;
|
|
||||||
begin
|
begin
|
||||||
initialize(M,rows,cols);
|
mat.dim(0) := nb_rows;
|
||||||
for i in 1..cows loop
|
mat.dim(1) := nb_cols;
|
||||||
|
for i in 1..rows loop
|
||||||
for j in 1..cols loop
|
for j in 1..cols loop
|
||||||
M(i,j):=0;
|
mat.matrix(i)(j) := 0;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
end zeros;
|
end initialize;
|
||||||
|
|
||||||
function ones(rows, cols: in Positive) return T_Google is
|
function ones(rows, cols: Positive) return T_Google is
|
||||||
M : T_Google;
|
mat: T_Google;
|
||||||
begin
|
begin
|
||||||
initialize(M,rows,cols);
|
initialize(mat);
|
||||||
for i in 1..cows loop
|
for i in 1..rows loop
|
||||||
for j in 1..cols loop
|
for j in 1..cols loop
|
||||||
M(i,j):=1;
|
mat(i)(j) := 1;
|
||||||
end loop;
|
end loop;
|
||||||
end loop;
|
end loop;
|
||||||
end ones;
|
end ones;
|
||||||
|
|
||||||
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element) is
|
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element) is
|
||||||
begin
|
begin
|
||||||
mat.matrix(i,j):=elm;
|
mat.matrix(i)(j) := elm;
|
||||||
end insert;
|
end insert;
|
||||||
|
|
||||||
procedure initialize(mat: out T_Google; rows, cols : Positive ) is
|
|
||||||
|
|
||||||
begin
|
|
||||||
mat.dimension(0):=rows;
|
|
||||||
mat.dimension(1):=cols;
|
|
||||||
end initialize;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end googleNaive;
|
end googleNaive;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
generic
|
generic
|
||||||
|
|
||||||
type T_Element is private;
|
type T_Element is private;
|
||||||
CAPACITY: Positive;
|
nb_rows, nb_cols: Positive;
|
||||||
|
|
||||||
package googleNaive is
|
package googleNaive is
|
||||||
|
|
||||||
|
@ -9,21 +9,21 @@ package googleNaive is
|
||||||
type T_Google is limited private;
|
type T_Google is limited private;
|
||||||
|
|
||||||
function "*"(left, right: T_Google) return T_Google with
|
function "*"(left, right: T_Google) return T_Google with
|
||||||
Pre => left.dimension(1)=right.dimension(0),
|
Pre => left.dim(1) = right.dim(0),
|
||||||
Post => "*"'Result.dimension(0)= left.dimension(0) and "*"'Result.dimension(1)= right.dimension(1);
|
Post => "*"'Result.dim(0) = left.dim(0)*
|
||||||
|
and "*"'Result.dim(1) = right.dim(1);
|
||||||
|
|
||||||
function "+"(left, right: T_Google) return T_Google with
|
function "+"(left, right: T_Google) return T_Google with
|
||||||
Pre => left.dimension=right.dimension;
|
Pre => left.dim = right.dim;
|
||||||
|
|
||||||
function "*"(left: T_Google; right: Integer) return T_Google;
|
function "*"(left: T_Google; right: T_Element) return T_Google;
|
||||||
function "/"(left: T_Google; right: Integer) return T_Google with
|
function "/"(left: T_Google; right: T_Element) return T_Google with
|
||||||
Pre => right/=0;
|
Pre => right /= 0;
|
||||||
|
|
||||||
procedure initialize(mat: out T_Google; rows, cols : Positive )with
|
procedure initialize(mat: out T_Google; rows, cols : Positive) with
|
||||||
Post => isEmpty(mat);
|
Post => isEmpty(mat);
|
||||||
|
|
||||||
function ones(rows, cols: in Positive) return T_Google;
|
function ones(rows, cols: Positive) return T_Google;
|
||||||
function zeros(rows, cols: in Positive) return T_Google;
|
|
||||||
|
|
||||||
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element);
|
procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element);
|
||||||
|
|
||||||
|
@ -31,10 +31,9 @@ package googleNaive is
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
type T_Row is array (0..CAPACITY-1) of T_Element;
|
type T_Row is array (0..nb_cols-1) of T_Element;
|
||||||
type T_Google is record
|
type T_Google is record
|
||||||
dimension: array (0..1) of Positive;
|
dim: array (0..1) of Positive;
|
||||||
matrix: array (0..CAPACITY-1) of T_Row;
|
matrix: array (0..nb_rows-1) of T_Row; -- à voir si on obtient pas l'erreur: anonymous arrays not allowed as components
|
||||||
end record;
|
|
||||||
|
|
||||||
end googleNaive;
|
end googleNaive;
|
||||||
|
|
124
src/pageRank.adb
124
src/pageRank.adb
|
@ -5,11 +5,13 @@ with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||||
with googleNaive;
|
with googleNaive;
|
||||||
|
|
||||||
procedure pageRank is
|
procedure pageRank is
|
||||||
|
|
||||||
|
Type T_Double is digits 6; -- mettre la précision en générique au lieu de T_Element ?
|
||||||
|
|
||||||
ERROR_args: Exception;
|
ERROR_args: Exception;
|
||||||
|
|
||||||
procedure getArgs(filename: in out Unbounded_String;
|
procedure get_args(filename: in out Unbounded_String; -- remplaçable par un String ?
|
||||||
iteMAX: in out Natural;
|
ite_max: in out Natural;
|
||||||
alpha: in out Float;
|
alpha: in out Float;
|
||||||
naif: in out Boolean) is
|
naif: in out Boolean) is
|
||||||
i: Natural := 0;
|
i: Natural := 0;
|
||||||
|
@ -27,7 +29,7 @@ procedure pageRank is
|
||||||
alpha := Float'Value(Argument(i+1));
|
alpha := Float'Value(Argument(i+1));
|
||||||
i := i + 2;
|
i := i + 2;
|
||||||
elsif Argument(i) = "-I" then
|
elsif Argument(i) = "-I" then
|
||||||
iteMAX := Natural'Value(Argument(i+1)); -- verif si les conversions sont bonnes
|
ite_max := Natural'Value(Argument(i+1)); -- verif si les conversions sont bonnes
|
||||||
i := i + 2;
|
i := i + 2;
|
||||||
elsif Argument(i)(Argument(i)'Last-4 .. Argument(i)'Last) = ".net" then -- verif les indexs
|
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));
|
filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-5));
|
||||||
|
@ -43,74 +45,84 @@ procedure pageRank is
|
||||||
when others =>
|
when others =>
|
||||||
put_line("Erreur lors de la saisi de la commande");
|
put_line("Erreur lors de la saisi de la commande");
|
||||||
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
|
put_line("Usage: pagerank [-I max_iterations] [-A alpha] [-P] fichier_reseau.net");
|
||||||
end getArgs;
|
end get_args;
|
||||||
|
|
||||||
procedure createG_naive(G: in out T_Google) is
|
function row_sum(row: T_Row) return Natural is
|
||||||
File : Ada.Text_IO.File_Type;
|
s: Natural := 0;
|
||||||
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
|
begin
|
||||||
open(File, In_File, filename & ".net");
|
for r in row loop
|
||||||
get(File, N); -- on récupère le nombre de pages
|
s := s + r;
|
||||||
|
|
||||||
-- on créé H
|
|
||||||
while not end_of_File(File) loop
|
|
||||||
get(File, row);
|
|
||||||
get(File, col);
|
|
||||||
insert(H, row, col, 1.0);
|
|
||||||
end loop;
|
end loop;
|
||||||
|
return s;
|
||||||
-- on créé S
|
end row_sum;
|
||||||
-- 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;
|
filename: Unbounded_String;
|
||||||
iteMAX: Natural := 150;
|
ite_max: Natural := 150;
|
||||||
naif: Boolean := False;
|
naif: Boolean := False;
|
||||||
alpha: Float := 0.85;
|
alpha: Float := 0.85;
|
||||||
|
|
||||||
epsilon: Float := 0.01;
|
epsilon: Float := 0.01;
|
||||||
pi: T_Google;
|
ite: Natural := 0;
|
||||||
G: T_Google;
|
|
||||||
|
file: Ada.Text_IO.File_Type;
|
||||||
|
|
||||||
N: Positive;
|
N: Positive;
|
||||||
|
pi, pi_last, H, S, G: T_Google;
|
||||||
|
row, col: Natural;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
getArgs(filename, alpha, iteMAX, naif);
|
get_args(filename, alpha, ite_max, naif);
|
||||||
|
|
||||||
initialize(pi, 1, N);
|
|
||||||
initialize(G, N, N);
|
|
||||||
createG_naive(G);
|
|
||||||
|
|
||||||
for i in 1..N loop
|
open(file, In_File, filename & ".net"); -- verif si la concaténation fonctionne
|
||||||
pi := pi * G;
|
get(file, N); -- on récupère le nombre de pages
|
||||||
|
|
||||||
|
-- on peut maintenant utiliser le module générique Google_Naive
|
||||||
|
-- je sais pas si on peut utiliser deux fois le module, à voir
|
||||||
|
package Matrice is
|
||||||
|
new Google_Naive(T_Element => T_Double,
|
||||||
|
nb_rows => N,
|
||||||
|
nb_cols => N);
|
||||||
|
use Matrice;
|
||||||
|
package Vecteur is
|
||||||
|
new Google_Naive(T_Element => T_Double,
|
||||||
|
nb_rows => 1,
|
||||||
|
nb_cols => N);
|
||||||
|
use Vecteur;
|
||||||
|
|
||||||
|
-- on crée H
|
||||||
|
while not end_of_File(file) loop
|
||||||
|
get(file, row);
|
||||||
|
get(file, col);
|
||||||
|
insert(H, row, col, 1.0);
|
||||||
end loop;
|
end loop;
|
||||||
|
|
||||||
-- write2file(filename, pi); à implémenter
|
-- on crée S
|
||||||
|
-- horrible, voir si simplifiable
|
||||||
|
for row in H loop
|
||||||
|
p = row_sum(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;
|
||||||
|
|
||||||
|
-- on crée G
|
||||||
|
G := S*alpha + ones(N, N)*(1-alpha)/N;
|
||||||
|
|
||||||
|
-- on applique l'algorithme itératif
|
||||||
|
loop
|
||||||
|
pi_last := pi;
|
||||||
|
pi := pi * G;
|
||||||
|
ite := ite + 1;
|
||||||
|
exit when (ite >= ite_max) or (pi(1)(1) - pi_last(1)(1) < epsilon);
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
-- write_to_files(filename, ...); -- TODO
|
||||||
|
|
||||||
end pageRank;
|
end pageRank;
|
Loading…
Reference in a new issue