In [1]:
Sys.command "ocaml -version";;
Out[1]:
In [14]:
let ligne p t i = Array.init p (fun j -> t.(i).(j)) ;;
(* t.(i) marche aussi bien ! *)
let colonne p t j = Array.init p (fun i -> t.(i).(j)) ;;
Out[14]:
Out[14]:
In [15]:
let tousVrai = Array.for_all (fun x -> x);;
Out[15]:
In [16]:
let estNp p tab =
if tousVrai (Array.map (fun x -> (1 <= x) && (x <= p)) tab) then begin
let estLa = Array.make p false in
for i = 0 to p - 1 do
estLa.(tab.(i) - 1) <- true
done;
tousVrai estLa
end
else
false
;;
Out[16]:
In [24]:
let sum_array = Array.fold_left (+) 0;;
Out[24]:
In [25]:
let estNp_compte p tab =
if tousVrai (Array.map (fun x -> (1 <= x) && (x <= p)) tab) then begin
let estLa = Array.make p 0 in
for i = 0 to p - 1 do
estLa.(tab.(i) - 1) <- 1
done;
sum_array estLa
end
else
0
;;
Out[25]:
In [26]:
let racine_carree i = int_of_float (sqrt (float_of_int i));;
Out[26]:
In [18]:
let contraintes_lignes p t =
tousVrai (Array.init p (fun i ->
estNp p (ligne p t i)
))
;;
Out[18]:
In [27]:
let contraintes_lignes_compte p t =
sum_array (Array.init p (fun i ->
estNp_compte p (ligne p t i)
))
;;
Out[27]:
In [19]:
let contraintes_colonnes p t =
tousVrai (Array.init p (fun j ->
estNp p (colonne p t j)
))
;;
Out[19]:
In [28]:
let contraintes_colonnes_compte p t =
sum_array (Array.init p (fun j ->
estNp_compte p (colonne p t j)
))
;;
Out[28]:
In [20]:
let carre_latin p t =
(contraintes_lignes p t) && (contraintes_colonnes p t)
;;
Out[20]:
In [29]:
let carre_latin_compte p t =
(contraintes_lignes_compte p t) + (contraintes_colonnes_compte p t)
;;
Out[29]:
In [21]:
let petit_carre p n t i j =
Array.init p (fun k ->
t.(n*i + (k / n)).(n*j + (k mod n))
)
;;
Out[21]:
In [22]:
let petits_carres_sont_latins p t =
let n = racine_carree p in
(* Par flemme, on créé le tableau entier, qu'on vérifie après *)
let contraintes_petits_carres =
Array.init p (fun i ->
estNp p (petit_carre p n t (i / n) (i mod n) )
)
in
(* Mais on peut mieux faire, avec une boucle while par exemple, on sort dès qu'une contrainte est fausse *)
tousVrai contraintes_petits_carres
;;
Out[22]:
In [31]:
let petits_carres_sont_latins_compte p t =
let n = racine_carree p in
let contraintes_petits_carres =
Array.init p (fun i ->
estNp_compte p (petit_carre p n t (i / n) (i mod n) )
)
in
sum_array contraintes_petits_carres
;;
Out[31]:
In [32]:
let est_resolu p t =
(carre_latin p t) && (petits_carres_sont_latins p t)
;;
Out[32]:
In [50]:
let nb_contraintes_resolues p t =
(carre_latin_compte p t) + (petits_carres_sont_latins_compte p t)
;;
let score = nb_contraintes_resolues;;
Out[50]:
Out[50]:
In [49]:
let p = 9 ;;
let t = [|
[| 1; 2; 7; 4; 6; 3; 9; 8; 5 |];
[| 3; 4; 9; 8; 7; 5; 2; 6; 1 |];
[| 5; 8; 6; 2; 9; 1; 4; 3; 7 |];
[| 7; 6; 5; 9; 4; 2; 3; 1; 8 |];
[| 8; 3; 4; 7; 1; 6; 5; 2; 9 |];
[| 9; 1; 2; 5; 3; 8; 7; 4; 6 |];
[| 2; 7; 8; 6; 5; 4; 1; 9; 3 |];
[| 4; 5; 3; 1; 8; 9; 6; 7; 2 |];
[| 6; 9; 1; 3; 2; 7; 8; 5; 4 |];
|];
Out[49]:
Out[49]:
In [55]:
let score_max = 3 * 9 * 9;;
Out[55]:
In [56]:
score p t;;
Out[56]:
In [57]:
let p = 9 ;;
let t = [|
[| 1; 2; 7; 4; 6; 3; 9; 8; 5 |];
[| 3; 4; 9; 8; 7; 5; 2; 6; 1 |];
[| 5; 8; 6; 2; 9; 1; 4; 3; 7 |];
[| 7; 6; 5; 9; 4; 2; 3; 1; 8 |];
[| 8; 2; 4; 7; 1; 6; 5; 2; 9 |]; (* Ligne non valable, 2 est là deux fois !*)
[| 9; 1; 2; 5; 3; 8; 7; 4; 6 |];
[| 2; 7; 8; 6; 5; 4; 1; 9; 3 |];
[| 4; 5; 3; 1; 8; 9; 6; 7; 2 |];
[| 6; 9; 1; 3; 2; 7; 8; 5; 4 |];
|];
Out[57]:
Out[57]:
In [60]:
score p t;;
Out[60]:
On voit que 3 contraintes ne sont pas vérifiées.
In [34]:
let population = 100
and mutes = 45
and effaces = 10;;
let conserves = population - mutes - effaces;;
Out[34]:
Out[34]:
In [36]:
Random.self_init();;
Out[36]:
In [62]:
Random.int;;
Out[62]:
In [63]:
let choix_sans_remise tab k =
let n = Array.length tab in
assert 0 <= k <= n;
let reponse = Array.make k (-1) in
for i = 0 to k-1 do
done;
reponse
;;
In [41]:
let copie n t =
Array.init n (fun i -> (Array.copy t.(i)))
;;
Out[41]:
In [ ]:
let individu_initial n grille_initiale =
Array.init
;;
In [61]:
let mutation n grille_initiale individu =
XXX
;;
In [ ]:
let algorithme_genetique nb1 nb2 nb3 ii m generation =
let n = nb1 + nb2 + nb3 in
let population = List.init (fun i -> ii ()) in
In [ ]:
let resolution_genetique_sudoku n grille_initiale =
let ii = fun () -> individu_initial n grille_initiale in
let m = fun individu -> mutation n grille_initiale individu in
In [ ]: